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<algorithm>
#include<math.h>
#include<stdlib.h>
#include<cstdlib>
using namespace std;

struct ppp {
  int h;
  int r;
};

int tansaku(vector<ppp> c, vector<ppp> d, int cN, int dN, int ans, int cy, int dy) {
  if(dN >= d.size()) {
    for(int i = cN; i < c.size(); i++) {
      if(d[dy].r < c[i].r && d[dy].h < c[i].h) {
        ans++;
        d[dy] = c[i];
      }
    }
    return ans;
  }
  if(cN >= c.size()) {
    for(int i = dN; i < d.size(); i++) {
      if(d[i].r > c[cy].r && d[i].h > c[cy].h) {
        ans++;
        c[cy] = d[i];
      }
    }
    return ans;
  }
  if(c[cN].h == d[dN].h) {
    return c[cN].r > d[dN].r ? tansaku(c, d, cN + 1, dN + 1, ans + 1, cy, dN) :
    tansaku(c, d, cN + 1, dN + 1, ans + 1, cN, dy);
  }
  if(c[cN].r == d[dN].r) {
    return c[cN].h > d[dN].h ? tansaku(c, d, cN + 1, dN + 1, ans + 1, cy, dN) :
    tansaku(c, d, cN + 1, dN + 1, ans + 1, cN, dy);
  }
  if(c[cN].h < d[dN].h && c[cN].r < d[dN].r) {
    return tansaku(c, d, cN + 1, dN, ans + 1, cN, dy);
  }
  if(c[cN].h > d[dN].h && c[cN].r > d[dN].r) {
    return tansaku(c, d, cN, dN + 1, ans + 1, cy, dN);
  }
  if((c[cN].h < d[dN].h && c[cN].r > d[dN].r) || (c[cN].h > d[dN].h && c[cN].r < d[dN].r)) {
    return max(tansaku(c, d, cN + 1, dN, ans + 1, cN, dy), tansaku(c, d, cN, dN + 1, ans + 1, cy, dN));
  }
  return ans;
}

int main() {
  int a, b;
  while(cin >> a) {
    if(a == 0) break;
    vector<ppp> c(a);
    for(int i = 0; i < a; i++) {
      cin >> c[i].h >> c[i].r;
    }
    cin >> b;
    vector<ppp> d(b);
    for(int i = 0; i < b; i++) {
      cin >> d[i].h >> d[i].r;
    }
    cout << tansaku(c, d, 0, 0, 0, 0, 0) << endl;
  }
}

C++: #include <stdio.h>
#define max(a,b) ((a)>(b) ? (a) : (b))

main(){
    int n,m,N;
    int i,j;
    int h,r;
    int map[1000][1000]={{0,},};
    while(scanf("%d",&n)==1){
        for(i=0;i<1000;i++)
            for(j=0;j<1000;j++)
                map[i][j]=0;
        if(!n)break;
        for(i=0;i<n;i++){
            scanf("%d %d",&h,&r);
            map[h][r]=1;
        }
        scanf("%d",&m);
        for(;i<n+m;i++){
            scanf("%d %d",&h,&r);
            map[h][r]=1;
        }
        for(i=1;i<1000;i++){
            for(j=1;j<1000;j++){
                map[i][j]=map[i][j]+max(map[i-1][j],map[i][j-1]);
            }
        }
        printf("%d\n",map[999][999]-1);
    }
    return 0;
}

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

typedef pair<int, int> Doll;
vector<Doll> Dolls;
int dp[201];

void debug(){
  for(int i = 0; i < Dolls.size(); i++){
    cout << "(" << Dolls[i].first << ", " << Dolls[i].second << ")" << endl;
  }
}

bool comp(const Doll& d1, const Doll& d2){
  return ((d1.first <= d2.first) && (d1.second <= d2.second));
}

