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 <cstdio>
#include <utility>
#include <cctype>
#include <vector>
#include <algorithm>
using namespace std;

#ifdef __linux
#define _U(s) s##_unlocked
#else
#define _U(s) _##s##_nolock
#define _CRT_DISABLE_PERFCRIT_LOCKS
#endif
#define gcu _U(getchar)
#define pcu _U(putchar)
#define _DEF(r, n, ...) inline r n(__VA_ARGS__) noexcept
#define _T template <typename T>
#define _HT template <typename H,typename... T>
#define _OP(t) inline operator t()
struct _in {
#ifdef _GLIBCXX_STRING
	_OP(string){string s;char c;while(isspace(c = gcu()));do{s+=c;}while(c=gcu(),c!=' '&&c!='\n'&&c!=EOF);return s;}
#define _S
#endif
	_OP(char){char c=gcu();gcu();return c;}
	_OP(double){double d; scanf("%lf",&d); gcu();return d;}
	_T _OP(T){T n{},m{1},c;while(isspace(c = gcu()));if(c=='-')m=-1,c=gcu();do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;}
} in;
#define _SCAN(...) _DEF(bool,scan,__VA_ARGS__)
#ifdef _S
_SCAN(string &o) {int c{gcu()};if(c==EOF)return false;else{ungetc(c,stdin);string t=move(in);o=t;return true;}}
#endif
_T _SCAN(T &o) {int c{gcu()};return c==EOF?false:(ungetc(c,stdin),o=in,true);}
_HT _SCAN(H &h,T&&... t){return scan(h)&&scan(t...);}
#define _OUT(...) _DEF(void,out,__VA_ARGS__)
#define _OUTL(...) _DEF(void,outl,__VA_ARGS__)
_OUT(bool b){pcu('0'+b);}
_OUT(const char *s){while(*s)pcu(*s++);}
_OUT(char c){pcu(c);}
#ifdef _S
_OUT(string &s){for(char c:s)pcu(c);}
#endif
_T _OUT(T n){static char b[20];char *p=b;T m=n<0?pcu('-'),-1:1;
	if(!n)*p++='0';else while(n)*p++=(char)(n%10*m+'0'),n/=10;while(p!=b)pcu(*--p);}
_OUTL(){out('\n');}
#ifdef _GLIBCXX_VECTOR
	_T _OUT(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);}
#endif
	_HT _OUT(H &&h, T... t){out(h);out(t...);}
	template <typename... T> _OUTL(T... t){out(t...);outl();}
	struct range{
		int e,b=0,s=1;range(int _b,int _e,int _s):e(_e),b(_b),s(_s){} range(int _b,int _e): e(_e), b(_b){} range(int _e):e(_e){}
		struct it{int v,s; it(int _v,int _s):v(_v),s(_s){} operator int()const{return v;} operator int&(){return v;}int operator*()const{return v;}
			it& operator++(){v+=s;return *this;} }; it begin(){return {b,s};} it end(){return {e,s};}};
#define times(i,n) for(int i=n;i;i--)

using P = pair<int, int>;
template <typename T>
using V = vector<T>;

int main() {
	for (int n; (n = in);) {
		V<P> v(n);
		for (P &i: v)
			i = {in, in};
		times (i, in)
			v.push_back(P{in, in});
		sort(begin(v), end(v));
		V<int> d(v.size(), 1);
		int r {};
		for (int i: range(v.size())) {
			for (int j: range(i))
				d[i] = max(d[i], v[j].first < v[i].first && v[j].second < v[i].second ? d[j] + 1: 1);
			r = max(r, d[i]);
		}
		outl(r);
	}
}

C++: #include<iostream>
#include<queue>
using namespace std;
#define MAX_N 1000
int x[MAX_N], y[MAX_N], X[MAX_N][MAX_N], dist[MAX_N];
int A, B, N;
int main() {
	while (true) {
		cin >> A; if (A == 0) { break; } for (int i = 0; i < A; i++) { cin >> x[i] >> y[i]; }
		cin >> B; for (int i = A; i < A + B; i++) { cin >> x[i] >> y[i]; }N = A + B;
		for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (x[i] < x[j] && y[i] < y[j]) { X[i][j] = 1; } } }
		queue<int>Q; int res = 0; for (int i = 0; i < N; i++) { dist[i] = 0; Q.push(i); }
		while (!Q.empty()) {
			int a1 = Q.front(); Q.pop();
			for (int i = 0; i < N; i++) {
				if (X[a1][i] == 1 && dist[a1] + 1 > dist[i]) { dist[i] = dist[a1] + 1; Q.push(i); }
			}
		}
		for (int i = 0; i < N; i++) { if (res < dist[i]) { res = dist[i]; } }cout << res << endl;
	}
}

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

using P = pair<int, int>;

int main() {
    int n;
    while(cin >> n, n) {
        vector<P> v(n);
        for(int i=0; i<n; ++i) {
            cin >> v[i].first >> v[i].second;
        }
        int m;
        cin >> m;
        for(int i=0; i<m; ++i) {
            int h, r;
            cin >> h >> r;
            v.emplace_back(h, r);
        }
        sort(v.begin(), v.end());
        vector<int> dp(n+m, 1);
        for(int i=0; i<n+m; ++i) {
            for(int j=0; j<i; ++j) {
                if(v[i].first > v[j].first && v[i].second > v[j].second) {
                    dp[i] = max(dp[i], dp[j]+1);
                }
            }
        }
        cout << *max_element(dp.begin(), dp.end()) << endl;
    }
}

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


int main()
{
	int n, m;
	int table[250];
	pair<int, int> data[250];

	while (cin >> n)
	{
		for (int i = 0; i < n; i++)
			cin >> data[i].first >> data[i].second;

		cin >> m;
		for (int i = 0; i < m; i++)
			cin >> data[n+i].first >> data[n+i].second;

		for (int i = 0; i < n + m; i++)
		{
			for (int j = i + 1; j < n + m; j++)
			{
				if (data[i].first > data[j].first)
					swap(data[i], data[j]);
			}
		}

		int ans = 0;
		for (int i = 0; i < n + m; i++)
		{
			table[i] = 1;
			for (int j = 0; j < i; j++)
			{
				if (data[i].first > data[j].first && data[i].second > data[j].second)
					table[i] = max(table[i], table[j] + 1);
			}
			ans = max(ans, table[i]);
		}

		cout << ans << endl;
	}

	return 0;
}