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

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef pair<int,int> P;

int main(){
	int n,m,h,r;
	vector<P> mat;
	vector<int> lh,lr;
	while(cin >> n,n){
		for(int i = 0;i < n;i++) {
			cin >> h >> r;
			mat.push_back(P(h,r));
		}
		cin >> m;
		for(int i = n;i < n + m;i++){
			cin >> h >> r;
			mat.push_back(P(h,r));
		}
		sort(mat.begin(),mat.end());
		for(int i = 0;i < mat.size();i++){
			if(lh.empty()) {
				lh.push_back(mat[i].first);
				lr.push_back(mat[i].second);
			}
			else{
				if(lr.back() < mat[i].second) {
					if(lh.back() < mat[i].first) {
						lh.push_back(mat[i].first);
						lr.push_back(mat[i].second);
					}
				}
				else {
					if(lh.back() > mat[i].first){
						int pos = lower_bound(lr.begin(),lr.end(),mat[i].second) - lr.begin();
						lh[pos] = mat[i].first;
						lr[pos] = mat[i].second;
					}
				}
			}
		}
		cout << lh.size() << endl;
		lh.clear();
		lr.clear();
		mat.clear();
	}
	return 0;
}

C++: #include <iostream>
#include <list>
#include <algorithm>
#define F first
#define S second
using namespace std;

typedef pair<int,int> P;

int main(void){
	P dolls[200];
	P dolls1[200];
	int n,m;
	while(cin >> n,n){
		for(int i=0;i<n;i++)
			cin >> dolls[i].F >> dolls[i].S;
		cin >> m;
		for(int i=n;i<n+m;i++)
			cin >> dolls[i].F >> dolls[i].S;
		for(int i=0;i<n+m;i++)
			dolls1[i].F=dolls[i].S,dolls1[i].S=dolls[i].F;
		sort(dolls,dolls+n+m);
		sort(dolls1,dolls1+n+m);
		int coux=0,couy=0,cx=0,cy=0;
		for(int i=0;i<n+m;i++)
			if(cx<dolls[i].F&&cy<dolls[i].S)
				cx=dolls[i].F,cy=dolls[i].S,coux++;
		cx=0,cy=0;
		for(int i=0;i<n+m;i++)
			if(cx>dolls1[i].F&&cy>dolls1[i].S)
				cx=dolls1[i].F,cy=dolls1[i].S,couy++;
		cout << max(coux,couy) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;

const int MAX_N = 201;
const int MAX_H = 1001;
const int MAX_R = MAX_H;
int N;
int H[MAX_N], R[MAX_N];
int T[MAX_H][MAX_R];

int solve() {
  int ans = 0;
  fill(T[0],T[MAX_H],0);
  for(int j = 0; j < MAX_H; ++j) {
    for(int k = 0; k < MAX_R; ++k) {
      for(int i = 0; i < N; ++i) {
	if(H[i] > j && R[i] > k) {
	  T[H[i]][R[i]] = max(T[H[i]][R[i]], T[j][k] + 1);
	  ans = max(ans, T[H[i]][R[i]]);
	}
      }
    }
  }
  return ans;
}

int main() {
  while(cin >> N && N) {
    for(int i = 0; i < N; ++i) cin >> H[i] >> R[i];
    int m;
    cin >> m;
    for(int i = 0; i < m; ++i) cin >> H[N+i] >> R[N+i];
    N += m;
    cout << solve() << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 30;
int main(){
  int n, m;
  int dp[2000];
  P doll[2000];
  while(cin >> n && n){
    fill(doll, doll+2000, P(0, 0));
    fill(dp, dp + 2000, INF);
    for (int i = 0; i < n; i++){
      int a, b;
      cin >> a >> b;
      doll[i] = P(a, b);
    }
    cin >> m;
    for (int i = n; i < n + m; i++){
      int a, b;
      cin >> a >> b;
      doll[i] = P(a, b);
    }
    sort(doll, doll + n + m);
    dp[0] = doll[0].S;
    for (int i = 1; i < n + m; i++){
      if(doll[i].F != doll[i-1].F)
	*lower_bound(dp, dp + 2000, doll[i].S) = doll[i].S;
    }
     int ans;
        for(int i=n+m-1; i>=0; --i){
            if(dp[i] != INF){
                ans = i + 1;
                break;
            }
        }
	cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;

typedef pair<int,int> P;

bool involve(P a, P b) {
  return (a.first<b.first && a.second<b.second);
}

int main() {
  int n, m, s, t;
  int a[200];
  P dolls[201];
  while (1) {
    cin >> n;
    if (!n) break;
    dolls[0].first = dolls[0].second = 0;
    for (int i=1; i<=n; i++) {
      cin >> dolls[i].first >> dolls[i].second;
    }
    cin >> m;
    for (int i=1; i<=m; i++) {
      cin >> dolls[i+n].first >> dolls[i+n].second;
    }
    s = n+m;
    sort(dolls, dolls+s+1);
    for (int i=0; i<=s; i++) {
      a[i] = 0;
    }
    for (int i=1; i<=s; i++) {
      for (int j=0; j<i; j++) {
        if (involve(dolls[j], dolls[i])) {
          a[i] = max(a[i], a[j]+1);
        }
      }
    }
    t = 0;
    for (int i=0; i<=s; i++) {
      if (t < a[i]) {
        t = a[i];
      }
    }
    cout << t << endl;
  }
  return 0;
}