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 <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), [&](const P& p1, const P& p2){
			return p1.first != p2.first ? p1.first < p2.first
				: p1.second != p2.second ? p1.second > p2.second
				: false;
		});
		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 <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cctype>

#include <iostream>
#include <vector>
#include <deque>
#include <queue>
#include <map>
#include <algorithm>

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)
#define repi(i,l,n) for(int (i)=(int)(l);(i)<(int)(n);(i)++)
#define d_arr(arr, h, w) rep(i,(h) ){ cout << "["; rep(j,(w) ) { cout << (arr)[i][j] << ", "; } cout << "]" << endl;}
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x);
#define fs first
#define sc second
#define pb push_back

typedef pair<int ,int> P;
vector<P> mv;
map<int, int> memo;

//
int next_of(int i){
    if( i == mv.size()-1 ) { return mv.size(); }
    else if( i >= mv.size()-1 ) { return mv.size(); }
    int h = mv[i].fs;
    int r = mv[i].sc;
    int nh = h;
    int nr = r;
    while(1){
        if( nh > h && nr > r) { break; }
        i++;
        nh = mv[i].fs;
        nr = mv[i].sc;
    }
    return i;
}

// mv[i] 以上で作れるマトリョーシカの数の最大値
int rec(int i){
    //cout << "rec : " << i << endl;
    if( memo.find(i) != memo.end() ){ return memo[i]; }
    if( i == mv.size() ){ return 0; }
    if( mv.size()-1 <= i ){
        return 0;
    }
    int e0 = rec(i+1);
    int e1 = 0;
    int next = next_of(i);
    e1 = rec(next) + 1;
    memo[i] = max(e0,e1);
    return memo[i];
}

int main()
{
    int n,m;
    int h,r;
    while(1){
        mv.clear();
        memo.clear();
        cin >> n;
        if( n==0 ){ break; }
        rep(i,n){
            cin >> h >> r;
            mv.pb(make_pair(h,r));
        }
        cin >> m;
        rep(i,m){
            cin >> h >> r;
            mv.pb(make_pair(h,r));
        }
        sort(mv.begin(),mv.end());
        cout << rec(0) << endl;
    }
}

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

int main(){
	while(1){
		bool dp[1000][1000] = {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 = cnt; i < 1000; i++){
			for(int j = cnt; j < 1000; j++){
				if(dp[i][j] == true || dp[j][i] == true){
					ans++;
					break;
				}
			}
			cnt++;
		}
		cout <<ans <<endl;
	}
	return 0;
}

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

vector<int> dp;
vector<int> x,y;
int N;

int calc(int id){
	if(dp[id]>=0) return dp[id];
	int res=1;
	for(int i=0;i<N;i++){
		if(x[id]>x[i] && y[id]>y[i]){
			res=max(res,calc(i)+1);
		}
	}
	dp[id]=res;
	return res;
}

int main()
{
	int n,m;
	while(cin>>n){
		if(n==0) break;
		x=vector<int>();
		y=vector<int>();
		for(int i=0;i<n;i++){
			int a,b;
			cin>>a>>b;
			x.push_back(a);
			y.push_back(b);
		}
		cin>>m;
		N=n+m;
		dp=vector<int>(N,-1);
		for(int i=0;i<m;i++){
			int a,b;
			cin>>a>>b;
			x.push_back(a);
			y.push_back(b);
		}
		int res=0;
		for(int i=0;i<N;i++){
			res=max(res,calc(i));
		}
		cout<<res<<endl;
	}
	return 0;
}