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 <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>


typedef long long ll;
const double Eps = 1e-4;

using namespace std;

int main()
{
	int n[2];
	int h[2][100];
	int r[2][100];

	while (scanf("%d", n), *n)
	{
		for (int i = 1; i <= n[0]; ++i)
			scanf("%d %d", h[0]+i, r[0]+i);
		scanf("%d", n+1);
		for (int i = 1; i <= n[1]; ++i)
			scanf("%d %d", h[1]+i, r[1]+i);
		h[0][0] = h[1][0] = r[0][0] = r[1][0] = 0;
		h[0][n[0]+1] = h[1][n[1]+1] = r[0][n[0]+1] = r[1][n[1]+1] = 10000;

		int ans = 0;
		for (int w = 0; w < 2; ++w)
		{
			int* ha = h[w];
			int* hb = h[(w+1)%2];
			int* ra = r[w];
			int* rb = r[(w+1)%2];

			int sum = n[w];
			int j = 1;
			for (int i = 0; i < n[w]+1 && j <= n[w]; ++i)
			{
				while (hb[j] < ha[i] || rb[j] < ra[i])
					++j;
				while (ha[i] < hb[j] && hb[j] < ha[i+1]
					&& ra[i] < rb[j] && rb[j] < ra[i+1])
					{
						++sum;
						++j;
					}
			}
			if (sum > ans)
				ans = sum;
		}
		printf("%d\n", ans);
	}

	return 0;
}

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[2001];
int dp[2][2001], dp2[2001];
int main(){
	while (cin >> n){
		if (!n)
			return 0;
		fill(dp[0], dp[0] + 2001, INF);
		fill(dp[1], dp[1] + 2001, INF);
		fill(dp2, dp2 + 2001, 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;
#define MAX_NM 202

struct D{
  int r, h;
};

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

int main(void){
  int n, m, dp[MAX_NM];
  D a[MAX_NM];

  while(cin >> n, n){
    for(int i = 0; i < n; i++){
      cin >> a[i].h >> a[i].r;
    }
    cin >> m;
    for(int i = 0; i < m; i++){
      cin >> a[n+i].h >> a[n+i].r;
    }

    int lis_r = 0, lis_h = 0;
    sort(a, a + n + m, ascending_M_r);

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

    sort(a, a + n + m, ascending_M_h);

    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      dp[i] = 1;
      for(int j = 0; j < i; j++){
        if(a[j].r < a[i].r && a[j].h && a[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<vector>
#include<iostream>
#include<algorithm>

#define	mp	make_pair
#define	pb	push_back
#define	h	first
#define	r	second

using namespace std;

typedef	pair<int,int>	pii;

vector< vector<int> > child;
//vector<int> memo;
// max number of matrioshka which i-th matrioshka can have (i-th one is included)

int rec(int i)
{
//	if(memo[i]!=-1)	return memo[i];

	int nmax=1;
	for(int j=0;j<child[i].size();j++)
		nmax=max(nmax,1+rec(child[i][j]));
//	return memo[i]=nmax;
	return nmax;
}

int main()
{
	while(1){
		vector<pii>	mat;
		int n;	cin>>n;
		for(int i=0,h,r;i<n;i++)	cin>>h>>r,mat.pb(mp(h,r));
		int m;	cin>>m;
		for(int i=0,h,r;i<m;i++)	cin>>h>>r,mat.pb(mp(h,r));
		if(n==0)	break;

		child=vector< vector<int> >(n+m);
		for(int i=0;i<n+m;i++){
			for(int j=0;j<n+m;j++){
				if(mat[i].h>mat[j].h && mat[i].r>mat[j].r)
					child[i].pb(j);
			}
		}

//		memo=vector<int>(n+m,-1);
		int ans=0;
		for(int i=0;i<n+m;i++)	ans=max(ans,rec(i));
		cout<<ans<<endl;
	}

	return 0;
}