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 <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
void print(vector<vector<int> > dp){
  cout << "---------------" << endl;
  for(int i = 0;i  < dp.size();i++){
	for(int j = 0;j < dp.size();j++){
	  cout << dp[i][j] << " ";
	}
	cout << endl;
  }
}
int cal(vector<pair<int,int> > vec){
  vector<vector<int> > dp(vec.size(),vector<int>(vec.size(),0));  
  for(int i = 0;i < vec.size()-1;i++){
	for(int j = 0;j < vec.size();j++){
	  for(int k = j+1;k < vec.size();k++){
		if(vec[j].first > vec[k].first && vec[j].second > vec[k].second){
		  dp[i+1][k] = max(dp[i][j]+1,dp[i+1][k]);

		}
	  }
	}
  }
  int ans = 0;
  for(int i = 0;i < vec.size();i++){
	ans = max(ans,dp[vec.size()-1][i]);
  }
  return ans;
}
void print2(vector<int> vec){
  for(int i = 0;i < vec.size();i++)
	cout << vec[i] << " ";
  cout << endl;

}
int cal2(vector<pair<int,int> > vec){
  vector<int> dp(vec.size(),0);
  for(int i = 0;i < vec.size();i++){
	for(int j =i+1;j < vec.size();j++){
	  if(vec[i].first > vec[j].first && vec[i].second > vec[j].second){
		dp[j] = max(dp[i]+1,dp[j]);
	  }
	}
//	print2(dp);
  }
  int ans = 0;
  for(int i = 0;i < dp.size();i++){
	ans = max(ans,dp[i]);
  }
  return ans;
}
int main(){
  int n;

  while(cin >> n && n!=0){
	vector<pair<int,int> > vec;
	vec.emplace_back(10000,10000);
	for(int i = 0;i < n;i++){
	  int h,r;
	  cin >> h >> r;
	  vec.emplace_back(h,r);  
	}
	cin >> n;
	for(int i = 0;i < n;i++){
	  int h,r;
	  cin >> h >> r;
	  vec.emplace_back(h,r);
	}
	sort(vec.begin(),vec.end(),greater<pair<int,int> >());
	cout << cal(vec) << endl;
//	cout << cnt << endl;
  }
  return 0;
}

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

int main(){
	int n[2],c[2];
	while(cin>>n[0],n[0]){
		int t1,t2,p;
		vi h[2],r[2],mx[2];
		rep(p,2){
			rep(i,n[p]){
				cin>>t1>>t2;
				h[p].push_back(t1);
				r[p].push_back(t2);
				mx[p].push_back(0);
			}
			if(!p)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;
		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;
}

C++: #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;

#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)
typedef pair<int,int> pii;
int n,m;
pii a[222];
int dp[222];

inline bool le(pii const& t, pii const& u){
    return t.first<u.first && t.second<u.second;
}

int solve(){
    sort(a,a+n+m);
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=1;i<n+m;i++){
        for(int j=0;j<i;j++){
            if(le(a[j],a[i])){
                dp[i]=max(dp[i],dp[j]+1);
            }
        }
    }
    int ans=1;
    for(int i=0;i<n+m;i++){
        ans=max(ans,dp[i]);
    }
    return ans;
}

int main(){
    while(cin>>n && n){
        rep(i,n)cin>>a[i].first>>a[i].second;
        cin>>m;
        rep(i,m)cin>>a[i+n].first>>a[i+n].second;
        cout << solve() << endl;
    }
}

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 <vector>
#include <algorithm>
using namespace std;

const int MAX = 1001;
typedef pair<int,int> P;

int main(){
    int n,m,h,r;
    
    while(cin >> n ,n){
        vector<P> v;
        int dp[MAX] = {0};
        
        for(int i = 0 ; i < n ; i++){
            cin >> h >> r;
            v.push_back(P(h,r));
        }
        
        cin >> m;
        for(int i = 0 ; i < m ; i++){
            cin >> h >> r;
            v.push_back(P(h,r));
        }
        
        sort(v.begin(),v.end());
        
        int res = 0;
        for(int i = 0 ; i < n+m ; i++){
            dp[i] = 1;
            for(int j = 0 ; j < i ; j++){
                if(v[j].first < v[i].first && v[j].second < v[i].second){
                    dp[i] = max(dp[i],dp[j]+1);
                }
                res = max(res,dp[i]);
            }
        }
        cout << res << endl;
    }
    
    return 0;
}

C++: #include <iostream>
#include <utility>
#include <algorithm>
#include <vector>

using namespace std;

int nm;
vector<pair<int,int> > mat;
vector<vector<int> > DP;

int slave(int h,int r){
    if(DP[h][r] >= 0) return DP[h][r];
    int ret = 0;
    for(int i=0;i<nm;i++){
        if(h < mat[i].first && r < mat[i].second){
            ret = max(ret,slave(mat[i].first,mat[i].second)+1);
        }
    }
    return DP[h][r] = ret;
}

int main(){
    while(true){
        mat.clear();
        DP.clear();
        int n,m;
        cin >> n;
        if(n==0) return 0;
        for(int i=0;i<n;i++){
            int h,r;
            cin >> h >> r;
            mat.push_back(make_pair(h,r));
        }
        cin >> m;
        for(int i=0;i<m;i++){
            int h,r;
            cin >> h >> r;
            mat.push_back(make_pair(h,r));
        }
        nm = n+m;
        for(int i=0;i<1002;i++){
            DP.push_back(vector<int>(1002,-1));
        }
        cout << slave(0,0) << endl;

    }
    return 0;
}