Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"sort"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		hr := make([]entry, 0)
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			first, _ := strconv.Atoi(buf[0])
			second, _ := strconv.Atoi(buf[1])
			mt := entry{first, second}
			hr = append(hr, mt)
		}
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			first, _ := strconv.Atoi(buf[0])
			second, _ := strconv.Atoi(buf[1])
			mt := entry{first, second}
			hr = append(hr, mt)
		}
		sort.Sort(ByFirst(hr))
		fmt.Println(LIS(hr))
	}
}

type entry struct {
	first, second int
}

type ByFirst []entry

func(f ByFirst) Len() int {
	return len(f)
}

func (f ByFirst) Swap(i, j int) {
	f[i], f[j] = f[j], f[i]
}

func (f ByFirst) Less(i, j int) bool {
	return f[i].first < f[j].first
}

func LIS(hr []entry) int {
	n := len(hr)
	lis := make([]int, n)
	lis[n-1] = 1
	for i := n-2; i > -1; i-- {
		m := 0
		for j := i+1; j < n; j++ {
			if hr[i].first < hr[j].first && hr[i].second < hr[j].second && lis[j] > m {
				m = lis[j]
			}
		}
		lis[i] = m+1
	}
	return lis[0]
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <iomanip>
  
using namespace std;

#define pi 3.141592653589793

int main()
{
	int n, m;
	while(1){
		cin >> n;
		if(n == 0) break;
		vector<pair<int, int> > mato(200);
		for(int i = 0; i < n; i++){
			cin >> mato[i].first >> mato[i].second;
		}
		cin >> m;
		for(int i = 0; i < m; i++){
			cin >> mato[n + i].first >> mato[n + i].second;
		}
		sort(mato.begin(), mato.begin() + m + n);
		int cnt = 0;
		int maxh = 0, maxr = 0;
		for(int i = 0; i < n + m; i++){
			// cout << mato[i].first << " " << mato[i].second;
			if(mato[i].first > maxh && mato[i].second > maxr){
				cnt++;
				maxh = mato[i].first;
				maxr = mato[i].second;
			}
			// cout << " " << cnt << endl;
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <map>
using namespace std;
typedef pair<int, int> P;

const int MAX_N = 1001;
int dp[MAX_N];
//int h[MAX_N * 2], r[MAX_N * 2];
// ???????????????

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	int n;
	while(cin >> n && n != 0){
	//cin >> n;
	vector<P> vec;
	
	for(int i = 0; i < n; ++i){
		int h, r;
		cin >> h >> r;
		vec.push_back(P(h, r));
		//cin >> h[i] >> r[i];
	}
	
	int m;
	cin >> m;
	
	for(int i = n; i < n + m; ++i){
		int h, r;
		cin >> h >> r;
		vec.push_back(P(h, r));
		//cin >> h[i] >> r[i];
	}
	
	sort(vec.begin(), vec.end());
	
	for(int i = 0; i < n + m; ++i){
		//cout << vec[i].first << " " << vec[i].second << endl;
		dp[i] = 1;
	}
	
	int ans = 0;
	for(int i = 0; i < n + m; ++i){
		for(int j = 0; j < n + m; ++j){
			if(vec[i].first > vec[j].first && vec[i].second > vec[j].second){
				dp[i] = max(dp[i], dp[j] + 1);
			}
			ans = max(ans, dp[i]);
		}
	}
	
	cout << ans << endl;
	}
}

C++: //Solution for aoj:0157 Russian Dolls
#include<iostream>
#include<algorithm>
using namespace std;
typedef pair<int, int>P;
const int INF = 1 << 30;
int n, m, ans, p, s, s2, s3;
pair<int,int> doll[201];
int dp[2][201],dp2[201];
int main(){
	while (cin >> n){
		if (!n)
			return 0;
		fill(dp[0], dp[0] + 201, INF);
		fill(dp[1], dp[1] + 201, INF);
		fill(dp2, dp2 + 201, INF);
		for (int i = 1; i <= n; i++){
			int a, b;
			cin >> a >> b;
			doll[i].first = a;
			doll[i].second = b;
		}
		cin >> m;
		for (int i = n + 1; i <= n + m; i++){
			int a, b;
			cin >> a >> b;
			doll[i].first = a;
			doll[i].second = b;
		}
		sort(doll + 1, doll + 1 + n + m);
		for (int i = 1; i <= n + m; i++){
			p = lower_bound(dp[0] + 1, dp[0] + n + m + 1, doll[i].first) - dp[0];
			if (dp[1][p] > doll[i].second){
				dp[0][p] = doll[i].first;
				dp[1][p] = doll[i].second;
			}
		}
		for (int i = 1; i <= n + m; i++){
			*lower_bound(dp2 + 1, dp2 + n + m + 1, dp[1][i]) = dp[1][i];
		}
		cout <<lower_bound(dp2 + 1, dp2 + n + m + 1, INF) - dp2 -1<< endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct D{int h, r;};

bool ascendDr(const D& d1, const D& d2){
  return d1.r < d2.r;
}
bool ascendDh(const D& d1, const D& d2){
  return d1.h < d2.h;
}

int main(void){
  int n, m, dp[200];
  D dolls[200];
  while(cin >> n){
    if(n == 0) break;

    for(int i = 0; i < n; i++) cin >> dolls[i].h >> dolls[i].r;
    cin >> m;
    for(int i = 0; i < m; i++) cin >> dolls[n+i].h >> dolls[n+i].r;

    int lis_r = 0, lis_h = 0;
    sort(dolls, dolls + n + m, ascendDr);

    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      dp[i] = 1;
      for(int j = 0; j < i; j++){
        if(dolls[j].r < dolls[i].r && dolls[j].h && dolls[i].h){
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
    }
    lis_r = dp[n+m-1];

    sort(dolls, dolls + n + m, ascendDh);

    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      dp[i] = 1;
      for(int j = 0; j < i; j++){
        if(dolls[j].r < dolls[i].r && dolls[j].h && dolls[i].h){
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
    }
    lis_h = dp[n+m-1];

    cout << max(lis_h, lis_r) << endl;
  }
  return 0;
}

C++: #include <iostream>
using namespace std;

struct Doll{
  int h, r;
};

int lis(Doll d1[], Doll d2[], int n, int m) {
  int size = n+m*(n+1)+1;
  int L[size];
  Doll A[size];
  int p = 1;
  A[0].h = A[0].r = 0;
  for(int i = 0; i <= n; i++) {
    for(int j = 0; j < m; j++) {
      A[p++] = d2[j];
    }
    if(i == n) break;
    A[p++] = d1[i];
  }
  for(int i = 0; i < size; i++) {
    L[i] = 0;
  }

  for(int i = 1; i < size; i++) {
    for(int j = 0; j < i; j++) {
      if(A[j].h < A[i].h && A[j].r < A[i].r && L[j]+1 > L[i]) {
	L[i] = L[j] + 1;
      }
    }
  }

  int ans = 0;
  for(int i = 0; i < size; i++) {
    if(L[i] > ans) ans = L[i];
  }
  return ans;
}

int main() {
  while(1) {
    int n, m;

    cin >> n;
    if(n == 0) break;
    Doll d1[n];
    for(int i = 0; i < n; i++) cin >> d1[i].h >> d1[i].r;

    cin >> m;
    Doll d2[m];
    for(int i = 0; i < m; i++) cin >> d2[i].h >> d2[i].r;

    cout << lis(d1, d2, n, m) << endl;
  }
  return 0;
}