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 <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <cstring>
#include <limits>
#include <cmath>
#include <cfloat>

#define FOR(i,b,n) for(int i=b;i<n;i++)
#define CLR(mat) memset(mat, 0, sizeof(mat))

using namespace std;
typedef pair<int, int> doll;

int n, m;
vector<doll> vc;

int solve_rec(int d, int dp[])
{
	int res = 1;
	
	if( dp[d] != 0 )
	{
		return dp[d];
	}
	else
	{
		FOR(i, 0, n+m)
		{
			if( vc[i].first < vc[d].first && vc[i].second < vc[d].second )
			{
				res = max(res, solve_rec(i, dp)+1);
			}
		}
		
		dp[d] = res;
		return res;
	}
}

int solve()
{
	int res = 1;
	int dp[1000] = {0};

	FOR(i, 0, n+m)
	{
		res = max(res, solve_rec(i, dp));
	}
	
	return res;
}
int main()
{
	while(cin >> n, n)
	{
		int res = 1;
		vc.clear();

		FOR(i, 0, n)
		{
			doll d;
			cin >> d.first >> d.second;
			
			vc.push_back(d);
		}
		
		cin >> m;
		
		FOR(i, 0, m)
		{
			doll d;
			cin >> d.first >> d.second;
			
			vc.push_back(d);
		}
		
		cout << solve() << endl;
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init));

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;
  while(cin >> n && n){
    vector<pll> v;
    ll h,r;
    rep(i,n){
      cin >> h >> r;
      v.push_back(make_pair(h,r));
    }
    cin >> m;
    rep(i,m){
      cin >> h >> r;
      v.push_back(make_pair(h,r));
    }
    ll ans = 1,tmp = 1;
    h = v[0].fi,r = v[0].se;
    sort(all(v));
    rep(i,n+m){
      if(v[i].fi > h && v[i].se > r){
        h = v[i].fi;
        r = v[i].se;
        ++ans;
      }
    }
    sort(all(v),[](const pll& a,const pll &b){return a.se < b.se;});
    h = v[0].fi,r = v[0].se;
    rep(i,n+m){
      if(v[i].fi > h && v[i].se > r){
        h = v[i].fi;
        r = v[i].se;
        ++tmp;
      }
    }
    ans = max(ans,tmp);
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>

using namespace std;

int n, m;
int d[200][2], dp[200];

int main(){
    for(;;){
        if(cin >> n){
            for(int i = 0; i < 200; i++){
                dp[i] = 0;
            }
            if(n == 0){
                break;
            }
            for(int i = 0; i < n; i++){
                cin >> d[i][0] >> d[i][1];
                for(int j = i; j > 0; j--){
                    if(d[j][0] > d[j - 1][0]){
                        break;
                    }else{
                        int a = d[j][0];
                        int b = d[j][1];
                        d[j][0] = d[j - 1][0];
                        d[j][1] = d[j - 1][1];
                        d[j - 1][0] = a;
                        d[j - 1][1] = b;
                    }
                }
            }
            cin >> m;
            for(int i = n; i < n + m; i++){
                cin >> d[i][0] >> d[i][1];
                for(int j = i; j > 0; j--){
                    if(d[j][0] > d[j - 1][0]){
                        break;
                    }else{
                        int a = d[j][0];
                        int b = d[j][1];
                        d[j][0] = d[j - 1][0];
                        d[j][1] = d[j - 1][1];
                        d[j - 1][0] = a;
                        d[j - 1][1] = b;
                    }
                }
            }
            int maax = 0;
            for(int i = 0; i < n + m; i++){
                dp[i]++;
                int a = 0;
                for(int j = 0; j < i; j++){
                    if(a <= dp[j]){
                        if(d[j][1] < d[i][1]){
                            a = dp[j];
                        }
                    }
                }
                dp[i] += a;
                if(maax <= dp[i]){
                    maax = dp[i];
                }
            }
            cout << maax << endl;
        }else{
            break;
        }
    }
    return 0;
}