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<cstdio>
#include<vector>
#include<algorithm>
int field1[1001];
int field2[1001];
using namespace std;
int main(void){
    while(1){
        int n; scanf("%d",&n);
        if(n == 0) break;
        vector<pair<int,int> > v1;
        vector<pair<int,int> > v2;
        pair<int,int> p;
        p.first = 0; p.second = 0;
        v1.push_back(p); v2.push_back(p);
        for(int i = 0;i < n;i++){
            int h,r; scanf("%d%d",&h,&r);
            p.first = h; p.second = r;
            v1.push_back(p);
            p.first = r; p.second = h;
            v2.push_back(p);
        }

        scanf("%d",&n);
        for(int i = 0;i < n;i++){
            int h,r; scanf("%d%d",&h,&r);
            p.first = h; p.second = r;
            v1.push_back(p);
            p.first = r; p.second = h;
            v2.push_back(p);
        }

        sort(v1.begin(),v1.end()); sort(v2.begin(),v2.end());
        for(int i = 1;i < v1.size();i++){
            for(int j = i - 1;j >= 0;j--){
                if(v1[i].second > v1[j].second && field1[i] <= field1[j]+1){
                    field1[i] = field1[j]+1;
                }
            }
        }

        for(int i = 1;i < v2.size();i++){
            for(int j = i-1;j >= 0;j--){
                if(v2[i].second > v2[j].second && field2[i] <= field2[j]+1){
                    field2[i] = field2[j]+1;
                }
            }
        }
        int ans = -1;
        for(int i = 0;i < v1.size();i++){
            if(field1[i] >= ans) ans = field1[i];
            if(field2[i] >= ans) ans = field2[i];
        }
        printf("%d\n",ans);
    }
    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int doll[200][2];
int memo[200];
int n,m;

int solve(int p) {
	if(memo[p]) return memo[p];
	memo[p]=1;
	for(int i=0;i<n+m;i++) {
		if(doll[p][0]>doll[i][0]&&doll[p][1]>doll[i][1]) memo[p]=max(1+solve(i),memo[p]);
	}
	return memo[p];
}

int main() {
	while(scanf("%d",&n),n) {
		for(int i=0;i<n;i++) {
			scanf("%d %d",&doll[i][0],&doll[i][1]);
		}
		scanf("%d",&m);
		for(int i=n;i<n+m;i++) {
			scanf("%d %d",&doll[i][0],&doll[i][1]);
		}
		int ans=0;
		memset(memo,0,sizeof(memo));
		for(int i=0;i<n+m;i++) {
			ans=max(ans,solve(i));
		}
		printf("%d\n",ans);
	}
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;
int n,m;
static int h[200], r[200];
static int dp[200];
int calc(int i) {if(dp[i] == -1) {
dp[i] = 1;
for(int j = 0; j < n+m; j++) {
if(h[j] < h[i] && r[j] < r[i]) {
dp[i] = max(dp[i], calc(j)+1);
}
}
}
return dp[i];
}
int main() {
for(int tci = 0; ; tci++) {
scanf("%d", &n); if(n==0) break;
for(int i = 0; i < n; i++) {
scanf("%d%d", h+i, r+i);
}
scanf("%d", &m);
for(int i = 0; i < m; i++) {
scanf("%d%d", h+n+i, r+n+i);
}
fill(dp, dp+n+m, -1);
int mv = 0;
for(int i = 0; i < n+m; i++) {
mv = max(mv, calc(i));
}
printf("%d\n", mv);
}
return 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();
  int d[N][N];

  rep (i, N) {
    rep (j, N) {
      if ((hs[i] < hs[j] && rs[i] < rs[j])) {
        d[i][j] = 1;
      } else {
        d[i][j] = 0;
      }
    }
  }

  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;
      rep (j, N) {
        if (d[car(t)][j]) s.push(cons(j, cdr(t) + 1));
      }
      ans = max(ans, cdr(t));
    }
  }
  cout << ans << endl;

  goto more;
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
 
int dp[1010][1010];
int n, m, h, r;
 
signed main(){
    while (cin >> n && n) {
        rep(i, 0, 1010) {
            rep(j, 0, 1010) {
                dp[i][j] = 0;
            }
        }
        vector<pii> doll;
        rep(i, 0, n) {
            cin >> h >> r;
            doll.push_back(make_pair(h, r));
        }
        cin >> m;
        rep(i, 0, m) {
            cin >> h >> r;
            doll.push_back(make_pair(h, r));
        }
        sort(all(doll));
        rep(i, 0, n + m + 1) {
            rep(j, 1, 1010) {
                if (doll[i].se == j) {
                    dp[i + 1][j] = dp[i][j - 1] + 1;
                }
                else {
                    dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - 1]);
                }
            }
        }
        cout << dp[n + m][1001] << endl;
    }
}