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

int main(){
    int n = 0, m = 0;
    int d[200][2], dp[200];

    for(;;){
        if(cin >> n){
            for(int i = 0; i < 200; i++){
                dp[i] = 0;
                d[i][0] = 0;
                d[i][1] = 0;
            }
            if(n == 0){
                break;
            }
            for(int i = 0; i < n; i++){
                cin >> d[i][0] >> d[i][1];
                for(int j = i; j > 0; j--){
                    if(d[j][0] > d[j - 1][0]){
                        break;
                    }else{
                        swap(d[j][0], d[j - 1][0]);
                        swap(d[j][1], d[j - 1][1]);
                    }
                }
            }
            cin >> m;
            for(int i = n; i < n + m; i++){
                cin >> d[i][0] >> d[i][1];
                for(int j = i; j > 0; j--){
                    if(d[j][0] > d[j - 1][0]){
                        break;
                    }else{
                        swap(d[j][0], d[j - 1][0]);
                        swap(d[j][1], d[j - 1][1]);
                    }
                }
            }
            int maax = 0;
            for(int i = 0; i < n + m; i++){
                dp[i]++;
                int a = 0;
                for(int j = 0; j < i; j++){
                    if(a <= dp[j]){
                        if(d[j][1] < d[i][1]){
                            a = dp[j];
                        }
                    }
                }
                dp[i] += a;
                if(maax <= dp[i]){
                    maax = dp[i];
                }
            }
            cout << maax << endl;
        }else{
            break;
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)

int n, m;
int dp[100];
pair<int, int> data[200];

int main() {
	while (cin >> n, n) {
		REP(i, n) scanf("%d %d", &data[i].first, &data[i].second);
		cin >> m;
		FOR(i, n, n + m) scanf("%d %d", &data[i].first, &data[i].second);
		
		sort(data, data + n + m);
		fill(dp, dp + n + m, 1);
		
		REP(i, n + m) REP(j, i) {
			if (data[i].first > data[j].first && data[i].second > data[j].second) {
				dp[i] = max(dp[i], dp[j] + 1);
			}
		}
		
		int ans = -1;
		REP(i, n + m) ans = max(ans, dp[i]);
		cout << ans << endl; 
	}
	return 0;
}

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

using namespace std;

int main() {
	int n;
	while ( cin >> n, n ) {
		vector<int> h(n), r(n);
		for (int i = 0; i < n; ++i) {
			cin >> h[i] >> r[i];
		}
		int M; cin >> M;
		h.resize(n+M);
		r.resize(n+M);
		for (int i = 0; i < M; ++i) {
			cin >> h[n+i] >> r[n+i];
		}
		n += M;

		int m[200][200] = {0};
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				if (h[i] < h[j] && r[i] < r[j]) {
					m[i][j] = -1;
				}
			}
		}
		int ans = 0;
		for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
			if (m[i][k] && m[k][j]) {
				m[i][j] = min(m[i][j], m[i][k] + m[k][j]);
				ans = min(ans, m[i][j]);
			}
		}
		cout << (-ans + 1) << endl;
	}
}

C++: #include<stdio.h>
#include<algorithm>
struct S{int h,r,c;}a[201];
int C(S l,S r){return l.h+l.r<r.h+r.r;} 
int main()
{
	int m,n,i,j;
	while(scanf("%d",&n),n)
	{
		for(i=0;i<n;a[i++].c=0)scanf("%d%d",&a[i].h,&a[i].r);
		for(scanf("%d",&m),n+=m;i<n;a[i++].c=0)scanf("%d%d",&a[i].h,&a[i].r);
		a[n].h=a[n].r=a[n].c=0;
		std::sort(a,a+n+1,C);
		for(i=n;i>=0;--i)
			for(j=i+1;j<=n;++j)
				if(a[i].h<a[j].h&&a[i].r<a[j].r)
					a[i].c=std::max(a[i].c,a[j].c+1);
		printf("%d\n",a[0].c);
	}
	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 result = 0;
		int dp[ 201 ][ 1000 ];
		memset( dp, 0, sizeof(dp) );
		for( int i = 0; i < (int)sortedList.size(); ++i ){
			int r = sortedList[ i ][ 0 ].second;
			for( int j = 0; j < r; ++j ){
				dp[ i + 1 ][ j ] = dp[ i ][ j ];
			}
			for( int j = r; j < 1000; ++j ){
				dp[ i + 1 ][ j ] = max( dp[ i ][ j ], dp[ i ][ r - 1 ] + 1 );
			}
		}

		return dp[ sortedList.size() ][ 1000 - 1 ];
	}

	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;
}