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 iota(i,n,b,s) for(int i=int(b);i!=int((b)+(s)*(n));i+=(s))
#define range(i,n,m) iota(i,(((n)>(m))?((n)-(m)):((m)-(n))),(n),((n)>(m)?-1:1))
#define rep(i,n) iota(i,(n),0,1)
#define loop for(;;)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a,b) (make_pair(a,b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;

typedef long long Integer;
typedef double Real;
typedef vector<int> vi;
typedef vector<string> vs;
typedef map<string,int> Dictionary;
const Real PI = acos(-1);

template<class S, class T>
ostream& operator<<(ostream& os, pair<S,T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template<class T>
ostream& operator<<(ostream& os, vector<T> v) {
  if (v.size() == 0) {
    os << "(empty)";
    return os;
  }
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

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

int main() {

more:

  vector<int> hs;
  vector<int> rs;

  {
    int n; cin >> n;
    if (!n) return 0;
    rep (_, n) {
      int h, r; cin >> h >> r;
      hs.push_back(h);
      rs.push_back(r);
    }
  }

  {
    int n; cin >> n;
    rep (_, n) {
      int h, r; cin >> h >> r;
      hs.push_back(h);
      rs.push_back(r);
    }
  }

  const int N = hs.size();
  vector<vector<int>> d(N);

  rep (i, N) {
    rep (j, N) {
      if ((hs[i] < hs[j] && rs[i] < rs[j])) {
        d[i].push_back(j);
      }
    }
  }

  int ans = 0;
  vector<bool>used(N, false);
  rep (i, N) {
    if (used[i]) continue;
    stack<pair<int,int>> s;
    s.push(cons(i, 1));
    while (!s.empty()) {
      auto t = s.top(); s.pop();
      used[car(t)] = true;
      //for (int j : d[car(t)]) {
      //  s.push(cons(j, cdr(t) + 1));
      //}
      ans = max(ans, cdr(t));
    }
  }
  cout << ans << endl;

  goto more;
}

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

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;

		REP( i, 0, n )
		{
			int a, b;
			cin >> a >> b;
			ary.PB( MP( a, b ) );
		}

		int m;
		cin >> m;

		REP( i, 0, m )
		{
			int a, b;
			cin >> a >> b;
			ary.PB( MP( a, b ) );
		}

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

	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <iomanip>
  
using namespace std;

#define pi 3.141592653589793

int main()
{
	int n, m;
	while(1){
		cin >> n;
		if(n == 0) break;
		vector<pair<int, int> > mato(100);
		for(int i = 0; i < n; i++){
			cin >> mato[i].first >> mato[i].second;
		}
		cin >> m;
		for(int i = 0; i < m; i++){
			cin >> mato[n + i].first >> mato[n + i].second;
		}
		sort(mato.begin(), mato.begin() + m + n);
		int cnt = 0;
		int maxh = 0, maxr = 0;
		for(int i = 0; i < n + m; i++){
			// cout << mato[i].first << " " << mato[i].second;
			if(mato[i].first > maxh && mato[i].second > maxr){
				cnt++;
				maxh = mato[i].first;
				maxr = mato[i].second;
			}
			// cout << " " << cnt << endl;
		}
		cout << cnt << endl;
	}
	return 0;
}

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

using namespace std;


// dp[i]ÍiÜÅÌ}g[VJÅÜÞ±ÆÌÅ«éÅåÌ}g[VJ
int dp[201];
int n,m;
vector<pair<int,int> > mt;

int dfs(int idx){
	if(dp[idx]!=-1)
		return dp[idx];
	int maxMat=0;
	for(int i = 0; i < mt.size(); i++){
		if(mt[i].first<mt[idx].first&&mt[i].second<mt[idx].second){
			maxMat=max(maxMat,dfs(i)+1);
		}
	}

	dp[idx]=maxMat;
	return dp[idx];
}

int main(){

	while(cin>>n&&n!=0){
		mt.clear();
		fill(dp,dp+201,-1);
		for(int i = 0; i < n; i++){
			int h,r;
			cin>>h>>r;
			mt.push_back(make_pair(h,r));
		}
		cin>>m;
		for(int i = 0; i < m; i++){
			int h,r;
			cin>>h>>r;
			mt.push_back(make_pair(h,r));
		}
		int maxMat=0;
		for(int i = 0; i < mt.size(); i++){
			maxMat=max(maxMat,dfs(i));
		}
		cout<<maxMat+1<<endl;
	}

	return 0;
}