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<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<cstdlib>
using namespace std;

struct ppp {
  int h;
  int r;
};

int tansaku(vector<ppp> c, vector<ppp> d, int cN, int dN, int ans, int cy, int dy) {
  if(dN >= d.size()) {
    for(int i = cN; i < c.size(); i++) {
      cout << dy << endl;
      if(d[dy].r < c[i].r && d[dy].h < c[i].h) {
        ans++;
        d[dy] = c[i];
      }
    }
    return ans;
  }
  if(cN >= c.size()) {
    for(int i = dN; i < d.size(); i++) {
      if(d[i].r > c[cy].r && d[i].h > c[cy].h) {
        ans++;
        c[cy] = d[i];
      }
    }
    return ans;
  }
  if(c[cN].h == d[dN].h) {
    return c[cN].r > d[dN].r ? tansaku(c, d, cN + 1, dN + 1, ans + 1, cy, dN) :
    tansaku(c, d, cN + 1, dN + 1, ans + 1, cN, dy);
  }
  if(c[cN].r == d[dN].r) {
    return c[cN].h > d[dN].h ? tansaku(c, d, cN + 1, dN + 1, ans + 1, cy, dN) :
    tansaku(c, d, cN + 1, dN + 1, ans + 1, cN, dy);
  }
  if(c[cN].h < d[dN].h && c[cN].r < d[dN].r) {
    return tansaku(c, d, cN + 1, dN, ans + 1, cN, dy);
  }
  if(c[cN].h > d[dN].h && c[cN].r > d[dN].r) {
    return tansaku(c, d, cN, dN + 1, ans + 1, cy, dN);
  }
  if((c[cN].h < d[dN].h && c[cN].r > d[dN].r) || (c[cN].h > d[dN].h && c[cN].r < d[dN].r)) {
    return max(tansaku(c, d, cN + 1, dN, ans + 1, cN, dy), tansaku(c, d, cN, dN + 1, ans + 1, cy, dN));
  }
  return ans;
}

int main() {
  int a, b;
  cin >> a;
  vector<ppp> c(a);
  for(int i = 0; i < a; i++) {
    cin >> c[i].h >> c[i].r;
  }
  cin >> b;
  vector<ppp> d(b);
  for(int i = 0; i < b; i++) {
    cin >> d[i].h >> d[i].r;
  }
  cout << tansaku(c, d, 0, 0, 0, 0, 0) << endl;
}

C++: #include<bits/stdc++.h>

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

using namespace std;

const int MAX = 300;

typedef pair<int,int> ii;
int n,m,dp[MAX];
ii input[MAX];

void compute(){
  sort(input,input+(n+m));
  memset(dp,0,sizeof(dp));
  rep(i,n+m){
    rep(j,i){
      if( input[i].first > input[j].first && input[i].second > input[j].second ) {
	dp[i] = max(dp[i],dp[j]+1);
      }
    }
  }
  int maxi = 0;
  rep(i,n+m) maxi = max(maxi,dp[i]);
  printf("%d\n",maxi+1);
}

int main(){
  while( cin >> n, n ){
    rep(i,n) cin >> input[i].first >> input[i].second;
    cin >> m;
    rep(i,m) cin >> input[n+i].first >> input[n+i].second;
    compute();
  }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

struct RussianDoll
{	int h, r; RussianDoll(int h, int r) : h(h), r(r) { }};

bool operator < (RussianDoll left, RussianDoll right)
{	return left.h < right.h && left.r < right.r;	}

struct Edge
{	int from, to, cost;	Edge(int from, int to, int cost) : from(from), to(to), cost(cost) { } };

void input(vector<RussianDoll>& v, int n)
{
	rep(i, n)
	{
		int h, r;
		cin >> h >> r;
		v.push_back(RussianDoll(h, r));
	}
}

int Bellmanford(int n, const vector<Edge>& v)
{
	vint d(n, 0);
	
	bool update = true;
	while(update)
	{
		update = false;
		rep(i, v.size())
		{
			if(d[v[i].from] + v[i].cost < d[v[i].to])
			{
				d[v[i].to] = d[v[i].from] + v[i].cost;
				update = true;
			}
		}
	}
	
	return d[n - 1];
}

int main()
{
    int n;
	while(cin >> n && n)
	{
		vector<RussianDoll> v;
		vector<Edge> edges;
		
		input(v, n);
		
		cin >> n;
		input(v, n);
		
		n = v.size();
		
		rep(i, n) edges.push_back(Edge(0, i + 1, -1)), edges.push_back(Edge(i + 1, n + 1, 0));
		rep(i, n) rep(j, n) if(v[i] < v[j]) edges.push_back(Edge(i + 1, j + 1, -1));
			
		cout << -Bellmanford(n + 2, edges) << endl;
	}
}

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

using namespace std;

int main() {
  
  int N;
  while(cin >> N && N) {
    
    vector<pair<int, int> > v1, v2;
    
    for(int i=0; i<N; i++) {
      int h, r; cin >> h >> r;
      v1.push_back(make_pair(h, r));
    }

    int M; cin >> M;
    
    for(int i=0; i<M; i++) {
      int h, r; cin >> h >> r;
      v1.push_back(make_pair(h, r));
    }
    
    sort(v1.begin(), v1.end());
    
    int size = N+M;
    int dp1[size];
    int ans = 1;
    for(int i=0; i<size; i++) {
      dp1[i] = 1;
      for(int j=0; j<i; j++)
        if(v1[i].first > v1[j].first &&
           v1[i].second > v1[j].second) { dp1[i] = max(dp1[i], dp1[j]+1); }
      ans = max(ans, dp1[i]);
    }
    
    cout << ans << endl;
    
  }
  
  return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 10e-8;

class mycmp{
public:
	bool operator()(const P p1, const P p2){
		return (p1.first < p2.first && p1.second < p2.second);
	}
};

int main(){
	int n;

	while(~scanf("%d",&n)){
		if(n==0) break;
		vector<P> dolls;

		for(int i=0;i<n;i++){
			int h,r;
			scanf("%d %d",&h,&r);
			dolls.push_back(P(h,r));
		}

		int m;
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			int h,r;
			scanf("%d %d",&h,&r);
			dolls.push_back(P(h,r));
		}


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

		int ans = 1;
		for(int i=0;i<dolls.size();i++){
			for(int j=i+1;j<dolls.size();j++){
				if(dolls[i].first < dolls[j].first
					&& dolls[i].second < dolls[j].second){
						ans++;
						i=j-1;
						break;
				}
			}
		}

		cout << ans << endl;
	}
}