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 <algorithm>
#include <iostream>
#include <map>

using namespace std;

#define fst first
#define snd second
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
typedef pair<int,int> pii;

int n,m;
int dp[210];
pii dolls[210];

const int inf=1<<27;

int main(void){
	while(cin >> n ,n){
		rep(i,210) dp[i]=inf;

		rep(i,n) cin >> dolls[i].fst >> dolls[i].snd;
		cin >> m;
		rep(i,m) cin >> dolls[n+i].fst >> dolls[n+i].snd;
		
		sort(dolls,dolls+n+m);

		rep(i,n+m)	*lower_bound(dp,dp+n+m,dolls[i].snd)=dolls[i].snd;
		int ans=lower_bound(dp,dp+n+m,inf)-dp;
		cout << ans << endl;
	}
	return 0;
}

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

typedef pair<int,int> pii;

bool eqfirst(const pii &p, const pii &q){
	return p.first == q.first;
}

int main(){
	int n, h, r;
	const int INF = 1 << 30;
	while(1){
		vector<pii> v;
		for(int i = 0; i < 2; ++i){
			if(scanf("%d", &n) == EOF){ return 0; }
			for(int j = 0; j < n; ++j){
				scanf("%d%d", &h, &r);
				v.push_back(pii(h, r));
			}
		}
		sort(v.begin(), v.end());
		v.erase(unique(v.begin(), v.end(), eqfirst), v.end());

		vector<int> dp(v.size() + 1, INF);
		for(int i = 0; i < v.size(); ++i){
			r = v[i].second;
			*upper_bound(dp.begin(), dp.end(), r) = r;
		}
		printf("%d\n", lower_bound(dp.begin(), dp.end(), INF) - dp.begin());
	}
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef pair<int, int>P;

int main() {
	int n;
	while (scanf("%d", &n), n) {
		vector<P>v1, v2;
		rep(i, n) {
			int h, r; scanf("%d%d", &h, &r);
			v1.push_back(P(h, r));
		}
		int m; scanf("%d", &m);
		rep(i, m) {
			int h, r; scanf("%d%d", &h, &r);
			v2.push_back(P(h, r));
		}
		sort(v1.begin(), v1.end());
		sort(v2.begin(), v2.end());
		v1.push_back(P(2000, 2000));
		v2.push_back(P(2000, 2000));
		int cnt1 = 0;
		rep(i, n + 1) {
			rep(j, m) {
				if ((i&&v1[i - 1].first < v2[j].first&&v1[i - 1].second < v2[j].second) &&
					v2[j].first < v1[i].first&&v2[j].second < v1[i].second)
					cnt1++;
			}
		}
		int cnt2 = 0;
		rep(i, m + 1) {
			rep(j, n) {
				if ((i&&v2[i - 1].first < v1[j].first&&v2[i - 1].second < v2[j].second) &&
					v1[j].first < v2[i].first&&v1[j].second < v2[i].second)
					cnt2++;
			}
		}
		printf("%d\n", max(cnt1 + n, cnt2 + m));
	}
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <set>

using namespace std;

#define ALL(co) co.begin(), co.end()
#define tpl(...) make_tuple(__VA_ARGS__)

typedef long long LL;
typedef pair<int, int> P; typedef pair<int, P> IP; typedef pair<P, P> PP;
typedef vector<int> Array; typedef vector<vector<int> > Array2;
typedef vector<LL> LArray; typedef vector<P> PArray; typedef vector<string> SArray;

const int INF = 1 << 29;
const LL LINF = 1LL << 60;

inline int getInt() { int itiv; return (cin >> itiv, itiv); }
template <typename T> void readAll(vector<T>& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i]; }
template <typename T, typename U> void readAll(vector<pair<T, U> >& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i].first >> vec[i].second; }
template < typename T > inline string toString( const T &a ){ ostringstream oss; oss << a; return oss.str(); };
inline bool between(int min, int max, int n) { return min <= n && n <= max; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }

int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
int dr[] = { 0, -1, 0, 1 }, dc[] = { -1, 0, 1, 0 };

int m, n;

int dp[210];

int main(void)
{
	ios::sync_with_stdio(false);
	while(cin >> n && n)
	{
		PArray arr;
		for (int i = 0; i < n; i++)
		{
			int h, r; cin >> h >> r;
			arr.emplace_back(h, r);
		}
		cin >> m;
		for (int i = 0; i < m; i++)
		{
			int h, r; cin >> h >> r;
			arr.emplace_back(h, r);
		}

		sort(ALL(arr));
		fill(dp, dp + n + m, INF);
		for (int i = 0; i < n + m; i++)
		{
			*(lower_bound(dp, dp + n + m, arr[i].second)) = arr[i].second;
		}
		cout << (lower_bound(dp, dp + n + m, INF) - dp) << endl;
	}
	return 0;
}

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

using P = std::pair<int, int>;

int n;

P dolls[200];

int dp[200];

int main() {
	while (1) {
		int m;
		std::cin >> n;
		if (n == 0)
			break;
		for (int i = 0; i < n; ++i)
			std::cin >> dolls[i].first >> dolls[i].second;
		std::cin >> m;
		for (int i = n; i < n + m; ++i)
			std::cin >> dolls[i].first >> dolls[i].second;
		n += m;
		std::sort(dolls, dolls + n);
		dp[0] = 1;
		for (int i = 1; i < n; ++i) {
			dp[i] = dp[i - 1];
			for (int j = i - 1; j >= 0; --j) {
				if (dolls[i].first > dolls[j].first&&
					dolls[i].second > dolls[j].second) {
					dp[i] = std::max(dp[j] + 1, dp[i]);
					break;
				}
			}
		}
		std::cout << dp[n - 1] << std::endl;
	}
	return 0;
}