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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;

#define BIG_NUM 2000000000

using namespace std;

struct Info{
	Info(){
		h = 0;
		r = 0;
		used = false;
		may_in_num = in_num = 0;
	}
	int h,r,may_in_num,in_num;
	bool used;
};

void func(int n){

	Info info[401];
	int m;

	for(int i = 0; i < n; i++){
		scanf("%d %d",&info[i].h,&info[i].r);
	}

	scanf("%d",&m);

	for(int i = 0; i < m; i++){
		scanf("%d %d",&info[n+i].h,&info[n+i].r);
	}

	int count,maximum = 0,limit = n+m;

	while(true){
		count = 0;

		for(int i = 0; i < limit; i++){
			if(info[i].used)continue;
			for(int k = 0; k < limit; k++){
				if(info[k].used == false && info[k].h < info[i].h && info[k].r < info[i].r){
					info[i].may_in_num++;
				}
			}
		}

		for(int i = 0; i < limit; i++){
			if(info[i].may_in_num == 0){
				info[i].used = true;

				for(int k = 0; k < limit; k++){
					if(info[k].h > info[i].h && info[k].r > info[i].r){
						if(info[k].in_num < info[i].in_num+1){
							count++;
							info[k].in_num = info[i].in_num+1;
							maximum = max(maximum,info[k].in_num);
						}
					}
				}
			}
		}

		for(int i = 0; i < limit; i++){
			if(info[i].used == false){
				info[i].may_in_num = 0;
			}
		}

		if(count == 0)break;
	}

	printf("%d\n",maximum+1);

}

int main(){

	int n;

	while(true){
		scanf("%d",&n);
		if(n == 0)break;

		func(n);
	}

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define MP(a, b) make_pair(a, b)
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int n, m;
vector<pi>s;
int dp[1024];

int main(){
  while(scanf("%d", &n) && n){
    s.clear();
    int h, r;
    rep(i, n){
      scanf("%d%d", &h, &r);
      s.push_back(MP(h, r));
    }
    scanf("%d", &m);
    rep(i, m){
      scanf("%d%d", &h, &r);
      s.push_back(MP(h, r));
    }

    sort(s.begin(), s.end());
    memset(dp, 0, sizeof(dp));

    int res = 0;
    rep(i, s.size()){
      dp[i] = 1;
      rep(j, i) if(s[j].F < s[i].F && s[j].S < s[i].S){
	dp[i] = max(dp[i], dp[j] + 1);
      }
      res = max(res, dp[i]);
    }
    printf("%d\n", res);
  }
  return 0;
}

C++: #include<iostream>
#include<cstdlib>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
#define MAX 3000
using namespace std;
int test=0;
int n,m;
int dp[MAX+1];
class Doll{
public:
   int h;
   int r;
   bool operator < (const Doll &d)const{
      if(h == d.h)return r < d.h;//hが同じ時ｒで比較
      return h < d.h;//普段はｈで比較
   }
};

Doll Dolls[MAX];

void solve(void){
   dp[0] = 0;
   dp[1] = 1;
   for(int i=0;i<(n+m);i++){
      //dp[i+1] = 1;
      for(int j=0;j<i;j++){
	 if(Dolls[j].r < Dolls[i].r){
	    dp[i+1] = max(dp[i],dp[j+1]+1);
	 }
      }
   }

   cout << dp[n+m] << endl;
}

int main(void){
   int h,r;
   while(1){
      cin >> n;
      if(n==0)break;
      for(int i=0;i<n;i++){
	 cin >> h >> r;
	 Dolls[i].h = h;
	 Dolls[i].r = r;
      }
      cin >> m;
      for(int i=n;i<(n+m);i++){
	 cin >> h >> r;
	 Dolls[i].h = h;
	 Dolls[i].r = r;
      }
      sort(Dolls,Dolls+(n+m)-1);
      solve();
      /*
      rep(i,n+m)
	 cout << Dolls[i].h << " " << Dolls[i].r << " " << dp[i+1] << endl;
      cout << endl;
      */
   }
   return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define f first
#define s second
#define pb(a,b) push_back(make_pair(a,b))
#define g(a,b) for(int i=a;i<b;i++)

int m,n,x,y;
vector<pair<int,int> >v;
int dfs(int i,int x,int y){
    if(i==v.size())return 0;
    return max(dfs(i+1,x,y),(v[i].f>x&&v[i].s>y)?dfs(i+1,v[i].f,v[i].s)+1:0);
}
int main(){
    while(cin>>n,n){
        v.clear();
        g(0,n){
            cin>>x>>y;
            v.pb(x, y);
        }
        cin>>m;
        g(0,m){
            cin>>x>>y;
            v.pb(x, y);
        }
        sort(v.begin(),v.end());
        cout<<dfs(0,0,0)<<endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>

#define NM 210

#define pb push_back
#define mp make_pair

using namespace std;

int dp[NM];

int main(void)
{
	int n,m;
	int h,r;
	int i1,i2;
	int max;

	while(1){
		cin>>n;
		if(n==0) break;

		vector<pair<int,int> > doll;
		memset(dp,0,sizeof(dp));

		for(i1=0;i1<n;i1++){
			cin>>h>>r;
			doll.pb(mp(h,r));
		}
		cin>>m;
		for(i1=0;i1<m;i1++){
			cin>>h>>r;
			doll.pb(mp(h,r));
		}

		sort(doll.begin(), doll.end());

		dp[0]=1;
		for(i1=1;i1<doll.size();i1++){
			for(i2=0;i2<=i1;i2++){
				if(doll[i1].first==doll[i2].first || doll[i1].second<=doll[i2].second) {
					//cout<<doll[i1].first<<" "<<doll[i1].second<<"   ";
					//cout<<doll[i2].first<<" "<<doll[i2].second<<endl;

					break;
				}
			}
			dp[i1]=dp[i2-1]+1;
		}

		max=0;
		for(i1=0;i1<n+m;i1++){
			if(max<dp[i1]) max=dp[i1];
		}

		cout<<max<<endl;
	}



	return 0;
}

C++: #include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(){
	int n[2],c[2];
	while(cin>>n[0],n[0]){
		int h[2][110],r[2][110],mx[2][110];
		rep(q,2){
			rep(i,n[q])mx[q][i]=0,cin>>h[q][i]>>r[q][i];
			if(!q)cin>>n[1];
		}
		mx[0][0]=(h[0][0]>h[1][0]&&r[0][0]>r[1][0])+1;
		mx[1][0]=(h[0][0]<h[1][0]&&r[0][0]<r[1][0])+1;
		int p;
		c[0]=c[1]=p=0;
		while(c[0]<n[0]||c[1]<n[1]){
			int tmax=0,f;
			rep(k,2){
				rep(i,n[k]){
					f=h[k][i]<h[p][c[p]]&&r[k][i]<r[p][c[p]];
					if(f&&tmax<mx[k][i])tmax=mx[k][i];
					if(f&&mx[k][i]==0){
						p^=1;
						goto NEXT;
					}
				}
			}
			mx[p][c[p]++]=tmax+1;
			NEXT:;
		}
		cout<<max(mx[0][n[0]-1],mx[1][n[1]-1])<<endl;
	}
	return 0;
}