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>
#define mk make_pair
#define f first
#define s second
using namespace std;
int n,m,p1,p2,dp[201][1001][1001];

vector<pair<int,int> >v;

int dfs(int d,int h,int w){
  int res=0;
  if(dp[d][h][w])return dp[d][h][w];
  if(d==v.size()) return 1;
  if(v[d].f<h&&v[d].s<w)
    res=max(res,dfs(d+1,v[d].f,v[d].s))+1;
  res=max(res,dfs(d+1,h,w));
  return dp[d][h][w]=res;
}

main(){
  while(cin>>n,n){
    memset(dp,0,sizeof(dp));
    v.clear();
    for(int i=0;i<n;i++){
      cin>>p1>>p2;
      v.push_back(mk(p2,p1));
    }
    cin>>m;
    for(int i=0;i<m;i++){
      cin>>p1>>p2;
      v.push_back(mk(p2,p1));
    }
    stable_sort(v.begin(),v.end(),greater<pair<int,int> >());
    for(int i=0;i<v.size();i++)swap(v[i].f,v[i].s);
    stable_sort(v.begin(),v.end(),greater<pair<int,int> >());
    cout<<dfs(0,1001,1001)-1<<endl;
  }
}

C++: #include<algorithm>
#include<stdio.h>
using namespace std;
pair<int,int> dat[1000];
int dp[1000];
int main(){
	int a;
	while(scanf("%d",&a),a){
		for(int i=0;i<1000;i++)dp[i]=0;
		int n=0;
		for(int i=0;i<a;i++){
			int b,c;
			scanf("%d%d",&b,&c);
			dat[n++]=make_pair(b,c);
		}
		scanf("%d",&a);
		for(int i=0;i<a;i++){
			int b,c;
			scanf("%d%d",&b,&c);
			dat[n++]=make_pair(b,c);
		}
		std::sort(dat,dat+n);
		for(int i=0;i<n;i++){
			dp[i]=1;
			for(int j=0;j<i;j++){
				if(dat[j].first<dat[i].first&&dat[j].second<dat[i].second)dp[i]=max(dp[i],dp[j]+1);
			}
		}
		int ret=0;
		for(int i=0;i<n;i++)ret=max(ret,dp[i]);printf("%d\n",ret);
	}
}

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

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});
    }
    ll ans = 1,tmp = 1;
    h = v[0].h,r = v[0].r;
    sort(all(v),[](const data &a,const data &b){
        return a.s < b.s;});
    ll c = 0,mi = inf;
    while(1){
      mi = inf;
      rep(i,n+m){
        if(v[i].h > h && v[i].r > r && mi > v[i].h -h + v[i].r - r){
          h = v[i].h;
          r = v[i].r;
          c = i;
          mi = v[i].h - h +v[i].r - r;
        }
      }
      if(mi != inf){
        ++ans;
      }
      else
        break;
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
#include <list>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define F first
#define S second


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

typedef struct{
  int h,r;
}mat;

bool lessh(const mat&l,const mat&r){return l.h<r.h;};
bool lessr(const mat&l,const mat&r){return l.r<r.r;};


int low[200],ans;
mat hr[200];

main(){
  int n;
  while(cin>>n,n){
    rep(i,n)cin>>hr[i].h>>hr[i].r;
    int m;
    cin>>m;
    rep(i,m)cin>>hr[i+n].h>>hr[i+n].r;

    
    memset(low,0,sizeof(0));

    stable_sort(hr,hr+m+n,lessh);
    stable_sort(hr,hr+m+n,lessr);

    int ans=0;
    rep(i,n+m){
      rep(j,i){
	if(hr[j].h<hr[i].h && hr[j].r<hr[i].r){
	  low[i]=max(low[i],low[j]+1);
	  ans=max(ans,low[i]);
	}
      }
    }

    cout<<ans+1<<endl;
  }
}

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

int N;

int m[200][2];
vector<pair<pair<int, int>, int>> m1, m2;
vector<int> pa[200];

int dfs(int x)
{
  int r = 0;
  for(auto v: pa[x])
    r = max(r, dfs(v));
  return r + 1;
}

int main()
{
  while(true)
  {
    for(int i = 0; i < 200; i++)
      pa[i].clear(), (m[i][0] = m[i][1] = 0);
    m1.clear(); m2.clear();

    N = 0;
    for(int i = 0; i < 2; i++)
    {
      int a;
      cin >> a;
      if(!a)
        return 0;
      for(int j = N; j < a + N; j++)
      {
        cin >> m[j][0] >> m[j][1];
        m1.push_back(make_pair(make_pair(m[j][0], m[j][1]), j));
        m2.push_back(make_pair(make_pair(m[j][1], m[j][0]), j));
      } 
      N += a;
    }
    sort(m1.begin(), m1.end());
    sort(m2.begin(), m2.end());
    for(int i = 0; i < N; i++)
    {
      bool b1 = false, b2 = false;
      for(int j = i + 1; j < N; j++)
      {
        if(!b1 && m1[j].first.second > m1[i].first.second)
          pa[m1[i].second].push_back(m1[j].second), b1 = true;
        if(!b2 && m2[j].first.second > m2[i].first.second)
          pa[m2[i].second].push_back(m2[j].second), b1 = true;
      }
    }
    cout << max(dfs(m1[0].second), dfs(m2[0].second)) - 1 << endl;;
  }
}