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 <bits/stdc++.h>

using namespace std;

#define MAX 514
#define INF 1145141919
typedef pair<int, int> pii;

int main()
{
    int N, M;
    while (cin >> N, N) {
        vector<pii> v;
        int a, b;        
        for (int i = 0; i < N; i++) {
            cin >> a >> b;
            v.push_back(pii(a, -b));
        }
        cin >> M;
        for (int i = 0; i < M; i++) {
            cin >> a >> b;
            v.push_back(pii(a, -b));
        }

        N += M;

        sort(v.begin(), v.end());
        int dp[MAX];
        fill(dp, dp + MAX, INF);
        for (int i = 0; i < N; i++) {
            int c = -v[i].second;
            *lower_bound(dp, dp + N, c) = c;
        }
        cout << (lower_bound(dp, dp + N, INF) - dp) << endl;
    }
    return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <map>

using namespace std;

#define fst first
#define snd second
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
typedef pair<int,int> pii;

int n,m;
int dp[210];
pii dolls[210];

const int inf=1<<27;

int main(void){
	while(cin >> n ,n){
		rep(i,210) dp[i]=0;

		rep(i,n) cin >> dolls[i].fst >> dolls[i].snd;
		cin >> m;
		rep(i,m) cin >> dolls[n+i].fst >> dolls[n+i].snd;
		
		sort(dolls,dolls+n+m);

		dp[0]=1;
		rep(i,n+m){
			rep(j,i){
				if(dolls[i].fst > dolls[j].fst && dolls[i].snd > dolls[j].snd)
					dp[i]=max(dp[i],dp[j]+1);
			}
			//cout << dolls[i].fst << " " << dolls[i].snd << endl;
			//cout << i << endl;
			//cout << dp[i] << endl;
		}
		int ans=0;
		rep(i,n+m){
			ans=max(ans,dp[i]);
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <list>
#include <map>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SUPAML 101
#define SUPBML 101

struct M{
	int h, r, i;
};

int aml,bml;
int ml;
M ams[SUPAML],bms[SUPBML];
M ms[SUPAML+SUPBML];
int rs[SUPAML+SUPBML];

int main(){
	while(true){
		cin >> aml;			if(!aml) return 0;
		FOR(ami,0,aml){
			int h,r;
			cin >> h >> r;
			ams[ami].h=h; ams[ami].r=r;
		}
		cin >> bml;
		FOR(bmi,0,bml){
			int h,r;
			cin >> h >> r;
			bms[bmi].h=h; bms[bmi].r=r;
		}
		
		ml = aml+bml;
		int ami=0,bmi=0;
		FOR(mi,0,ml){
			M m;
			if(ami==aml) m=bms[bmi++];
			else if(bmi==bml) m=ams[ami++];
			else { M am=ams[ami], bm=bms[bmi];
				if(am.h<bm.h || (am.h==bm.h)&&(am.r<=bm.r)) {m=am; ami++;}
				else {m=bm; bmi++;}
			}
			m.i=mi;ms[mi]=m;
		}

		fill(rs,rs+ml,1);
		FOR(mi,0,ml){	M m=ms[mi];
			FOR(ni,0,mi){	M n=ms[ni];
				if(n.h<m.h&&n.r<m.r) rs[mi] = max(rs[mi],rs[ni]+1);
				else rs[mi] = max(rs[mi],rs[ni]);
			}
		}

		cout << rs[ml-1] << endl;
	}
}

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

#define ll (long long)
#define REP(i,n) for(int i = 0;i<(int)n;i++)
#define FOR(i,a,b) for(int i = (a);i < (b);i++)
#define RFOR(i,a,b) for(int i = (a);i>(b);i--)
#define COUNTER int i= 0,j = 0,k = 0,l = 0,m = 0, n= 0;
#define RETURN cout << endl;
/*vector<vector<int>>ArrayName = vector<vector<int>>(sizeofx, vector<int>(sizeofy, 0));*/
using namespace std;


int main() {
    int answer = 0;
    while (true) {
        /*pair??§????????§??±???*/
        vector< pair<int, int> > vp;
        int dp[205];
        int n; cin >> n; if (!n) break;
        for (int i = 0; i < n; i++) {
            int h, r; cin >> h >> r;
            vp.push_back(pair<int, int>(h, r));
        }
        int m; cin >> m;
        for (int i = 0; i < m; i++) {
            int h, r; cin >> h >> r;
            vp.push_back(pair<int, int>(h, r));
        }
        sort(vp.begin(), vp.end());
        //REP(i,vp.size()){cout << vp[i].first <<" " << vp[i].second<< endl;}
           int checkh = 0,checkr = 0;
        answer = 0;
        REP(i, m+n)
        {
            if(!i)
                answer++;
            else
                if(vp[i].first > checkr && vp[i].second >checkh)
                {
                    checkr = vp[i].first;
                    checkh = vp[i].second;
                    answer++;
                }
            
            
        }
        cout << answer << endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1<<30;
int main(){
	int n;
	while(cin>>n,n){
		vp in;
		rep(i,n){
			int a,b;
			cin>>a>>b;
			in.pb(pii(a,b));
		}
		int s=n;
		cin>>n;
		s+=n;
		rep(i,n){
			int a,b;
			cin>>a>>b;
			in.pb(pii(a,b));
		}
		sort(all(in));
		vi dp(s);
		dp[0]=1;
		loop(i,1,s){
			rep(j,i)
				if(in[j].first<in[i].first&&
					in[j].second<in[i].second)
					dp[i]=max(dp[i],dp[j]+1);
		}
		int out=0;
		rep(i,s)out=max(out,dp[i]);
		cout<<out<<endl;
	}
}

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);
    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;
    }
}