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 Matryoshka{
public:
  int h;
  int r; 

};

bool LessMatryoshka(const Matryoshka &a, const Matryoshka &b){
  if( a.h < b.h && a.r < b.r ){
    return true;
  }else if( a.h == b.h || a.r == b.r ){
    return true;
  }else{
    return false;
  }
} 

typedef vector< Matryoshka > Dolls;

void MaxSubSequenceLength( Dolls &d, vector< int > &ans ){
  ans[0] = 0;
  for(int i = 1; i < d.size(); i++){
    int maxSub = 0, maxj = -1;

    for(int j = i - 1; j >= 0; j--){
      if( d[j].r < d[i].r && d[j].h < d[i].h ){
	if( maxSub < ans[j] ){
	  maxj = j;
	  maxSub = ans[j];
	}
      }
    }

    if( maxj < 0 ){
      ans[i] = 1;
    }else{
      ans[i] = ans[maxj] + 1;
    }
  }
}

void InputMatryoshka( Dolls &d, int num ){
  for(int i = 0; i < num; i++){
    Matryoshka m;
    
    cin >> m.h;
    cin >> m.r;
    d.push_back( m );
  }
}

int main(void){
  while( 1 ){
    int n, m;
    Dolls dolls;
    Matryoshka matryoBanpei;
    int maxMatryoshkaNum = 0;
    vector<int> ans;

    cin >> n;
    if( n == 0 ){
      break;
    }

    matryoBanpei.h = -1;
    matryoBanpei.r = -1;
    dolls.push_back( matryoBanpei );
    
    InputMatryoshka( dolls, n );

    cin >> m;
    InputMatryoshka( dolls, m );

    sort( dolls.begin(), dolls.end(), LessMatryoshka );
    ans.resize( n+m+1, 0 );
    MaxSubSequenceLength( dolls, ans );

    //output
    for(int i = 0; i < ans.size(); i++){
      maxMatryoshkaNum = max( maxMatryoshkaNum, ans[i] );
    }
    cout << maxMatryoshkaNum << '\n';
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<short, short> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

vector<P> v;
int n, m;
int dp[201][1001];

int solve(int i, int r)
{
	if (i == v.size()) return 0;
	if (dp[i][r] >= 0) return dp[i][r];
	int res;
	if (-v[i].second > r)
	{
		res = max(solve(i + 1, r), solve(i + 1, -v[i].second) + 1);
	}
	else res = solve(i + 1, r);
	return dp[i][r] = res;
}

int main()
{
	while (cin >> n, n)
	{
		v.clear();
		MS(dp, -1);
		REP(i, n)
		{
			int a, b; cin >> a >> b;
			v.push_back(P(a, -b));
		}
		cin >> m;
		REP(i, m)
		{
			int a, b; cin >> a >> b;
			v.push_back(P(a, -b));
		}
		sort(ALL(v));

		cout << solve(0, 0) << endl;
	}
	return 0;

}

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

using namespace std;

class Matryoshka{
public:
  int h;
  int r; 

  bool operator < ( const Matryoshka &m ) const {
    if( h < m.h && r < m.r ){
      return true;
    }else{
      return false;
    }
  }
};

typedef vector< Matryoshka > Dolls;

void MaxSubSequenceLength( Dolls &d, vector< int > &ans ){
  ans[0] = 0;
  for(int i = 1; i < d.size(); i++){
    int maxSub = -1, maxj;

    for(int j = i - 1; j >= 0; j--){
      if( d[j].r < d[i].r && d[j].h < d[i].h ){
	if( maxSub < ans[j] ){
	  maxj = j;
	  maxSub = ans[j];
	}
      }
    }

    ans[i] = ans[maxj] + 1;
  }
}

void InputMatryoshka( Dolls &d, int num ){
  for(int i = 0; i < num; i++){
    Matryoshka m;
    
    cin >> m.h;
    cin >> m.r;
    d.push_back( m );
  }
}

int main(void){
  while( 1 ){
    int n, m;
    Dolls dolls;
    Matryoshka matryoBanpei;
    int maxMatryoshkaNum = 0;
    vector<int> ans;

    cin >> n;
    if( n == 0 ){
      break;
    }

    matryoBanpei.h = -1;
    matryoBanpei.r = -1;
    dolls.push_back( matryoBanpei );
    
    InputMatryoshka( dolls, n );

    cin >> m;
    InputMatryoshka( dolls, m );

    sort( dolls.begin(), dolls.end() );
    ans.resize( n+m+1, 0 );

    MaxSubSequenceLength( dolls, ans );

    //output
    for(int i = 0; i < ans.size(); i++){
      maxMatryoshkaNum = max( maxMatryoshkaNum, ans[i] );
    }
    cout << maxMatryoshkaNum << '\n';
  }
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <iomanip>
  
using namespace std;

#define pi 3.141592653589793

int main()
{
	int n, m;
	while(1){
		cin >> n;
		if(n == 0) break;
		vector<pair<int, int> > mato(200);
		for(int i = 0; i < n; i++){
			cin >> mato[i].first >> mato[i].second;
		}
		cin >> m;
		for(int i = 0; i < m; i++){
			cin >> mato[n + i].first >> mato[n + i].second;
		}
		sort(mato.begin(), mato.begin() + m + n);
		int cnt = 0;
		int maxh = 0, maxr = 0;
		for(int i = 0; i < n + m; i++){
			// cout << mato[i].first << " " << mato[i].second;
			if(mato[i].first > maxh && mato[i].second > maxr){
				cnt++;
				maxh = mato[i].first;
				maxr = mato[i].second;
			}
			// cout << " " << cnt << endl;
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
int main(){
  int n,m,x,y;
  while(cin >>n,n){
    vector<pair<int, int> > doll;
    vector<int> lot;
    int dp[201] = {0};
    int ans = 0;
    for(int i=0; i<n; i++){cin >>x>>y;doll.push_back(make_pair(x,y));}
    cin >>m;
    for(int i=0; i<m; i++){cin >>x>>y;doll.push_back(make_pair(x,y));}
    sort(doll.begin(),doll.end());
    lot.push_back(0);
    for(int i=0,r=-1; i<doll.size(); i++){
      if(r != doll[i].first){
	r = doll[i].first;
	lot.push_back(doll[i].second);
      }
    }
    for(int i=1; i<lot.size(); i++){
      int r = 0;
      for(int j=0; j<i; j++){
	if(lot[j]<lot[i] && r<dp[j]){r = dp[j];}
      }
      dp[i] = r+1;
    }
    for(int i=0;i<lot.size();i++){
      if(ans<dp[i]){ans = dp[i];}
    }
    cout <<ans<<endl;
  }
  return 0;
}