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

struct data{
  ll h,r,s;
  bool operator<(const data& d){
    return s < d.s;
  }
};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;
  while(cin >> n && n){
    vector<data> v;
    ll h,r;
    rep(i,n){
      cin >> h >> r;
      v.push_back({h,r,h+r});
    }
    cin >> m;
    rep(i,m){
      cin >> h >> r;
      v.push_back({h,r,h+r});
    }
    sort(all(v));
    ll ans = 0,c = 0;
    vvl(dp,1002,1002,0);
    REP(i,0,1001){
      REP(j,0,1001){
        if(c >= n+m) break;
        if(v[c].h <= i && v[c].r <= j){
          dp[i+1][j+1] = max(dp[i+1][j+1],dp[i][j]+1);
          ++c;
        }
        else{
          dp[i+1][j+1] = max(dp[i+1][j+1],max(dp[i+1][j],max(dp[i][j],dp[i][j+1])));
        }
      }
    }
    rep(i,1002){
      rep(j,1002){
        ans = max(ans,dp[i][j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

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

void solve()
{
	int n;
	while(cin >> n, n)
	{
		vector<P> doll;
		for(int i = 0; i < n; ++i)
		{
			int h, r;
			cin >> h >> r;
			doll.push_back(P(h, r));
		}
		int m;
		cin >> m;
		for(int i = 0; i < m; ++i)
		{
			int h, r;
			cin >> h >> r;
			doll.push_back(P(h, r));
		}
		sort(doll.begin(), doll.end());
		int N = doll.size();
		vector<int> dp(N, 1);
		int ans = 1;
		for(int i = 0; i < N; ++i)
		{
			for(int j = 0; j < i; ++j)
			{
				if(doll[j].first < doll[i].first && doll[j].second < doll[i].second)
				{
					dp[i] = max(dp[i], dp[j] + 1);
				}
			}
			ans = max(ans, dp[i]);
		}
		cout << ans << endl;
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include<stdio.h>
#include<list>
#include<utility>
#include<algorithm>
using namespace std;


typedef list<int> OuterList;
struct Doll
{
	int outerCount;	//±êðüêçêél`ÌÂ
	int h, r;	//å«³
	OuterList outerList;	//±êðüêçêél`ÌQÆæÌCfbNX
};
const int N = 200;
//ärÖÌ¹¢Å±±É@ÝvÔá¦½H
Doll doll[N];

//åÌå«¢ÉÀÑÖ¦éæ¤
bool SortDoll(const Doll &lhv, const Doll &rhv)
{
	return (lhv.h+lhv.r) > (rhv.h+rhv.r);
}
//êÔO¤Ìl`ª½¢ÌðÔ·
//max_elementÉn·àÌÍAoperator<ãíèÌàÌ
bool CmpOuterDoll(int lhv, int rhv)
{
	return doll[lhv].outerCount < doll[rhv].outerCount;
}

int main()
{
	int n,m,i,j,dollCount;
	const int INVALID_VALUE = -1;

	while(scanf("%d",&n),n)
	{
		for(i = 0; i < n; ++i)
			scanf("%d%d",&doll[i].h, &doll[i].r);
		scanf("%d",&m);
		for(i = 0; i < m; ++i)
			scanf("%d%d",&doll[i+n].h, &doll[i+n].r);

		dollCount = n+m;
		//åÌå«¢ÉÀÑÖ¦é
		sort(doll, doll+dollCount, SortDoll);

		// él`ðàïÅ«él`Ìñ
		for(i=0; i < dollCount; ++i)
		{
			doll[i].outerCount = INVALID_VALUE;	//Ü¾mèµÄ¢Ü¹ñæ
			doll[i].outerList.clear();	//Á¦È³¢æ
			for(j = 0; j < dollCount; ++j)
			{
				//üèÜ·H
				if(doll[i].h < doll[j].h &&
					doll[i].r < doll[j].r)
				{
					doll[i].outerList.push_back(j);
				}
			}
		}

		int finishCount = 0;
		int maxDoll = 0;
		//Sl`ªðI¦éÜÅ[v
		while(finishCount < dollCount)
		{
			for(i = 0; i < dollCount; ++i)
			{
				//Ü¾»Ìl`ÌªIíÁÄ¢È¢Èç
				if(doll[i].outerCount == INVALID_VALUE)
				{
					bool f = true;	//SÄÌO¤ÌzªðI¦Ä¢é©
					for(OuterList::iterator ite = doll[i].outerList.begin();
						ite != doll[i].outerList.end(); ++ite)
					{
						if(doll[*ite].outerCount == INVALID_VALUE)
						{
							f = false;
							break;
						}
					}
					//SÄIíÁÄ¢éÈçA él`ÍA»Ìl`ðÍ¦él`ÌÂ+1@ÂÉÈèÜ·
					if(f)
					{
						if(doll[i].outerList.empty())
						{
							//½à»êðÍ¤±ÆªoÈ¢l`ÈÌÅ·
							doll[i].outerCount = 1;
						}
						else
						{
							//±êð©ÄAY¦ðÇµ½±Æðã÷·éÌÅµ½
							doll[i].outerCount = doll[
								*max_element(
									doll[i].outerList.begin(), doll[i].outerList.end(), CmpOuterDoll
								)
								].outerCount + 1;
						}
						maxDoll = max(maxDoll, doll[i].outerCount);
						finishCount++;	//Ü½êÂmèµÜµ½
					}
				}
			}
		}

		printf("%d\n", maxDoll);
	}
	return 0;
}

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

int main(){
	int n;
	int m;
	int h[500];
	int r[500];
	int dp[500];
	for(;;){
		cin >> n;
		if(n==0) break;
		for(int i=0; i<n; i++){
			cin >> h[i] >> r[i];
		}
		cin >> m;
		for(int i=0; i<m; i++){
			cin >> h[i+n] >> r[i+n];
		}

		for(int i=(n+m)-1; i>=0; i--){
			for(int j=0; j<i; j++){
				if(h[j]>h[j+1]){
					int tmp = h[j];
					h[j] = h[j+1];
					h[j+1] = tmp;

					tmp = r[j];
					r[j] = r[j+1];
					r[j+1] = tmp;
				}
			}
		}

//		for(int i=0; i<(n+m); i++){
//			cout << h[i] << " " << r[i] << endl;
//		}

		for(int i=0; i<(n+m); i++){
			dp[i] = 1;
		}

		for(int i=0; i<(n+m); i++){
			for(int j=0; j<i; j++){
				if(r[j]<r[i] && h[j]<h[i]){
					if(dp[i]<dp[j]+1){
						dp[i] = dp[j]+1;
					}
				}
			}
		}

		int mx = 0;
		for(int i=0; i<(n+m); i++){
			if(mx<dp[i]) mx = dp[i];
		}
		cout << mx << endl;

	}

}