int main(void){
  int n, m;
  while(cin >> n, n){
    Dolls.clear();
    Dolls.push_back(Doll(0, 0));
    for(int i = 0; i < n; i++){
      int h, r;
      cin >> h >> r;
      Dolls.push_back(Doll(h, r));
    }
    cin >> m;
    for(int i = 0; i < m; i++){
      int h, r;
      cin >> h >> r;
      Dolls.push_back(Doll(h, r));
    }
    sort(Dolls.begin(), Dolls.end(), comp);
    for(int i = 1; i < Dolls.size(); i++){
      int max_k = 0;
      for(int j = 0; j < i; j++){
        if(Dolls[j].first < Dolls[i].first && Dolls[j].second < Dolls[i].second && dp[max_k] < dp[j]){
          max_k = j;
        }
      }
      dp[i] = dp[max_k] + 1;
    }
    cout << dp[Dolls.size()-1] + 1 << endl;
    fill(dp, dp + 200, 0);
  }
  return 0;
}

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

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

void solve()
{
	int n;
	while(cin >> n, n)
	{
		map<int, int> data;
		for(int i = 0; i < n; ++i)
		{
			int h, r;
			cin >> h >> r;
			data.insert(map<int, int>::value_type(h, r));
		}
		int m;
		cin >> m;
		for(int i = 0; i < m; ++i)
		{
			int h, r;
			cin >> h >> r;
			data.insert(map<int, int>::value_type(h, r));
		}
		vector<P> doll;
		map<int, int>::reverse_iterator rit = data.rbegin();
		while(rit != data.rend())
		{
			doll.push_back(P(rit->first, rit->second));
			++rit;
		}
		int max_doll_count = 0;
		for(int i = 0; i < doll.size(); ++i)
		{
			int doll_count = 1;
			P current = doll[i];
			for(int j = i + 1; j < doll.size(); ++j)
			{
				if(current.first > doll[j].first && current.second > doll[j].second)
				{
					++doll_count;
					current = doll[j];
				}
			}
			if(max_doll_count < doll_count)
			{
				max_doll_count = doll_count;
			}
		}
		
		vector<P> doll2;
		map<int, int>::reverse_iterator rit2 = data.rbegin();
		while(rit2 != data.rend())
		{
			doll2.push_back(P(rit2->second, rit2->first));
			++rit2;
		}
		for(int i = 0; i < doll2.size(); ++i)
		{
			int doll_count = 1;
			P current = doll2[i];
			for(int j = i + 1; j < doll2.size(); ++j)
			{
				if(current.first > doll2[j].first && current.second > doll2[j].second)
				{
					++doll_count;
					current = doll2[j];
				}
			}
			if(max_doll_count < doll_count)
			{
				max_doll_count = doll_count;
			}
		}
		cout << max_doll_count << endl;
	}
}

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

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int,int> MAT;
#define h first
#define r second
#define rep(i,n) for(int i=0;i<n;i++)
int dp[1000][1000];
MAT info[200];
int n,m;

int dfs(int a,int b){
	if(~dp[a][b])return dp[a][b];
	
	int hoge = 0;
	rep(i,n){
		if(info[i].first < a && info[i].second < b){
			hoge = max(hoge, dfs(info[i].first,info[i].second)+1);
		}
	}
	return hoge;
}
int main(){
	while(cin >> n , n ){
		rep(i,1000)rep(j,1000)dp[i][j] = -1;
		
		int ret = 0;
		rep(i,n)cin >> info[i].h >> info[i].r;
		cin >> m;
		rep(i,m)cin >> info[n+i].h >> info[n+i].r;
		
		n = n + m;
		
		rep(i,n){
			ret = max(ret, dfs(info[i].first , info[i].second)+1);
		}
		cout << ret << endl;
	}
}

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

int main(){
	while(1){
		bool dp[NUM][NUM] = {false};
		int n, m, h, r, ans = 0;
		cin >>n;
		if(n == 0) break;
		for(int i = 0; i < n; i++){
			cin >>h >>r;
			dp[h][r] = true;
		}
		cin >>m;
		for(int i = 0; i < m; i++){
			cin >>h >>r;
			dp[h][r] = true;
		}
		int cnt = 1;
		for(int i = cnt; i < NUM; i++){
			for(int j = cnt; j < NUM; j++){
				if(dp[i][j] == true){
					ans++;
					for(int k = (i + 1); k < NUM; k++) dp[k][j] = false;
					break;
				}
				else if(dp[j][i] == true){
					ans++;
					for(int k = (i + 1); k < NUM; k++) dp[j][k] = false;
					break;
				}
			}
			cnt++;
		}
		cout <<ans <<endl;
	}
	return 0;
}