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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
#include <list>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define F first
#define S second


int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

typedef struct{
  int h,r;
}mat;

bool lessh(const mat&l,const mat&r){return l.h<r.h;};
bool lessr(const mat&l,const mat&r){return l.r<r.r;};


int low[200],ans;
mat hr[200];

main(){
  int n;
  while(cin>>n,n){
    rep(i,n)cin>>hr[i].h>>hr[i].r;
    int m;
    cin>>m;
    rep(i,m)cin>>hr[i+n].h>>hr[i+n].r;

    
    memset(low,0,sizeof(0));

    stable_sort(hr,hr+m+n,lessr);
    stable_sort(hr,hr+m+n,lessh);


    int ans=0;
    rep(i,n+m){
      rep(j,i){
	if(hr[j].h<hr[i].h && hr[j].r<hr[i].r){
	  low[i]=max(low[i],low[j]+1);
	  ans=max(ans,low[i]);
	}
      }
    }
    //rep(i,m+n)cout<<hr[i].h<<" "<<hr[i].r<<" "<<low[i]+1<<endl;
    cout<<ans+1<<endl;
  }
}

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

typedef pair<int,int> P;

int main(){
	int n,m,h,r;
	vector<P> mat;
	vector<int> l;
	while(cin >> n,n){
		for(int i = 0;i < n;i++) {
			cin >> h >> r;
			mat.push_back(P(h,r));
		}
		cin >> m;
		for(int i = n;i < n + m;i++){
			cin >> h >> r;
			mat.push_back(P(h,r));
		}
		sort(mat.begin(),mat.end());
		for(int i = n + m - 1;i > 0;i--){
			if(mat[i].first == mat[i - 1].first) mat.erase(mat.begin() + i);
		}
		for(int i = 0;i < n + m;i++){
			if(l.empty()) l.push_back(mat[i].second);
			else{
				if(l.back() < mat[i].second) l.push_back(mat[i].second);
				else l[lower_bound(l.begin(),l.end(),mat[i].second) - l.begin()] = mat[i].second;
			}
		}
		cout << l.size() << endl;
		l.clear();
		mat.clear();
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef pair<int,int> pi;

vector<pi> a;
int A;

int dp[200];
int dfs(int now)
{
    if(dp[now]>=0) return dp[now];

    int ret=1;
    rep(i,A)
    {
        if(a[i].fi<a[now].fi && a[i].se<a[now].se)
        {
            ret=max(ret,1+dfs(i));
        }
    }

    return dp[now]=ret;
}

int main()
{
    int n,m;
    while(cin >>n,n)
    {
        a.clear();

        rep(i,n)
        {
            int h,r;
            scanf(" %d %d", &h, &r);
            a.pb(pi(h,r));
        }
        cin >>m;
        rep(i,m)
        {
            int h,r;
            scanf(" %d %d", &h, &r);
            a.pb(pi(h,r));
        }

        //n+m
        A=a.size();

        int ans=1;
        memset(dp,-1,sizeof(dp));
        rep(i,A) ans=max(ans,dfs(i));

        cout << ans << endl;
    }
    return 0;
}

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

int n, m;
int h1[100], h2[100], r1[100], r2[100];
int next1[100], next2[100];

int calc(int idx, int d, bool init)
{
	static int dp[2][100];
	
	if(init) {
		for(int i = 0; i < n; ++i)
			dp[0][i] = -1;
		for(int i = 0; i < m; ++i)
			dp[1][i] = -1;
	}
	
	int ret = 0;
	
	if((idx < 0) || (d == 0 && idx >= n) || (d == 1 && idx >= m))
		return 0;
		
	if(dp[d][idx] != -1) {
		//printf("%d %d -> %d\n", d, idx, dp[d][idx]);
		return dp[d][idx];
	}
	
	ret = calc(idx + 1, d, false);
	if(d == 0)
		ret = max(ret, calc(next1[idx], 1, false));
	else
		ret = max(ret, calc(next2[idx], 0, false));
	
	//printf("%d %d -> %d\n", d, idx, ret + 1);
	return dp[d][idx] = (ret + 1);
}

int main()
{
	while(true) {
	
		int idx;
		
		scanf("%d", &n);
		if(n == 0)
			break;
		for(int i = 0; i < n; ++i)
			scanf("%d%d", &h1[i], &r1[i]), next1[i] = -1;
		scanf("%d", &m);
		if(m == 0)
			break;
		for(int i = 0; i < m; ++i)
			scanf("%d%d", &h2[i], &r2[i]), next2[i] = -1;
		
		idx = 0;
		for(int i = 0; i < n; ++i) {
			
			while(idx < m) {
				if(h1[i] < h2[idx] && r1[i] < r2[idx])
					break;
				++idx;
			}
			
			if(idx >= m)
				break;
			next1[i] = idx;
		}
		
		idx = 0;
		for(int i = 0; i < m; ++i) {
			
			while(idx < n) {
				if(h2[i] < h1[idx] && r2[i] < r1[idx])
					break;
				++idx;
			}
			
			if(idx >= n)
				break;
			next2[i] = idx;
		}
		
		int ans = calc(0, 0, true);
		ans = max(ans, calc(0, 1, false));
		
		printf("%d\n", ans);
	}
	
	return 0;
}

C++: #include<bits/stdc++.h>
#define P pair<int,int>
using namespace std;

P a[200];
int b;
int memo[200];
int saiki(int c){
	if(memo[c]!=-1)return memo[c];
	int MAX=1;
	for(int i=0;i<b;i++){
		if(a[i].first<a[c].first&&a[i].second<a[c].second){MAX=max(MAX,saiki(i)+1);}
		}
		return memo[c]=MAX;
	}
signed main(){
	int c;
	while(cin>>c,c){
		memset(memo,-1,sizeof(memo));
	for(int d=0;d<c;d++){
		scanf("%d%d",&a[d].first,&a[d].second);
		}
		b=c;cin>>c;
		for(int d=0;d<c;d++){
		scanf("%d%d",&a[b+d].first,&a[b+d].second);
		}
	b+=c;
	int k=0;
	for(int i=0;i<b;i++){
		k=max(k,saiki(i));
		}
		cout<<k<<endl;
	}
	}

C++: #include<stdio.h>
int dp[200];//iが一番外の最大値
int h[200];
int r[200];
int m,n;
int max(int a,int b){return a>b?a:b;}
int dfs(int x){
	if(dp[x]!=-1)return dp[x];
	int i;
	int ans=1;
	for(i=0;i<m+n;i++){
		if(h[i]<h[x]&&r[i]<r[x])ans=max(ans,dfs(i)+1);
	}
	return dp[x]=ans;
}
int main(){
	while(1){
	scanf("%d",&m);
	if(m==0)return 0;
	int i;
	for(i=0;i<m;i++)scanf("%d %d",&h[i],&r[i]);
	scanf("%d",&n);
	for(i=0;i<n;i++)scanf("%d %d",&h[i+m],&r[i+m]);
	int ans=1;
	for(i=0;i<m+n;i++)dp[i]=-1;
	for(i=0;i<m+n;i++)ans=max(ans,dfs(i));
	printf("%d\n",ans);
	}
}