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>

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

using namespace std;

int main() {
  while(1) {
    vector<pair<int,int>> mt;
    REP(i,2) {
      int n;
      cin >> n;
      if (!n) return 0;
      REP(j,n) {
        int h,r;
        cin>>h>>r;
        mt.emplace_back(h,r);
      }
    }
    sort(begin(mt),end(mt));
    int n = mt.size();
    vector<int> res(n);
    REP(i,n) {
      int mx = 0;
      REP(j,i) {
        if (mt[j].first < mt[i].first && mt[j].second < mt[i].second)
          mx=max(mx, res[j]);
      }
      res[i] = mx+1;
    }
    cout << *max_element(begin(res),end(res)) << endl;
  }
  return 0;
}

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

using namespace std;

struct doll {
    int x, h;
    bool operator<(const doll& o) const {
        return x < o.x && h < o.h;
    }
};

bool compare_by_x(const doll& a, const doll& b) {
    return a.x < b.x;
}

int n, m;
vector<doll> ds;
vector<int> dp;

int solve(int c) {
    if (0 <= dp[c]) return dp[c];
    for (int i = 0; i < c; i++) {
        if (ds[i] < ds[c]) {
            dp[c] = max(dp[c], solve(i)+1);
        }
    }
    return dp[c];
}

int main() {
    while (cin >> n, n != 0) {
        ds.clear();
        for (int i = 0; i < n; i++) {
            doll d;
            cin >> d.x >> d.h;
            ds.push_back(d);
        }
        cin >> m;
        for (int i = 0; i < m; i++) {
            doll d;
            cin >> d.x >> d.h;
            ds.push_back(d);
        }
        sort(ds.begin(), ds.end(), compare_by_x);
        dp.clear();
        dp.resize(n+m, -1);

        dp[0] = 1;
        int max_mat = 0;
        for (int i = 0; i < n+m; i++) 
            max_mat = max(max_mat, solve(i));
        cout << max_mat << endl;
    }
    return 0;
}

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

typedef pair<int, int> P;

int m,n;
int dp[1002][1002];
vector<P> v(200);

int rec(int h, int r)
{
    if(dp[h][r] >= 0) 
        return dp[h][r];
    
    int ret = 0;
    for(int i = 0; i < m + n; i++) {
        if(v[i].first < h && v[i].second < r) {
            ret = max(ret, rec(v[i].first, v[i].second) + 1);
        }
    }
    
    return dp[h][r] = ret;
}

int main()
{
    while(cin >> n && n) {
        v.clear();
        int h,r;
        for(int i = 0; i < n; i++) {
            cin >> h >> r;
            v.push_back(P(h, r));
        }

        cin >> m;
        for(int i = 0; i < m; i++) {
            cin >> h >> r;
            v.push_back(P(h, r));
        }
        
        memset(dp, -1, sizeof(dp));
        cout << rec(1001, 1001) << endl;
    }
}

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

#define REP(i,n,m) for(int i=n;i<m;i++)
#define rep(i,n) REP(i,0,n)

int n,m;
int t[201][2];
int dp[201];

int solve(int idx){
	if(dp[idx] != -1) return dp[idx];

	int res = 0;

	rep(i,n){
		if(t[idx][0] > t[i][0] && t[idx][1] > t[i][1]){
			res = max(res,solve(i));
		}
	}

	return (dp[idx] = res + 1);
}

int main(void){
	t[200][0] = t[200][1] = 1001;

	while(cin>>n && n){
		rep(i,n) cin>>t[i][0]>>t[i][1];
		cin>>m;
		REP(i,n,n+m) cin>>t[i][0]>>t[i][1];
		n += m;

		memset(dp,-1,sizeof(dp));
		cout<<solve(200)-1<<endl;
	}

	return 0;
}

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

int n,m;

typedef pair<int,int> p1;
typedef pair<int,p1> P;

int calc(P d[1000],int c)
{
	int res = 0;
	int ok = 0;
	P now = P(0,p1(0,0));
	for(int i = 0; i < 1000; i++){
		if(d[i].second.first > now.second.first && d[i].second.second > now.second.second){
			res++;
			now = d[i];

		}
	}

	return res;
}

void solve()
{
	P Rs[1000] = {},Hs[1000] = {};
	P in;
	int c = 0;

	for(int i = 0; i < n; i++,c++){
		cin >> in.second.first >> in.second.second;
		in.first = (in.second.first-in.second.second) & ~(65536);
		Rs[c] = in;
		swap(in.second.first, in.second.second);
		Hs[c] = in;
	}
	cin >> m;
	for(int j = 0; j < m; j++,c++){
		cin >> in.second.first >> in.second.second;
		in.first = (in.second.first-in.second.second) & ~(65536);
		Rs[c] = in;
		swap(in.second.first, in.second.second);
		Hs[c] = in;
	}
	sort(Rs,Rs+c);
	sort(Hs,Hs+c);

	int res = calc(Rs,c);
	res = max(res, calc(Hs,c));

	for(int i = 0; i < 1000; i++){
		Rs[i].first = 0;
	}

	for(int i = 0; i < 1000; i++){
		Hs[i].first = 0;
	}
	sort(Rs,Rs+c);
	sort(Hs,Hs+c);

	res = max(res, calc(Rs,c));
	res = max(res, calc(Hs,c));

	cout << res << endl;
}


int main(void)
{
	while(cin >> n, n){
		solve();
	}
	return 0;
}

C++: #include<iostream>

using namespace std;

int main(void){
  int n,m,h[201],r[201];

  while(cin >> n,n){
    for(int i=0;i<n;i++)cin >> h[i] >> r[i];
    cin >>m;
    for(int i=n;i<n+m;i++)cin >> h[i] >> r[i];

    for(int i=0;i<n+m;i++){
      for(int j=i;j<n+m;j++){
	if(h[i]>h[j]){
	  int t=h[i];
	  h[i]=h[j];
	  h[j]=t;
	  t=r[i];
	  r[i]=r[j];
	  r[j]=t;
	}
      }
    }
    for(int i=0;i<n+m;i++){
      for(int j=i;j<n+m;j++){
	if(r[i]>r[j]){
	  int t=h[i];
	  h[i]=h[j];
	  h[j]=t;
	  t=r[i];
	  r[i]=r[j];
	  r[j]=t;
	}
      }
    }

    int dp[201]={0};
    dp[0]=1;
    for(int i=0;i<n+m;i++){
      for(int j=0;j<n+m;j++){
	if(h[i]<h[j] && r[i]<r[j] && dp[i]>=dp[j])dp[j]=dp[i]+1;
      }
    }
    int res=0;
    for(int i=0;i<n+m;i++)res=max(res,dp[i]);
    cout << res << endl;
  }

  return 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;
				prev_r = r;
			}

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