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<cstring>
#include<cstdlib>
using namespace std;
#define MAXR 1000

int dp[1010][1010];
int doll[1010][1010];

int main()
{
    int n,m;
    while(cin >>n)
    {
        if(n==0)break;
        memset(dp,0,sizeof(dp));
        memset(doll,0,sizeof(doll));
        int h,r;
        for(int i=0;i<n;i++)
        {
            cin>>h>>r;
            doll[h][r]=1;
        }
        cin >> m;
        for(int i=0;i<m;i++)
        {
            cin>>h>>r;
            doll[h][r]=1;
        }

        for(int i=1;i<MAXR;i++)
        {
            for(int j=1;j<MAXR;j++)
            {
                int temp;
                if(doll[i][j]==1)
                {
                    temp = max(dp[i-1][j],dp[i][j-1]);
                    dp[i][j] = max(1+dp[i-1][j-1],temp);
                }
                else
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
            }
        }
        cout << dp[999][999] << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<short, short> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

vector<P> v;
int n, m;
int dp[201][1001][1001];

int solve(int i, int h, int r)
{
	if (i == v.size()) return 0;
	if (dp[i][h][r] >= 0) return dp[i][h][r];
	int res;
	if (v[i].first > h&&v[i].second > r)
	{
		res = max(solve(i + 1, h, r), solve(i + 1, v[i].first, v[i].second) + 1);
	}
	else res = solve(i + 1, h, r);
	return dp[i][h][r] = res;
}

int main()
{
	while (cin >> n, n)
	{
		v.clear();
		MS(dp, -1);
		REP(i, n)
		{
			int a, b; cin >> a >> b;
			v.push_back(P(a, b));
		}
		cin >> m;
		REP(i, m)
		{
			int a, b; cin >> a >> b;
			v.push_back(P(a, b));
		}
		sort(ALL(v));
		cout << solve(0, 0, 0) << 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,le);
    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=0;
    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 <cstring>
#include <queue>

using namespace std;

typedef struct DOLL
{
	int h, r;
	
	bool operator > (const DOLL& d) const
	{
		return (h > d.h && r > d.r);
	}
	
	bool operator < (const DOLL& d) const
	{
		return (h < d.h && r < d.r);
	}
} D;

typedef struct Node
{
	D d;
	int num;
};

const int MAX = 100;

int n, m;
D A[MAX];
D B[MAX];

int AN[MAX];
int BN[MAX];

void solve()
{
	queue<Node> q;
	memset(AN, 0, sizeof(int)*n);
	memset(BN, 0, sizeof(int)*m);
	
	int max =0;
	Node n1 = {A[0], 1};
	Node n2 = {B[0], 1};
	q.push(n1);
	q.push(n2);
	while(!q.empty())
	{
		Node current = q.front();
		q.pop();
		
		if(max < current.num)
		{
			max = current.num;
		}
		
		for(int i=0; i<n; i++)
		{
			if(current.d < A[i])
			{
				if(AN[i] >= current.num+1) continue;
				
				Node new_node = {A[i], current.num+1};
				q.push(new_node);
				AN[i] = current.num+1;
				break;
			}
		}
		
		for(int i=0; i<m; i++)
		{
			if(current.d < B[i])
			{
				if(BN[i] >= current.num +1) continue;
				
				Node new_node = {B[i], current.num+1};
				q.push(new_node);
				BN[i] = current.num+1;
				break;
			}
		}
	}
	
	cout << max << endl;
}

int main(int argc, char** argv)
{
	int h, r;
	
	while(cin >> n, n)
	{
		for(int i=0; i<n; i++)
		{
			cin >> A[i].h >> A[i].r;
		}
		
		cin >> m;
		for(int i=0; i<m; i++)
		{
			cin >> B[i].h >> B[i].r;
		}
		
		solve();
	}
}