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 <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define EACH( v, c ) for ( auto &v : c )

#define ITER( c ) __typeof( (c).begin() )
#define IREP( c, it ) for ( ITER(c) it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

void addary( vector< PII > &ary, int n )
{
	REP( i, 0, n )
	{
		int a, b;
		cin >> a >> b;
		ary.PB( MP( a, b ) );
	}

	return;
}

struct Tsort
{
	int N;
	VVI G;

	VI order;
	vector< bool > used;

	Tsort( VVI G ) : N( G.size() ), G( G ), used( vector< bool >( N, false ) )
	{
		return;
	}

	VI solve()
	{
		REP( i, 0, N )
		{
			rec( i );
		}
		
		reverse( ALL( order ) );

		return order;
	}

	void rec( int v )
	{
		if ( used[v] )
		{
			return;
		}

		used[v] = true;

		REP( i, 0, G[v].size() )
		{
			rec( G[v][i] );
		}

		order.PB( v );
	}
};

int solve( vector< PII > ary )
{
	const int N = ary.size();

	VVI G( N );

	REP( i, 0, N )
	{
		REP( j, 0, N )
		{
			if ( i == j )
			{
				continue;
			}

			if ( ary[i].fst < ary[j].fst && ary[i].snd < ary[j].snd )
			{
				G[i].PB( j );
			}
		}
	}

	VI order = Tsort( G ).solve();

	VI dp( N, 1 );

	REP( i, 0, N )
	{
		int cur = order[i];

		REP( j, 0, G[ order[i] ].size() )
		{
			int next = G[ cur ][j];

			dp[ next ] = max( dp[ next ], dp[ cur ] + 1 );
		}
	}

	return dp[ order.back() ];
}

int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	while ( true )
	{
		int n;
		cin >> n;

		if ( !n )
		{
			break;
		}

		vector< PII > ary;
		addary( ary, n );

		cin >> n;
		addary( ary, n );

		cout << solve( ary ) << endl;
	}

	return 0;
}

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

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplece_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define Edge pair< i_i, int>
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; scanf("%lld", &x);
#define int2(x, y) int x, y; scanf("%lld %lld", &x, &y);
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second
 
//int dxy[5] = {0, 1, 0, -1, 0};
// assign
 
signed main()
{
    int n;
    while (cin >> n, n) {
        vp yui(n);
        rep(i, n) {
            cin >> yui[i].fir >> yui[i].sec;
            yui[i].sec *= -1;
        }
        Int(m);
        rep(i, m) {
            i_i kyoko;
            cin >> kyoko.fir >> kyoko.sec;
            kyoko.sec *= -1;
            yui.pb(kyoko);
        }

        sort(all(yui));
        vi funami(1010, 0);
        rep(i, n+m) {
            int yuikyo = 0;
            rep(j, -yui[i].sec) {
                yuikyo = max(yuikyo, funami[j]);
            }
            funami[-yui[i].sec] = yuikyo+1;
        }
        int yuruyuri = 0;
        rep(i, 1010) {
            yuruyuri = max(yuruyuri, funami[i]);
        }
        cout << yuruyuri << endl;
    }

    return 0;
}

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


class Doll{
public:
    int h;
    int r;
    int dp;
    static bool Less(const Doll& l, const Doll& r){
	return l.h < r.h;
    }
    Doll(int x, int y, int z){
	h = x; r = y; dp = z;
    }
};

typedef vector<Doll> Dolls;

int main(){
    int n, m, h, r, result=0;
    Dolls dolls;
    while(cin >> n, n){
	for(int i=0; i<n; ++i){
	    cin >> h >> r;
	    Doll temp(h,r,1);
	    dolls.push_back(temp);
	}
	cin >> m;
	for(int i=0; i<m; ++i){
	    cin >> h >> r;
	    Doll temp(h,r,1);
	    dolls.push_back(temp);
	}
	sort(dolls.begin(), dolls.end(), Doll::Less);
	
	for(Dolls::iterator it1=dolls.begin()+1;it1 != dolls.end(); ++it1){
	    for(Dolls::iterator it2=dolls.begin();it2 != it1; ++it2){
		if(it2->h < it1->h && it2->r < it1->r && it1->dp <= it2->dp){
		    it1->dp = it2->dp + 1;
		    result = max(result, it1->dp);
		}
	    }
	}
	cout << result << endl;
	cerr << result << endl;
	dolls.clear();
	result = 0;
    }
}

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


int n, m;
pair<int, int> doll[200];
int dp[200] = { 1 };


int solve(){
	for (int i = 0; i < n + m; i++){
		vector<int> mem = { -1 };
		for (int j = 0; j < i; j++){
			if (doll[j].second < doll[i].second){
				mem.push_back(dp[j]);
			}
		}
		if (mem.back() != -1)
			dp[i] = *max_element(mem.begin(), mem.end()) + 1;
	}
	return *max_element(dp, dp + n + m);
}


void clear(){
	for (int i = 0; i < 200; i++){
		doll[i] = { 0, 0 };
		dp[i] = 0;
	}
	dp[0] = 1;
}


int main(){
	while (cin >> n, n){
		for (int i = 0; i < n; i++) cin >> doll[i].first >> doll[i].second;
		cin >> m;
		for (int i = n; i < n + m; i++) cin >> doll[i].first >> doll[i].second;

		sort(doll, doll + n + m);

		cout << solve() << endl;

		clear();
	}
	
	return 0;
}