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 <vector>
#include <cstring>
#include <algorithm>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

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

int dp[1005][1005];

int main()
{
	int n, m;
	while ( cin >> n && n ){
		vector <P> d;
		d.clear();
		memset (dp, 0, sizeof (dp ) );
		rep (i, n ){
			int h, r;
			cin >> h >> r;
			d.push_back (P (h, r ) );
		} // end rep 
		cin >> m;
		rep (i, m ){
			int h, r;
			cin >> h >> r;
			d.push_back (P (h, r ) );
		} // end rep
		sort (ALL (d ) );
		rep (i, n+m ){
			P b = d[i];
			if (dp[b.first][b.second] == 0 )
				dp[b.first][b.second] = 1;
			int cnt = dp[b.first][b.second];
			rep (j, n+m ){
				if (d[j].first > b.first && d[j].second > b.second ){
					if (dp[d[j].first][d[j].second] < cnt+1 ){
						dp[d[j].first][d[j].second] = cnt+1;
						cnt++;
					}else{
						cnt = dp[d[j].first][d[j].second];
					} // end if
					b = d[j];
				} // end if
			} // end for
		} // end rep
		int res = 0;
		rep (i, 1005 )
			rep (j, 1005 )
				res = max (res, dp[i][j] );
		cout << res << endl;
		
	} // end loop
	return 0;
}

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

int main() {
  int n, m, x, y;
  
  while(cin >> n && n) {
    vector<pair<int, int> > vec;
    for(int i=0; i<n; i++) {
      cin >> x >> y;
      vec.push_back(make_pair(x, y));
    }
    
    cin >> m;
    for(int i=0; i<m; i++) {
      cin >> x >> y;
      vec.push_back(make_pair(x, y));
    }
    
    int N = n+m;
    
    vector<int> dp(N, 0);
    
    sort(vec.begin(), vec.end());
    
    int ans = 0;
    
    for(int i=0; i<N; i++) {
      for(int j=0; j<i; j++) {
	if(vec[j].first < vec[i].first && vec[j].second < vec[i].second) {
	  dp[i] = max(dp[i], dp[j]+1);
	}
      }
      ans = max(dp[i], ans);
    }
    
    cout << ans+1 << endl;
  }
  
  return 0;
}

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

struct D{int r, h;};

bool ascending_M_r(const D& d1, const D& d2){
  return d1.r < d2.r;
}
bool ascending_M_h(const D& d1, const D& d2){
  return d1.h < d2.h;
}

int main(void){
  int n, m, dp[MAX_NM];
  D a[MAX_NM];

  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 >> a[n+i].h >> a[n+i].r;
    }

    int lis_r = 0, lis_h = 0;
    sort(a, a + n + m, ascending_M_r);

    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      dp[i] = 1;
      for(int j = 0; j < i; j++){
        if(a[j].r < a[i].r && a[j].h && a[i].h){
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
    }
    lis_r = dp[n+m-1];

    sort(a, a + n + m, ascending_M_h);

    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      dp[i] = 1;
      for(int j = 0; j < i; j++){
        if(a[j].r < a[i].r && a[j].h && a[i].h){
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
    }
    lis_h = dp[n+m-1];

    cout << max(lis_h, lis_r) << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>

typedef std::pair<int,int> P;

int dp[200];//何番目のまとりょーしかが一番大きいか＝数

int main(){
	int ichiro,jiro,M=0;
	while(true){
		P doll[200];//高さ　半径
		std::cin>>ichiro;
		if(!ichiro){
			break;
		}
		for(int i=0;i<ichiro;i++){
			std::cin>>doll[i].first>>doll[i].second;
		}
		std::cin>>jiro;
		for(int i=0;i<jiro;i++){
			std::cin>>doll[i+ichiro].first>>doll[i+ichiro].second;
		}
		std::sort(doll,doll+ichiro+jiro);

		for(int i=0;i<ichiro+jiro;i++)dp[i]=1;
		for(int i=1;i<ichiro+jiro;i++){
			for(int j=i-1;j>=0;j--){
				if(doll[i].first!=doll[j].first&&doll[i].second>doll[j].second){
					dp[i]=std::max(dp[i],dp[j]+1);
				}
			}
			M=std::max(M,dp[i]);
		}
		std::cout<<M<<std::endl;
		M=0;
		for(int i=0;i<ichiro+jiro;i++)dp[i]=0;
	}
	return 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;
}

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 dist( N, 1 );

	// ベルマンフォードもどき
	while ( true )
	{
		bool update = false;

		REP( i, 0, N )
		{
			REP( j, 0, G[i].size() )
			{
				if ( dist[ G[i][j] ] < dist[i] + 1 )
				{
					dist[ G[i][j] ] = dist[i] + 1;
					update = true;
				}
			}
		}

		if ( !update )
		{
			break;
		}
	}

	return *max_element( ALL( dist ) );
}

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