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<cstdio>
#define N 1000 
using namespace std;
int h1[N],r1[N];

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

int main(){/*main*/
  int i,n1,n2,j,res;

  for(i=0;i<N;i++){
    h1[i]=0;
    r1[i]=0;
  }

  while(1){

    cin >> n1; // êQÌ}g[VJÌ
    
    if(n1 == 0)break;
    
    for(i=0;i<n1;i++){
      cin >> h1[i] >> r1[i]; //(n1-1)ÜÅªêQ
    }
    
    cin >> n2; //ñYÌ}g[VJÌ
    
    for(i=n1;i<(n1+n2);i++){
      cin >> h1[i] >> r1[i]; //n1©ç(n1+n2-1)ÜÅªñY
    }
    
    sort(n1,n2); // ¬³¢ÉÀÑÖ¦
    
    //  for(i=0;i<n1+n2;i++){
    // cout << i << ":"<< h1[i] << " " << r1[i] <<endl;
    // }
    
    res=syori(n1,n2);
    
    
    
    cout << res << endl;
    res=0;
    
   for(i=0;i<N;i++){
    h1[i]=0;
    r1[i]=0;
  }




  }
  
}


void sort(int n1,int n2){
  int k,l,max,h;

  for(k=0;k<(n1+n2);k++){
    max=k;
    for(l=k;l<(n1+n2);l++){
      if(h1[l]<h1[max]){ max=l; }
    }
    h=h1[max];
    h1[max]=h1[k];
    h1[k]=h;
    
    h=r1[max];
    r1[max]=r1[k];
    r1[k]=h;
    
  }
  
  
}


int syori(int n1,int n2){
  int q,i,H[N]/**/,Hcount=0,ans=0,I[N]/**/,HH[N]/**/,count=1/**/,kcount=0,korecount=0;
  
  
  for(q=0;q<N-5;q++){  
    H[q]=0;
    I[q]=0;
    HH[q]=0;
  }
  
  //cout << "test" << endl;
  



  for(q=0;q<(n1+n2);q++){
    
    //  cout <<"q--"<< q << " " <<"count--" << count << endl; 
    //cout << "korecount--" << korecount <<endl;    
     //      

    if(h1[q-korecount]<h1[q+1] && r1[q-korecount]<r1[q+1]){
      count++; 
      korecount=0;
      
    }  


        

    if((h1[q-korecount]==h1[q+1] || r1[q-korecount] == r1[q+1]) && HH[q+1]==0 ){
      
  korecount+=1; //2ñÈãÍ¢éº      
  

      if(korecount>=1){} 
      else{ count++; }
      
    
 
 H[q+1]=count-1;
 HH[q+1]=1;
 Hcount++;
 I[q+1]=q;
 // q+=1;
    }

    }
    
    //  cout << Hcount << endl;
    
    
    if(q==(n1+n2-1) && Hcount!=0){
      Hcount=0;
      kcount=1;
      
      if(count>=ans)ans=count;
      
      // cout << "ans--" << ans << endl; 
      
      for(i=0;i<(n1+n2);i++){
	
	if(HH[i]==1){
	  count=H[i];
	  H[i]=0;
	  HH[i]=0;
	  q=I[i];
	  break;
	}
	
      }
      
      
   
    
    
    
  }
 
  if(kcount==0){ ans=count; } // kcountªOÌÆ«Í¾ÔèÈµ
  else{ kcount=0; }           // ±ÌvOÅÍ¾ÔçÈ¢ÆansªÅÈ¢kokoayasii
  
  
  
  //    for(i=0;i<n1+n2;i++){
  //  cout << i << ":"<< h1[i] << " " << r1[i] <<endl;
  //  }
  
  
  return ans;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 10e-8;

int main(){
	int n;

	while(~scanf("%d",&n)){
		if(n==0) break;
		vector<P> dolls;

		for(int i=0;i<n;i++){
			int h,r;
			scanf("%d %d",&h,&r);
			dolls.push_back(P(h,r));
		}

		int m;
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			int h,r;
			scanf("%d %d",&h,&r);
			dolls.push_back(P(h,r));
		}
		sort(dolls.begin(),dolls.end());

		int ans=1;
		for(int i=0;i<dolls.size();i++){
			for(int j=i+1;j<dolls.size();j++){
				if(dolls[i].first < dolls[j].first
					&& dolls[i].second < dolls[j].second){
						ans++;
						i=j-1;
						break;
				}
			}
		}

		cout << ans << endl;
	}
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstring>
#include<stack>
#include<functional>
using namespace std;

struct Doll{
	int h,r;
	bool operator <(const Doll &a)const{
		return h<a.h;
	}
};

int main(){
	int N,M;
	while(cin>>N,N){
		vector<Doll> d;
		for(int i=0;i<N;i++){
			Doll in; cin>>in.h>>in.r; d.push_back(in);
		}
		cin>>M;
		for(int i=0;i<M;i++){
			Doll in; cin>>in.h>>in.r; d.push_back(in);
		}

		sort(d.begin(),d.end());
		int dp[101][1001] = {}, ans = 0;
		for(int i=0,cnt=0; cnt<d.size(); cnt++){
			if(cnt!=0 && d[cnt-1].h!=d[cnt].h) i++;
			for(int j=0;j<d[cnt].r;j++){
				ans = max(ans, dp[i+1][d[cnt].r] = max(dp[i+1][d[cnt].r], dp[i][j]+1));
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> P;

string str;

int main()
{
  int n,m;
  int i,j;
  int h,r;

  while(1){

    int doll[1005][1005]={};
    int dp[1005][1005]={};

    scanf("%d",&n);
    if(n==0)break;
    rep(i,n){
      scanf("%d %d",&h,&r);
      doll[h][r]=1;
    }

    scanf("%d",&m);
    rep(i,m){
      scanf("%d %d",&h,&r);
      doll[h][r]=1;
    }

    rep(i,1003) rep(j,1003){
      if(i>0 && j>0){
	dp[i][j]=max(dp[i-1][j],dp[i][j-1])+doll[i][j];
	if(doll[i][j]){
	  for(int k=j+1;k<1003;k++)
	    dp[i][k]=dp[i][j];
	  break;
	}
      }
    }

    cout<<dp[1000][1000]<<endl;
  }
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;
int dp[1000][1000];

int main(){
    while(true){
	vector< pair<int,int> > hr;
	int n,m;
	cin >> n;
	if(n == 0) break;
	REP(i,n){
	    int h,r;
	    cin >> h >> r;
	    hr.PB( pair<int,int>(h,r) );
	}
	cin >> m;
	REP(i,m){
	    int h,r;
	    cin >> h >> r;
	    hr.PB( pair<int,int>(h,r) );
	}
	n += m;
	int ans = 0;
	sort(ALL(hr));
	memset(dp,-1,sizeof(dp));
	dp[0][0] = 0;
	REP(i,n){
	    for(int j=0;j<hr[i].first;j++){
		for(int k=0;k<hr[i].second;k++){
		    if(dp[j][k] >= 0)
			dp[hr[i].first][hr[i].second] = max(dp[hr[i].first][hr[i].second], dp[j][k] + 1);
		}
	    }
	}
	REP(i,1000)REP(j,1000) ans = max(ans,dp[i][j]);
	cout << ans << endl;
    }

    return 0;
}