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>
using namespace std;

class Container
{
public:
	void append( pair< int, int > p )
	{
		masterList.push_back( p );
	}
	void cSort()
	{
		sort( masterList.begin(), masterList.end() );
		sortedList.clear();
		vector< pair< int, int > >::iterator itr = masterList.begin();
		while( itr != masterList.end() ){
			vector< pair< int, int > > v;
			int x = itr->first;
			v.push_back( *itr );
			++itr;
			if( itr != masterList.end() && itr->first == x ){
				v.push_back( *itr );
				++itr;
			}
			sortedList.push_back( v );
		}
	}

	int countContainment()
	{
		int result = 0;
		for( int k = 0; k < (int)sortedList.size(); ++k ){
			vector< pair< int, int > >& f = sortedList[ k ];
			int y = f[ 0 ].second;
			int count = 1;
			for( int i = k + 1; i < (int)sortedList.size(); ++i ){
				vector< pair< int, int > >& v = sortedList[ i ];
				for( int j = 0; j < (int)v.size(); ++j ){
					if( v[ j ].second > y ){
						y = v[ j ].second;
						++count;
						break;
					}
				}
			}
			result = max( result, count );
		}
		return result;
	}

	void cSwap()
	{
		for( int i = 0; i < (int)masterList.size(); ++i ){
			int t = masterList[ i ].first;
			masterList[ i ].first = masterList[ i ].second;
			masterList[ i ].second = t;
		}
	}


	vector< pair< int, int > > masterList;
	vector< vector< pair< int ,int > > > sortedList;
};

int main()
{
	int n, m;

	while( cin >> n && n ){
		Container cont;
		for( int i = 0; i < n; ++i ){
			pair< int, int > p;
			cin >> p.first >> p.second;
			cont.append( p );
		}
		cin >> m;
		for( int i = 0; i < m; ++i ){
			pair< int, int > p;
			cin >> p.first >> p.second;
			cont.append( p );
		}

		int result = 0;
		cont.cSort();
		result = cont.countContainment();
		cont.cSwap();
		cont.cSort();
		cout << max( result, cont.countContainment() ) << endl;
	}
	return 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;
}

//ðÔáÁÄ¢Ü¹ñ©H FE( while(i=5)  CR[ª«èÈ¢)

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];
map<pair<int,pair<int,int> >,int >m1;

vector<pair<int,int> >v;

int dfs(int d,int h,int w){
  int res=0;
  if(m1[mk(d,mk(h,w))])return m1[mk(d,mk(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 m1[mk(d,mk(h,w))]=res;
}

main(){
  while(cin>>n,n){
    m1.clear();
    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<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define f first
#define s second
#define pb(a,b) push_back(make_pair(a,b))
#define g(a,b) for(int i=a;i<b;i++)
int main(){
    int n;
    while(cin>>n,n){
        vector<pair<int,int> >v;
        int m,x,y,x1,y1,x2,y2,a=1,c=1,b=101;
        g(0,n){
            cin>>x>>y;
            v.pb(x, y);
        }
        cin>>m;
        g(0,m){
            cin>>x>>y;
            v.pb(x, y);
        }
        x1=v[0].f,y1=v[0].s,x2=v[n].f,y2=v[n].s;
        sort(v.begin(),v.end());
        x=x1,y=y1;
        g(1,m+n){
            if(v[i].s>y&&v[i].f>x){
                a++;
                x=v[i].f,y=v[i].s;
            }
            else
                b=min(b,i);
        }
        x=x2,y=y2;
        g(1,m+n){
            if(v[i].s>y&&v[i].f>x){
                c++;
                x=v[i].f,y=v[i].s;
            }
        }
        cout<<max(a,c)<<endl;
    }
}