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 <stdio.h>
#include <algorithm>
using namespace std;
int n, m, dp[2000]; pair<int, int> a[2000];
int main() {
	while(true) {
		scanf("%d", &n);
		if(n == 0) break;
		for(int i = 0; i < n; i++) scanf("%d%d", &a[i].first, &a[i].second);
		scanf("%d", &m);
		for(int i = 0; i < m; i++) scanf("%d%d", &a[n + i].first, &a[n + i].second);
		sort(a, a + n + m);
		int ret = 0;
		for(int i = 0; i < n + m; i++) dp[i] = -1;
		for(int i = 0; i < n + m; i++) {
			bool flag = false;
			for(int j = 0; j < ret; j++) {
				if(dp[j] >= a[i].second) {
					dp[j] = a[i].second; flag = true; break;
				}
			}
			if(!flag) dp[ret] = a[i].second, ret++;
		}
		//for(int i = 0; i < n + m; i++) printf("%d %d\n", a[i].first, a[i].second);
		//for(int i = 0; i < ret; i++) printf("%d\n", dp[i]);
		printf("%d\n", ret);
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
	while(cin >> n, n) {
		vector<pair<int, int> > v(n);
		for(int i = 0; i < n; i++) cin >> v[i].first >> v[i].second;
		cin >> m; v.resize(n + m);
		for(int i = 0; i < m; i++) cin >> v[n + i].first >> v[n + i].second;
		n += m;
		sort(v.begin(), v.end());
		vector<int> dp(n + 1);
		for(int i = 1; i <= n; i++) {
			dp[i] = 1;
			for(int j = 1; v[j - 1].first < v[i - 1].first; j++) {
				if(v[j - 1].second < v[i - 1].second) {
					dp[i] = max(dp[i], dp[j] + 1);
				}
			}
		}
		cout << *max_element(dp.begin(), dp.end()) << endl;
	}
}

C++: #include <iostream>
#include <list>
#include <algorithm>
#define F first
#define S second
using namespace std;

typedef pair<int,int> P;

int main(void){
	P dolls[200];
	int n,m;
	while(cin >> n,n){
		for(int i=0;i<n;i++)
			cin >> dolls[i].F >> dolls[i].S;
		cin >> m;
		for(int i=n;i<n+m;i++)
			cin >> dolls[i].F >> dolls[i].S;
		sort(dolls,dolls+n+m);
		int cou=0,cx=0,cy=0;
		for(int i=0;i<n+m;i++)
			if(cx<dolls[i].F&&cy<dolls[i].S)
				cx=dolls[i].F,cy=dolls[i].S,cou++;
		cout << cou<< endl;
	}
	return 0;
}

C++: /* {{{ Shinobu kawaii */
/*
  ______   __        _                  __                 
.' ____ \ [  |      (_)                [  |                
| (___ \_| | |--.   __   _ .--.   .--.  | |.--.   __   _   
 _.____`.  | .-. | [  | [ `.-. |/ .'`\ \| '/'`\ \[  | | |  
| \____) | | | | |  | |  | | | || \__. ||  \__/ | | \_/ |, 
 \______.'[___]|__][___][___||__]'.__.'[__;.__.'  '.__.'_/ 

*/
/* }}} */

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

// #define int long long
struct Fast {Fast(){std::cin.tie(0);ios::sync_with_stdio(false);}} fast;

/* cpp template {{{ */

/* short */
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()
#define X real()
#define Y imag()

/* REPmacro */
#define REPS(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (int i = (a); i >= n; --i)
#define DEP(i, n) DEPS(i, n, 0)

/* debug */
#define debug(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n';

/* alias */
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using D = double;
using P = complex<D>;

/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const D EPS = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

/* func */
inline bool inside(int y, int x, int H, int W) {return y >= 0 && x >= 0 && y < H && x < W;}
inline int in() {int x; std::cin >> x; return x;}
template <typename T> void print(T x) {std::cout << x << '\n';}
template <typename T>
void print(std::vector<T>& v, std::string s = " ") {
  REP(i, v.size()) {
    if (i != 0) std::cout << s;
    std::cout << v[i];
  }
  std::cout << '\n';
}

/* }}} */

signed main()
{
  for (int n, m; n = in(), n; ) {
    vector<pii> v;
    REP(i, n) v.pb(mp(in(), in()));
    m = in();
    REP(i, m) v.pb(mp(in(), in()));
    vi dp(v.size(), 0);

    sort(ALL(v));
    int ans = -INF;
    REP(i, v.size()) {
      dp[i] = 1;
      REP(j, i) {
        if (v[j].Fi < v[i].Fi && v[j].Se < v[i].Se) {
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
      ans = max(ans, dp[i]);
    }
    print(ans);
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef pair<int, int> P;

int dp[205];

int main()
{

	int n, m;
	while ( cin >> n && n ){
		vector <P> d;
		d.clear();
		memset (dp, 0, sizeof (dp ) );
		rep (i, n ){
			int h, r;
			cin >> h >> r;
			d.push_back (P (h, r ) );
		} // end rep 
		cin >> m;
		rep (i, m ){
			int h, r;
			cin >> h >> r;
			d.push_back (P (h, r ) );
		} // end rep
		sort (ALL (d ) );

		int res = 0;
		rep (i, n+m ){
			dp[i] = 1;
			rep (j, i ){
				if (d[j].first < d[i].first && d[j].second < d[i].second ) {
					dp[i] = max (dp[i], dp[j]+1 );
				} // end if
			} // end rep
			res = max (res, dp[i] );
		} // end rep
		cout << res << endl;
	} // end loop

	return 0;
}