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;

template<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }

void input_dolls(vector<pair<int, int>> &dolls, int n) {
	for(int i = 0; i < n; ++i) {
		int h, r;
		cin >> h >> r;
		dolls.emplace_back(h, r);
	}
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	for(int n; cin >> n && n;) {
		vector<pair<int, int>> dolls;
		input_dolls(dolls, n);

		int m;
		cin >> m;
		input_dolls(dolls, m);

		sort(dolls.begin(), dolls.end());

		int prev_r = -1;
		vector<int> dp(1000, 0);
		vector<int> tmp(dp);

		for(const auto &doll : dolls) {
			const int r = doll.second;
			if(prev_r != r) tmp = dp;

			const int num = *max_element(tmp.begin(), tmp.begin() + r);
			chmax(dp[r], num + 1);
		}

		cout << *max_element(dp.begin(), dp.end()) << endl;
	}

	return EXIT_SUCCESS;
}

C++: //Solution for aoj:0157 Russian Dolls
#include<iostream>
#include<algorithm>
using namespace std;
typedef pair<int, int>P;
const int INF = 1 << 30;

int main(){
	int n, m, a, b;
	P doll[201];
	int dp[201];
	while (cin >> n && n){
		fill(dp, dp + 201, INF);
		for (int i = 0; i < n; i++){
			cin >> a >> b;
			doll[i] = P(a, b);
		}
		cin >> m;
		for (int i = n; i < n + m; i++){
			cin >> a >> b;
			doll[i] = P(a, b);
		}
		sort(doll, doll + n + m);
		for (int i = 0; i < n + m; i++){
			if (doll[i].first != doll[i - 1].first){
				*lower_bound(dp, dp + n + m, doll[i].second) = doll[i].second;
			}
		}
		int ans;
		for (int i = n + m - 1; i >= 0; --i){
			if (dp[i] != INF){
				ans = i + 1;
				break;
			}
		}
		cout << ans << endl;
		//cout << lower_bound(dp, dp + n + m, INF) - dp << endl;
	}
}

C++: 
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <bitset>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#define strtok_r strtok_s
#endif

#define ITER(c)             __typeof__((c).begin())
#define FOREACH(it, c)      for (ITER(c) it=(c).begin(); it != (c).end(); ++it)
#define RITER(c)            __typeof__((c).rbegin())
#define RFOREACH(it, c)     for (RITER(c) it=(c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n)           REPEAT(i, 0, n)
#define RREP(i, n)          RREPEAT(i, 0, n)
#define REPEAT(i, k, n)     for(int i = (k); i < ((k)+(n)); ++i)
#define RREPEAT(i, k, n)    for(int i = (k)+(n)-1; i >= (k); --i)
#define FROMTO(i,f,t)       for(int i = f; i < t; i++)
#define ALL(c)              (c).begin(), (c).end()
#define LLPOW(p1,p2)        ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A)          (sizeof(A)/sizeof((A)[0]))
#define CIN_NO_SYNC         do { cin.tie(0); ios_base::sync_with_stdio(false); } while(0)
#define GETSTR(p)           fgets((p), sizeof(p), stdin)
#define CHOMP(p)            do{ char *_q = (p) + strlen(p)-1; if(*_q == '\n' || *_q == '\r') *_q = 0; } while(0)
#define FILL(m,v)           memset(m, v, sizeof(m))
#define mp                  make_pair
#define pb                  push_back
template<class _T> _T MAX(_T p1,_T p2){return (p1>p2)?p1:p2;} 
template<class _T> _T MIN(_T p1,_T p2){return (p1<p2)?p1:p2;} 
template <class _Tv> inline string join(const _Tv &v,string sep=" "){
    ostringstream s;FOREACH(it,v){if(it!=v.begin())s<<sep;s<<*it;};return s.str();
}

typedef long long ll;
typedef unsigned long long ull;
typedef double D;
typedef complex<D> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define X real()
#define Y imag()
#define EPS (1e-9)
#define DEQ(p1,p2) (abs((p1)-(p2)) < EPS)

#ifdef _DEBUG
template<class _Tv> inline void _prfe(const char *_n,const _Tv _c,bool _p=false){
    ITER(_c) _it=_c.begin();
    if(_p){cout<<_n<<" = ["<<endl;for(;_it!=_c.end();++_it)cout<<"    "<<*_it<<endl;cout<<"]"<<endl; }
    else{cout<<_n<<" = [ "<<*_it++;for(;_it!=_c.end();++_it)cout<<", "<<*_it;cout<<" ]"<<endl; }
}
#define pf printf
#define pr(n) do{cout<<#n" = "<<(n)<<endl;}while(0)
#define prfe(n) _prfe(#n, n)
#define prfep(n) _prfe(#n, n, true)
#define dbgchk(n) do{if(n)throw;}while(0)
#else
#define pf(...) /* */
#define pr(...) /* */
#define prfe(...) /* */
#define prfep(...) /* */
#define dbgchk(...) /* */
#endif

bool operator<(const pair<int,int> &p1, const pair<int,int> &p2){
    return (p1.first < p2.first && p1.second < p2.second);
}

int solve(vector<pair<int,int> > &s){
    int n = s.size();
    int dp[210];
    FILL(dp,0);
    REP(k,n) REP(i,n){
        int mx = 0;
        REP(j,n) if(s[j] < s[i]) mx = MAX(mx, dp[j]);
        dp[i] = mx + 1;
    }
    int mx = 0;
    REP(i,n) mx = MAX(mx, dp[i]);
    return mx;
}

int main(){
    int n, m;
    while(cin >> n && n){
        vector<pair<int,int> > s;
        int h, r;
        REP(i,n){
            cin >> h >> r;
            s.pb(mp(h,r));
        }
        cin >> m;
        REP(i,m){
            cin >> h >> r;
            s.pb(mp(h,r));
        }
        cout << solve(s) << endl;
    }
    return 0;
}

C++: //Solution for aoj:0157 Russian Dolls
#include<iostream>
#include<algorithm>
using namespace std;
typedef pair<int, int>P;
const int INF = 1 << 30;
int n, m, ans, p, s, s2, s3;
pair<int,int> doll[201];
int dp[2][201],dp2[201];
int main(){
	while (cin >> n){
		if (!n)
			return 0;
		fill(dp[0], dp[0] + 201, INF);
		fill(dp[1], dp[1] + 201, INF);
		fill(dp2, dp2 + 201, INF);
		for (int i = 1; i <= n; i++){
			int a, b;
			cin >> a >> b;
			doll[i].first = a;
			doll[i].second = b;
		}
		cin >> m;
		for (int i = n + 1; i <= n + m; i++){
			int a, b;
			cin >> a >> b;
			doll[i].first = a;
			doll[i].second = b;
		}
		sort(doll + 1, doll + 1 + n + m);
		for (int i = 1; i <= n + m; i++){
			p = lower_bound(dp[0] + 1, dp[0] + n + m + 1, doll[i].first) - dp[0];
			if (dp[1][p] > doll[i].second){
				dp[0][p] = doll[i].first;
				dp[1][p] = doll[i].second;
			}
		}
		for (int i = 1; i <= n + m; i++){
			*lower_bound(dp2 + 1, dp2 + n + m + 1, dp[1][i]) = dp[1][i];
		}
		cout <<lower_bound(dp2 + 1, dp2 + n + m + 1, INF) - dp2 << endl;
	}
}