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<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;
			if(c[p]==n[p]){
				p^=1;
				goto NEXT;
			}
			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++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 10e-8;

class mycmp{
public:
	bool operator()(const P p1, const P p2){
		if(p1.first < p2.first) return true;
		else if(p1.second < p2.second) return true;
		else false;
	}
};

int dp[202];

int main(){
	int n;

	while(~scanf("%d",&n)){
		if(n==0) break;
		vector<P> dolls;

		for(int i=0;i<n;i++){
			int h,r;
			scanf("%d %d",&h,&r);
			dolls.push_back(P(h,r));
		}

		int m;
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			int h,r;
			scanf("%d %d",&h,&r);
			dolls.push_back(P(h,r));
		}


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

		memset(dp,0,sizeof(dp));
		dp[0] = 1;

		for(int i=1;i<dolls.size();i++){
			for(int j=i-1;j>=0;j--){
				if(dolls[j].first < dolls[i].first
					&& dolls[j].second < dolls[i].second){
						dp[i] = max(dp[i],dp[j] + 1);
				}
				else dp[i] = dp[i-1];
			}
		}

		cout << dp[dolls.size()-1] << endl;
	}
}

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

int c[1000][1000];

int main() {
    int n;
    while (cin >> n, n) {
        vector<int> hs, rs;
        int h_max = 0, r_max = 0;
        for (int i = 0; i < n; i++) {
            int h, r;
            cin >> h >> r;
            hs.push_back(h);
            rs.push_back(r);
            h_max = max(h_max, h);
            r_max = max(r_max, r);
        }
        int m;
        cin >> m;
        for (int i = 0; i < m; i++) {
            int h, r;
            cin >> h >> r;
            hs.push_back(h);
            rs.push_back(r);
            h_max = max(h_max, h);
            r_max = max(r_max, r);
        }
        for (int y = 0; y <= h_max; y++) {
            for (int x = 0; x <= r_max; x++) {
                c[y][x] = 0;
            }
        }
        for (int i = 0; i < m+n; i++) {
            c[hs[i]][rs[i]] = 1;
        }
        for (int y = 1; y <= h_max; y++) {
            for (int x = 1; x <= r_max; x++) {
                c[y][x] = max(c[y][x] + c[y-1][x-1], max(c[y-1][x], c[y][x-1]));
            }
        }
        cout << c[h_max][r_max] << endl;
    }
}

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

struct Doll{
  int h,r;
  Doll(int a,int b){h=a;r=b;};
};

int n;
int h,r;
int m;
vector<Doll>doll;
int memo[201]={0};

bool comp(Doll a, Doll b){return a.h<b.h && a.r<b.r;};

int rec(int id)
{
  int ans=0;
  for(int i=0;i<n+m;i++){
    if(comp(doll[i],doll[id]))ans=max(rec(i)+1,ans);
  }

  return ans;
}

int main()
{
  while(cin>>n,n){
    doll.clear();
    for(int i=0;i<200;i++)memo[i]=0;

    for(int i=0;i<n;i++){
      cin>>h>>r;
      doll.push_back(Doll(h,r));
    }
    cin>>m;
    for(int i=0;i<m;i++){
      cin>>h>>r;
      doll.push_back(Doll(h,r));
    }

    doll.push_back(Doll(1001,1001));

    cout<<rec(n+m)<<endl;
  }
}

C++: #include <bits/stdc++.h>

using namespace std;

#define MAX 114
#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<iostream>
#include<queue>
using namespace std;
#define MAX_N 1000
int x[MAX_N], y[MAX_N], X[MAX_N][MAX_N], dist[MAX_N];
int A, B, N;
int main() {
	while (true) {
		cin >> A; if (A == 0) { break; } for (int i = 0; i < A; i++) { cin >> x[i] >> y[i]; }
		cin >> B; for (int i = A; i < A + B; i++) { cin >> x[i] >> y[i]; }N = A + B;
		for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (x[i] < x[j] && y[i] < y[j]) { X[i][j] = 1; } } }
		queue<int>Q; int res = 0; for (int i = 0; i < N; i++) { dist[i] = 0; Q.push(i); }
		while (true) {
			int a1 = Q.front(); Q.pop();
			for (int i = 0; i < N; i++) {
				if (X[a1][i] == 1 && dist[a1] + 1>dist[i]) { dist[i] = dist[a1] + 1; Q.push(i); }
			}
		}
		for (int i = 0; i < N; i++) { if (res < dist[i]) { res = dist[i]; } }cout << res << endl;
	}
}

C++: #include <cstdio>
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <map>
#define rep(i,n) for(int i=0; i<n; ++i)

using namespace std;
struct Doll{
    int H,R,L;
    bool operator<(const Doll& right)const {
        return H<right.H;
    }
    Doll(){
      H=0; R=0; L=0;
    }
};
int N, M;
int main(){
  while(cin >> N && N){
      vector<Doll> D;
      D.resize(200);
      rep(i,N){
        cin >> D[i+1].H >> D[i+1].R;
      }
      cin >> M;
      rep(i,M){
        cin >> D[N+i+1].H >> D[N+i+1].R;
      }
      sort(D.begin()+1, D.begin()+(N+M)+1);
      int ans = 0;
      for(int i=1; i<N+M+1; ++i){
        int m = 0;
        rep(j,i){
            if(D[j].R<D[i].R && D[j].H<D[i].H && m<D[j].L){
                m = D[j].L;
            }
        }
        D[i].L = m+1;
        ans = max(ans,D[i].L);
      }
      cout << ans << endl;
  }
}