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 <string>
#include <map>
#include <algorithm>

#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

int main() {
  while(1){
    int n,m;
    vector<vector<int>> a(1000);
    cin >> n;
    if(!n) break;
    for (int i = 0; i < n; ++i) {
      int h,r;
      cin >> h >> r;
      a[h].emplace_back(r);
    }
    cin >> m;
    for (int i = 0; i < m; ++i) {
      int h,r;
      cin >> h >> r;
      a[h].emplace_back(r);
    }
    vector<vector<int>> dp(1000,vector<int>(1000));
    for (int i = 1; i < 1000; ++i) {
      for (int j = 0; j < 1000; ++j) {
        dp[i][j] = dp[i-1][j];
        for (int r : a[i]) {
          if (r > j) continue;
          dp[i][j] = max(dp[i][j],dp[i-1][r-1]+1);
        }
      }
    }
    cout << dp[999][999] << endl;
  }
  return 0;
}

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

#define pb push_back

struct MTR {
  int h, r;
};

int G[1000][1000];

MTR mtr[200];

int dfs(int s, int k, int N) {
  int ret = 0;
  
  bool last = true;
  for(int i=0; i<N; i++) {
    if(G[s][i]) {
      last = false;
      ret = max(ret, dfs(i, k+1, N));
    }
  }
  if(last) return k;
  
  return ret;
}

int main() {
  
  int n, m;
  while(cin >> n && n) {
    for(int i=0; i<n; i++) {
      cin >> mtr[i].h >> mtr[i].r;
    }
    
    cin >> m;
    for(int i=n; i<n+m; i++) {
      cin >> mtr[i].h >> mtr[i].r;
    }
    
    fill(G[0], G[1000], 0);
    
    int N = n+m;
    for(int i=0; i<N-1; i++) {
      for(int j=0; j<N; j++) {
	if(mtr[i].h < mtr[j].h && mtr[i].r < mtr[j].r) {
	  G[i][j] = 1;
	}
	if(mtr[j].h < mtr[i].h && mtr[j].r < mtr[i].r) {
	  G[j][i] = 1;
	}
      }
    }
    
    int ans = 0;
    
    for(int i=0; i<N; i++) {
      ans = max(ans, dfs(i, 1, N));
    }
    cout << ans << endl;
  }

  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
using namespace std;
#define rep(i, j) for(int i = 0; i < j; i++)
#define f(i, k, j) for(int i = k; i < j; i++)
#define P pair<int, int>
const int INF = (1 << 26);
const int MAX_NM = 201;

int n, m, dp[MAX_NM];
vector<P> mato;

int loop(int i){
    if(dp[i] != 0) return dp[i];
    rep(j, mato.size()) if(mato[i].first > mato[j].first && mato[i].second > mato[j].second) dp[i] = max(dp[i], loop(j) + 1);
    return dp[i];
}


int main(){
    while(cin >>n && n != 0){
        mato.clear();
        rep(i, MAX_NM) dp[i] = 0;

        rep(i, n){
            int a, b; cin >>a >>b; mato.push_back( P(a, b) );
        }
        cin >>m;
        rep(i, m){
            int a, b; cin >>a >>b; mato.push_back( P(a, b) );
        }

        int ans = 0;
        rep(i, mato.size()) ans = max(ans, loop(i));
        cout <<ans + 1 <<endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;
static const int INF = (int)1e8;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) for (int i=0; i<(int)(n); ++i)
#define FOREQ(i,k,n) for (int i=(k); i<=(int)(n); ++i)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define FIND(m,w) ((m).find(w)!=(m).end())
int dfs(int h,int r,int n,vector<PII>& ich,vector<PII>& ji){
  int ret=n;
  REP(i,ich.size()){
    if(ich[i].first<h&&ich[i].second<r){
      ret = max(ret,dfs(ich[i].first,ich[i].second,n+1,ich,ji));
      break;
    }
  }
  REP(i,ji.size()){
    if(ji[i].first<h&&ji[i].second<r){
      ret = max(ret,dfs(ji[i].first,ji[i].second,n+1,ich,ji));
      break;
    }
  }
  return ret;
}
int main(void){
  int n,m;
  while(cin>>n,n){
    vector<PII> ich;
    vector<PII> ji;
    REP(i,n){
      int h,r;
      cin>>h>>r;
      ich.push_back(PII(h,r));
    } 
    cin>>m;
    REP(i,m){
      int h,r;
      cin>>h>>r;
      ji.push_back(PII(h,r));
    }
    reverse(ich.begin(),ich.end());
    reverse(ji.begin(),ji.end());
    cout<<dfs(INF,INF,0,ich,ji)<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
#define MAX_NM 202
 
struct D{int r, h;};
 
bool compr(const D& d1, const D& d2){
  return d1.r < d2.r;
}
bool comph(const D& d1, const D& d2){
  return d1.h < d2.h;
}
 
int main(void){
  int n, m, dp[MAX_NM];
  D d[MAX_NM];
 
  while(cin >> n, n){
    for(int i = 0; i < n; i++) cin >> d[i].h >> d[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(d, d + n + m, compr);
 
    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      dp[i] = 1;
      for(int j = 0; j < i; j++){
        if(d[j].r < d[i].r && d[j].h < d[i].h){
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
    }
    lis_r = dp[n+m-1];
 
    sort(d, d + n + m, comph);
 
    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      dp[i] = 1;
      for(int j = 0; j < i; j++){
        if(d[j].r < d[i].r && d[j].h < d[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 <cstring>
#include <vector>
#include <algorithm>
using namespace std;

typedef pair<int, int> doll;

int main()
{
	int n, m, i, j;
	vector<doll> vc;
	int dp[200];
	
	vc.reserve(200);
	
	while( cin >> n, n)
	{
		vc.clear();
		memset( dp, 0, sizeof(dp));
		
		for( i = 0; i < n; i++)
		{
			doll d;
			cin >> d.first >> d.second;
			vc.push_back(d);
		}
		
		cin >> m;
		
		for( i = 0; i < m; i++)
		{
			doll d;
			cin >> d.first >> d.second;
			vc.push_back(d);
		}
		
		std::sort(vc.begin(), vc.end());
		

		
		dp[0] = 1;
		doll surface(vc[0].first, vc[0].second);

		for( i = 1; i < n+m; i++)
		{
			if( surface.first < vc[i].first && surface.second < vc[i].second )
			{
				dp[i] = dp[i-1] + 1;
				surface.first = vc[i].first;
				surface.second = vc[i].second;
			}
			else
			{
				dp[i] = dp[i-1];
			}
		}
		
		
		cout << dp[n+m-1] << endl;
	}
	
	return 0;
}