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 <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cctype>

#include <iostream>
#include <vector>
#include <deque>
#include <queue>
#include <map>
#include <algorithm>

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)
#define repi(i,l,n) for(int (i)=(int)(l);(i)<(int)(n);(i)++)
#define d_arr(arr, h, w) rep(i,(h) ){ cout << "["; rep(j,(w) ) { cout << (arr)[i][j] << ", "; } cout << "]" << endl;}
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x);
#define fs first
#define sc second
#define pb push_back

typedef pair<int ,int> P;
vector<P> mv;
map<int, int> memo;

//
int next_of(int i){
    int h = mv[i].fs;
    int r = mv[i].sc;
    int nh = h;
    int nr = r;
    while(1){
        if( nh > h && nr > r) { break; }
        i++;
        nh = mv[i].fs;
        nr = mv[i].sc;
    }
    return i;
}

// mv[i] 以上で作れるマトリョーシカの数の最大値
int rec(int i){
    //cout << "rec : " << i << endl;
    if( memo.find(i) != memo.end() ){ return memo[i]; }
    if( i > mv.size()-1 ){
        return 0;
    }
    int e0 = rec(i+1);
    int next = next_of(i);
    int e1 = rec(next) + 1;

    memo[i] = max(e0,e1);
    return memo[i];
}

int main()
{
    int n,m;
    int h,r;
    while(1){
        mv.clear();
        memo.clear();
        cin >> n;
        if( n==0 ){ break; }
        rep(i,n){
            cin >> h >> r;
            mv.pb(make_pair(h,r));
        }
        cin >> m;
        rep(i,m){
            cin >> h >> r;
            mv.pb(make_pair(h,r));
        }
        sort(mv.begin(),mv.end());
        cout << rec(0) << endl;
    }
}

C++: #include <cstdio>
#include <cstring>
#define MAX(a,b)((a)>=(b)?(a):(b))
int n,m,h[200],r[200],memo[200];
int solve(int x) {
	if(memo[x]!=-1)return memo[x];
	int max_d=0;
	for(int i=0;i<(n+m);i++) {
		if(h[x]>h[i]&&r[x]>r[i]) {
			max_d=MAX(max_d,1+solve(i));
		}
	}
	return memo[x]=max_d;
}
int main() {
	while(scanf("%d",&n),n) {
		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[i+n],&r[i+n]);
		}
		memset(memo,-1,sizeof(memo));
		int max_d=0;
		for(int i=0;i<(n+m);i++) {
			max_d=MAX(max_d,1+solve(i));
		}
		printf("%d\n",max_d);
	}
}

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

int r[N],h[N];

int syori(int,int);
void sort(int,int);

int main(){
  int n1,n2,i,ans;
 
  while(1){
 
    for(i=1;i<=N;i++){
      r[i]=0;
      h[i]=0;
    }


    r[0]=-1;
    h[0]=-1;
   
    cin >> n1;
    if(n1==0)break;
   
    for(i=1;i<=n1;i++){    // 1©çÍ¶ßÄ¢éÉÓ
      cin >> h[i] >> r[i];
    }
   
    cin >> n2;
   
    for(i=n1+1;i<=n1+n2;i++){ // P©çÍ¶ßÄ¢éÉÓ
      cin >> h[i] >> r[i];
    }
   
    sort(n1,n2);

    ans=syori(n1,n2);
   
    cout << ans << endl;
  }
 
 
}

void sort(int n1,int n2){
  int i,j,max,hi;

  for(i=0;i<=(n1+n2);i++){
    max=i;
    for(j=i;j<(n1+n2);j++){
      if(h[j] < h[max])max=j;
    }
      hi=h[max];
      h[max]=h[i];
      h[i]=hi;
   
         hi=r[max];
      r[max]=r[i];
      r[i]=hi;


  }

}



int syori(int n1,int n2){
  int ANS[N],i,j; //ANS[N]Í·³ð²×ézñ 
  int max=0,res=0;
 
  for(i=0;i<N;i++){
    ANS[i]=0;
    
  }
  
 
  for(i=1;i<=(n1+n2);i++){
    max=0;
    //  cout <<endl<< "h["<<i<<"]--"<<h[i]<<endl;    
    // cout << "r["<<i<<"]--"<<r[i]<<endl;
    // cout << "ANS["<<i<<"]--"<<ANS[i]<<endl;
    // cout<< "Lo["<<i<<"]--"<<Lo[i]<<endl<<endl;


    for(j=0;j<i;j++){
      if(h[j]<h[i] && r[j]<r[i] && ANS[j]>ANS[max]){
    max=j;   
      }
   
    }
 ANS[i]=ANS[max]+1;
      
  }

  //  for(i=1;i<=(n1+n2);i++){
  //  cout << "ANS[" << i << "]--" << ANS[i]<< endl;
  // }


  for(i=1;i<=(n1+n2);i++){
      if(res<=ANS[i]){
    res=ANS[i];
    }
  }
  return res;
}

C++: #include <cstdio>
#include <cstring>
#define MAX(A,B)((A)>=(B)?(A):(B))
int N,M,H[200],R[200],memo[200];
int solve(int x) {
	if(memo[x]>=0) return memo[x];
	int ans=0;
	for(int i=0;i<(N+M);i++) {
		if(H[x]>H[i]&&R[x]>R[i]) {
			ans=MAX(ans,1+solve(i));
		}
	}
	return memo[x]=ans;
}
int main() {
	while(scanf("%d",&N),N) {
		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[i+N],&R[i+N]);
		}
		memset(memo,-1,sizeof(memo));
		int ans=0;
		for(int i=0;i<(N+M);i++) {
			ans=MAX(ans,1+solve(i));
		}
		printf("%d\n",ans);
	}
}

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

int main(){
  int i,j,a,b,n,m,hn[100],rn[100],hm[100],rm[100],h[201],r[201],k[2],L[201],max;
  while(1){
    cin >> n;
    if(n == 0) break;
    for(i=0;i<n;i++) cin >> hn[i] >> rn[i];
    cin >> m;
    for(i=0;i<m;i++) cin >> hm[i] >> rm[i];

    a = 0;
    b = 0;
    h[0] = -1;
    r[0] = -1;
    for(i=1;i<=m+n;i++){
      if(a < n && b < m && hn[a] < hm[b] || a < n && b >= m){
	  h[i] = hn[a];
	  r[i] = rn[a++];
	}
      else{
	h[i] = hm[b];
	r[i] = rm[b++];
      }
    }

      k[0] = 0;
      L[0] = 0;
      for(i=1;i<=m+n;i++){
	max = 0;
	for(j=0;j<i;j++){
	  if(L[j] > max && h[j] < h[i] && r[j] < r[i]) max = L[j];
	}
	L[i] = max + 1;
	if(k[0] < L[i]) k[0] = L[i];
      }

      a = 0;
      b = 0;
    for(i=1;i<=m+n;i++){
      if(a < n && b < m && rn[a] < rm[b] || a < n && b >= m){
	  h[i] = hn[a];
	  r[i] = rn[a++];
	}
      else{
	h[i] = hm[b];
	r[i] = rm[b++];
      }
    }

      k[1] = 0;
      L[0] = 0;
      for(i=1;i<=m+n;i++){
	max = 0;
	for(j=0;j<i;j++){
	  if(L[j] > max && h[j] < h[i] && r[j] < r[i]) max = L[j];
	}
	L[i] = max + 1;
	if(k[1] < L[i]) k[1] = L[i];
      }

      if(k[0] > k[1]) cout << k[0] << endl;
      else cout << k[1] << endl;

  }
  return 0;
}