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;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)

const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

int main(){
    int n, m;
    while(cin >> n, n){
        vector<pair<int, int> > v;
        for(int i=0; i< n; i++){
            int h, r;
            cin >> h >> r;
            v.push_back(make_pair(h, r));
        }
        cin >> m;
        for(int i=0; i< m; i++){
            int h, r;
            cin >> h >> r;
            v.push_back(make_pair(h, r));
        }
        sort(v.begin(), v.end());
        int count = 0;
        int h = 0, r = 0;
        for(int i=0; i < v.size(); i++){
            if(h < v[i].first && r < v[i].second){
                h = v[i].first; r = v[i].second; count++;
            }
        }
        cout << count << endl;
    }
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <numeric>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<((x))<<endl
#define fi first
#define se second

#define INF 2147483600
#define long long long

bool sorter(const pair<int, int> p1, const pair<int, int> p2){
  if(p1.fi == p2.fi) return p1.se > p2.se;
  else return p1.fi < p2.fi;
}

int main(){
  int n;
  while(cin>>n, n){
    vector<pair<int, int> > vec(n);
    rep(i,n) scanf("%d %d", &vec[i].fi, &vec[i].se);
    int m;
    cin>>m;
    vec.resize(n+m);
    repl(i,n,n+m) scanf("%d %d", &vec[i].fi, &vec[i].se);
    sort(all(vec), sorter);
    vector<int> lis(n+m, INF);
    rep(i,n+m){
      int idx = lower_bound(all(lis), vec[i].se) - lis.begin();
      lis[idx] = vec[i].se;
    }
    cout << lower_bound(all(lis), INF) - lis.begin() << endl;
  }

  return 0;
}

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

typedef pair<int,int> pii;
int dptab[256][1024] = {0};

int main() {
	int n, m;
	while(cin >> n, n) {
		vector<pii> v;
		for(int i = 0; i < n; i++) {
			pii tmp;
			cin >> tmp.first >> tmp.second;
			v.push_back(tmp);
		}
		cin >> m;
		for(int i = 0; i < m; i++) {
			pii tmp;
			cin >> tmp.first >> tmp.second;
			v.push_back(tmp);
		}
		sort(v.begin(), v.end());

		for(int i = 0; i < 256; i++)
			for(int j = 0; j < 1024; j++)
				dptab[i][j] = 0;

		for(int i = 0; i < v.size(); i++) {
			for(int j = 0; j < 1024; j++) {
				if(i > 0 && v[i].first == v[i - 1].first) {
					dptab[i + 1][j]
						= max(dptab[i][j], v[i].second < j ? dptab[i - 1][v[i].second] + 1 : 0);
				}
				else {
					dptab[i + 1][j]
						= max(dptab[i][j], v[i].second < j ? dptab[i][v[i].second] + 1 : 0);
				}
			}
		}
		cout << dptab[n + m][1010] << endl;
	}
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <stack>
#include <utility>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cctype>
using namespace std;
 
#define rep(i,n) for(int i=0; i<n; i++)
#define repa(i,s,e) for(int i=s; i<=e; i++)
 
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

int n, m, h, r;
vector<pii> v;
int dp[201];

void solve() {
	sort(v.begin(), v.end());

	fill(dp, dp+201, 1);
	int l = v.size();

	rep(i,l) {
		repa(j,i+1,l-1) {
			if(v[i].first < v[j].first  && v[i].second < v[j].second) {
				dp[j] = max(dp[j], dp[i]+1);
			}
		}
	}

	int maxval = 0;
	rep(i,l) {
		maxval = max(maxval, dp[i]);
	}
	cout << maxval << endl;
}

int main() {
	while(cin>>n,n) {
		v.clear();
		rep(i,n) {
			cin>>h>>r;
			v.push_back(pii(h,r));
		}
		cin>>m;
		rep(i,m) {
			cin>>h>>r;
			v.push_back(pii(h,r));
		}
		solve();
	}
}

C++: #include <iostream>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF 1e8
#define EPS 1e-9

#define rep2(i,m,n) for(int i=m;i<n;i++)
#define rep(i,n) rep2(i,0,n)
#define ll long long
#define P pair<int,int>
#define pb push_back

int n,m,h[200],r[200],d[200][200];

int main(){
	while(cin>>n&&n){
		rep(i,n)cin>>h[i]>>r[i];
		cin>>m;
		rep(i,m)cin>>h[i+n]>>r[i+n];
		n+=m;
		rep(i,n)fill(d[i],d[i]+n,INF);
		rep(i,n)rep(j,n){
			if(i==j)d[i][j]=0;
			if(h[i]>h[j]&&r[i]>r[j])d[i][j]=-1;
		}
		rep(k,n)rep(i,n)rep(j,n)d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
		int ans=0;
		rep(i,n)rep(j,n)ans=max(ans,-d[i][j]+1);
		cout<<ans<<endl;
	}
}