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 <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init));

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

struct data{
  ll h,r,s;
  bool operator<(const data& d){
    return s < d.s;
  }
};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;
  while(cin >> n && n){
    vector<data> v;
    ll h,r,mh = 0;
    rep(i,n){
      cin >> h >> r;
      mh = max(mh,h);
      v.push_back({h,r,h+r});
    }
    cin >> m;
    rep(i,m){
      cin >> h >> r;
      mh = max(mh,h);
      v.push_back({h,r,h+r});
    }
    sort(all(v));
    ll ans = 0,c = 0;
    vary(ll,dp,n+m+1,0);
    rep(i,n+m){
      dp[i] = 1;
      REP(j,0,i){
        if(v[j].h < v[i].h && v[j].r < v[i].r){
          dp[i] = max(dp[i],dp[j]+1);
        }
      }
      ans = max(ans,dp[i]);
    }
    cout << ans << endl;
  }
  return 0;
}

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

typedef std::pair<int, int> P;

P data[202];
int dp[202];

int main(){

	int n, m;
	while (std::cin >> n, n){

		memset(data, 0, sizeof(data));
		memset(dp, 0, sizeof(dp));

		for (int i = 1; i <= n; i++){
			std::cin >> data[i].first >> data[i].second;
		}

		std::cin >> m;
		for (int i = 1; i <= m; i++){
			std::cin >> data[i + n].first >> data[i + n].second;
		}

		std::sort(data, data + 1 + n + m);

		for (int i = 1; i <= n + m; i++){
			for (int j = 0; j < i; j++){
				if (data[i].first > data[j].first&&data[i].second > data[j].second){
					dp[i] =  dp[j] + 1;
				}
			}
		}

		int max = 0;
		for (int i = 0; i <= n + m; i++){
			if (dp[i] > max)max = dp[i];
		}
		std::cout << max << std::endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdlib>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
#define MAX 206
using namespace std;
int test=0;
int n,m;
int dp[MAX+1];
class Doll{
public:
   int h;
   int r;
   bool operator < (const Doll &d)const{
      if(h == d.h)return r < d.h;//hが同じ時ｒで比較
      return h < d.h;//普段はｈで比較
   }
};

Doll Dolls[MAX];

void solve(void){
   dp[0] = 0;
   dp[1] = 1;
   for(int i=0;i<(n+m);i++){
      //dp[i+1] = 1;
      for(int j=0;j<i;j++){
	 if(Dolls[j].r < Dolls[i].r){
	    dp[i+1] = max(dp[i],dp[j+1]+1);
	 }
      }
   }

   cout << dp[n+m] << endl;
}

int main(void){
   int h,r;
   while(1){
      cin >> n;
      if(n==0)break;
      for(int i=0;i<n;i++){
	 cin >> h >> r;
	 Dolls[i].h = h;
	 Dolls[i].r = r;
      }
      cin >> m;
      for(int i=n;i<(n+m);i++){
	 cin >> h >> r;
	 Dolls[i].h = h;
	 Dolls[i].r = r;
      }
      sort(Dolls,Dolls+(n+m)-1);
      solve();
      rep(i,n+m)
	 cout << Dolls[i].h << " " << Dolls[i].r << " " << dp[i+1] << endl;
      cout << endl;
   }
   return 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 count = 0;
		int y = 0;
		for( int i = 0; 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;
				}
			}
		}
		return count;
	}

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

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

int main(){
	while(1){
		bool dp[NUM][NUM] = {false};
		int n, m, h, r, ans = 0;
		cin >>n;
		if(n == 0) break;
		for(int i = 0; i < n; i++){
			cin >>h >>r;
			dp[h][r] = true;
		}
		cin >>m;
		for(int i = 0; i < m; i++){
			cin >>h >>r;
			dp[h][r] = true;
		}
		int cnt = 1;
		for(int i = 1; i < NUM; i++){
			for(int j = cnt; j < NUM; j++){
				if(dp[i][j] == true){
					ans++;
					cnt = j + 1;
					break;
				}
			}
		}
		cout <<ans <<endl;
	}
	return 0;
}