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 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];

int solve(int i, int r)
{
	if (i == v.size()) return 0;
	if (dp[i][r] >= 0) return dp[i][r];
	int res;
	if (v[i].second > r)
	{
		res = max(solve(i + 1, r), solve(i + 1, v[i].second) + 1);
	}
	else res = solve(i + 1, r);
	return dp[i][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));
		REP(i, v.size()-1)
		{
			if (v[i].first == v[i + 1].first)
			{
				v.erase(v.begin()+(v[i].second > v[i + 1].second ? i : i + 1));
				i--;
			}
		}
		cout << solve(0, 0) << endl;
	}
	return 0;

}

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

struct doll{
    int h, r;
    doll(int height, int radius) : h(height), r(radius) {}
    bool  operator<(const doll other) const{
        if(h != other.h)    return h < other.h;
        else                return r < other.r;
    }
};

int main(){
    while(1){
        int n, m, h, r;
        cin >> n;

        if(n == 0)  break;

        vector<doll> dolls;

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

        cin >> m;

        for(int i = 0; i < m; i++){
            cin >> h >> r;
            dolls.push_back(doll(h,r));
        }
    
        sort(dolls.begin(), dolls.end());

        n += m;
        
        int len[n];
        for(int i = 0; i < n; i++)  len[i] = 1;

        for(int i = 0; i < n; i++){
            for(int j = 0; j < i; j++){
                if(dolls[j].h < dolls[i].h && dolls[j].r < dolls[i].r && len[j] + 1 > len[i]){
                    len[i] = len[j] + 1;
                }
            }
        }

        int ans = 0;
        for(int i = 0; i < n; i++)  ans = max(ans, len[i]);
        cout << ans << endl;
    }

    return 0;
}

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

class Container
{
public:
	void append( pair< int, int > p )
	{
		masterList.push_back( p );
	}
	void cSort()
	{
		sort( masterList.begin(), masterList.end() );
		sortedList.clear();
		vector< pair< int, int > >::iterator itr = masterList.begin();
		while( itr != masterList.end() ){
			vector< pair< int, int > > v;
			int x = itr->first;
			v.push_back( *itr );
			++itr;
			if( itr != masterList.end() && itr->first == x ){
				v.push_back( *itr );
				++itr;
			}
			sortedList.push_back( v );
		}
	}

	int countContainment()
	{
		int result = 0;
		int dp[ 201 ][ 1000 ];
		memset( dp, 0, sizeof(dp) );
		for( int i = 0; i < (int)sortedList.size(); ++i ){
			int r = sortedList[ i ][ 0 ].second;
			for( int j = 0; j < r; ++j ){
				dp[ i + 1 ][ j ] = dp[ i ][ j ];
			}
			for( int j = r; j < 1000; ++j ){
				dp[ i + 1 ][ j ] = max( dp[ i ][ j ], dp[ i ][ r - 1 ] + 1 );
			}
		}

		return dp[ sortedList.size() ][ 1000 - 1 ];
	}

	void cSwap()
	{
		for( int i = 0; i < (int)masterList.size(); ++i ){
			int t = masterList[ i ].first;
			masterList[ i ].first = masterList[ i ].second;
			masterList[ i ].second = t;
		}
	}


	vector< pair< int, int > > masterList;
	vector< vector< pair< int ,int > > > sortedList;
};

int main()
{
	int n, m;

	while( cin >> n && n ){
		Container cont;
		for( int i = 0; i < n; ++i ){
			pair< int, int > p;
			cin >> p.first >> p.second;
			cont.append( p );
		}
		cin >> m;
		for( int i = 0; i < m; ++i ){
			pair< int, int > p;
			cin >> p.first >> p.second;
			cont.append( p );
		}

		int result = 0;
		cont.cSort();
		result = cont.countContainment();
		cont.cSwap();
		cont.cSort();
		cout << max( result, cont.countContainment() ) << 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;
	bool operator<(const M& m) const{
		return h<m.h;
	}
};

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;
		FOR(ami,0,aml) ms[ami]=ams[ami];
		FOR(bmi,0,bml) ms[aml+bmi]=bms[bmi];
		sort(ms,ms+ml);

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