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>

using namespace std;

int main(){/*main*/
  int i,j,k,n,h[201],hn2,hen,r[201],m,hn,count,ans[201],min,max;

  while(1)
    {/*while*/
      max=0;
      hen=0;
      count=0;
      h[0]=-1;
      r[0]=-1;
      for(i=0;i<201;i++)
    {
      ans[i]=0;
    }
      ans[0]=0;

      cin >> n ;
      if(n==0)break;
      for(i=1;i<=n;i++)
    {
      cin >> h[i] >> r[i];
    }

      cin >> m;
      for(i=n+1;i<=(m+n);i++)
    {
      cin >> h[i] >> r[i];
    }

      /*
      for(i=1;i<(n+m);i++)
      {
      printf("TES_h%d %d  TES_r%d %d\n",i,h[i],i,r[i]);
      }
      */
     


      for(i=1;i<=(m+n);i++)
    {/*for1*/
      min=i;
      for(j=i;j<=(m+n);j++)
        {/*for2*/
          if(h[j]<h[min])
        {
          min=j;
        }
        }/*E_for2*/
      hn=h[min];
      h[min]=h[i];
      h[i]=hn;

      hn2=r[min];
      r[min]=r[i];
      r[i]=hn2;
    }/*E_for1*/

 
      //    for(i=1;i<=(n+m);i++)
      // {
      //	printf("TES_h[%d] %d  TES_r[%d] %d\n",i,h[i],i,r[i]);
      // }
     

       /*   for(i=1;i<=(m+n);i++)
    {
      if(h[i]>h[i-1] && r[i]>r[i-1])
        {
          count+=1;
          ans[i]=1;
        }
      else
        {
          ans[i-1]=0;
          for(j=1;j<=i;j++)
        {
          if((h[i]>h[j] && r[i]>r[j]) && ans[j]==0 &&(h[i]!=h[i-1] && r[i]!=r[i-1]))
            {
              hen++;
            }

        }
          if(hen>0)count++;
        }
*/


       for(i=1;i<=(m+n);i++)
	 {
	   k=0;
	   for(j=1;j<i;j++)
	     {
	       if(r[i]>r[j] && j>k && ans[j]>ans[k])
		 {
		   k=j;
		 }
	       // cout << "TES_max"<< max << endl;
	     }
	   ans[i]=ans[k]+1;
	 }
       
       max=-1;
       for(i=1; i<=n+m; i++)
	 {
	   if(max<ans[i])
	     {
	       max=ans[i];
	     }
	 }

   
       
       
       cout << max << endl;
       
       
    }/*E_while*/
  return 0;
}/*E_main*/

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

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair <INT, INT> II;
typedef vector <II> VII;

template<class T> ostream& operator << ( ostream& os, vector<T> v ) {
    for ( typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i ) {
        os << *it_i << ", ";
    }
    return os;
}


const int SIZE = 101;
const int NONE = -1;

int H[SIZE];
int R[SIZE];
int N;
int dp[1002][1002];

int solve( int hh, int rr ) {
    int& res = dp[hh][rr];
    if ( res != NONE ) return res;
    int maxd = 0;
    for ( int i = 0; i < N; ++ i ) {
        if ( H[i] < hh && R[i] < rr ) {
            maxd = max( maxd, 1 + solve( H[i], R[i] ) );
        }
    }
    return res = maxd;
}

int main() {
    int n, m;
    while ( cin >> n && n ) {
        for ( int i = 0; i < 1002; ++ i ) {
            for ( int j = 0; j < 1002; ++ j ) {
                dp[i][j] = NONE;
            }
        }
        for ( int i = 0; i < n; ++ i ) {
            cin >> H[i] >> R[i];
        }
        cin >> m;
        for ( int i = n; i < n+m; ++ i ) {
            cin >> H[i] >> R[i];
        }
        N = n + m;
        cout << solve( 1001, 1001 ) << endl;
    }
    return 0;
}

C++: // 基本テンプレート
 
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
 
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;
 
signed main() {
    int N, M;
    while(cin >> N, N) {
        vector< pair<int, int> > box;
        for(int i=0; i<N; i++) {
            int A, B; cin >> A >> B;
            box.emplace_back(A, B);
        }
        cin >> M;
        for(int i=0; i<M; i++) {
            int A, B; cin >> A >> B;
            box.emplace_back(A, B);
        }
        sort(box.begin(), box.end());

        N += M;
        vector<int> dp(N, 0);
        for(int i=0; i<N; i++) {
            dp[i] = 1;
            for(int j=0; j<i; j++) {
                bool ok = true;
                ok &= (box[j].first < box[i].first);
                ok &= (box[j].second < box[i].second);

                if(ok) {
                    chmax(dp[i], dp[j] + 1);
                }
            }
        }
        cout << *max_element(dp.begin(), dp.end()) << endl;
    }
    return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 999999999

int main(){
    int n,m;
    bool data[1000][1000]={};
    
    while(cin>>n&&n){
        rep(i,1000)rep(j,1000)data[i][j]=false;
        rep(i,n){
            int a,b;
            cin>>a>>b;
            data[a][b]=true;
        }
        cin>>m;
        rep(i,m){
            int a,b;
            cin>>a>>b;
            data[a][b]=true;
        }
        
        
        int dp[1002][1002];
        rep(i,1002)rep(j,1002)dp[i][j]=0;
        
        
        int maxi = 0;
        for(int i=999;i>=0;i--){
            for(int j=999;j>=0;j--){
                dp[i][j] = max({dp[i][j+1],dp[i+1][j],dp[i+1][j+1]+data[i][j],dp[i][j]});
                //            cout<<dp[i][j]<<endl;
                maxi = max(maxi,dp[i][j]);
            }
        }
        cout<<maxi<<endl;
    }
}