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 <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))
#define ALL(a) (a).begin(),(a).end()
const int INF = 1e9;
int dx[4]={0, 1, 0, -1}, dy[4]={-1, 0, 1, 0};
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;

/* struct P {
	int x, y, n;
	P(int n, int x, int y):n(n), x(x), y(y){}
	P(){}
}; */

/** Problem0157 : Russian Dolls **/

bool cmp(const PII &a, const PII &b)
{
	return a.first > b.first;
}

bool cmp2(const PII &a, const PII &b)
{
	return a.second > b.second;
}

int main()
{
	int N, M;
	while (cin>>N, N) {
		vector<PII> dolls;
		rep(i, N) {
			int a, b;
			cin>>a>>b;
			dolls.emplace_back(a, b);
		}
		cin>>M;
		rep(i, M) {
			int a, b;
			cin>>a>>b;
			dolls.emplace_back(a, b);
		}
		N += M;
		
		int ans=0;
		vector<int> dp(N);
		sort(dolls.begin(), dolls.end(), cmp);
		for (int i=0; i<N; i++) {
			dp[i] = 1;
			for (int j=0; j<i; j++) {
				if (dolls[i].first < dolls[j].first && dolls[i].second < dolls[j].second)
					dp[i] = max(dp[j]+1, dp[i]);
			}
		}
		ans = max(ans, dp[N-1]);
		
		fill(dp.begin(), dp.end(), 0);
		sort(dolls.begin(), dolls.end(), cmp2);
		for (int i=0; i<N; i++) {
			dp[i] = 1;
			for (int j=0; j<i; j++) {
				if (dolls[i].first < dolls[j].first && dolls[i].second < dolls[j].second)
					dp[i] = max(dp[j]+1, dp[i]);
			}
		}
		ans = max(ans, dp[N-1]);
		
		cout << ans << endl;
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
#include <list>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define F first
#define S second


int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

typedef struct{
  int h,r;
}mat;

bool lessh(const mat&l,const mat&r){return l.h<r.h;};
bool lessr(const mat&l,const mat&r){return l.r<r.r;};


int low[200],ans;
mat hr[200];

main(){
  int n;
  while(cin>>n,n){
    rep(i,n)cin>>hr[i].h>>hr[i].r;
    int m;
    cin>>m;
    rep(i,m)cin>>hr[i+n].h>>hr[i+n].r;

    
    memset(low,0,sizeof(0));

    sort(hr,hr+m+n,lessh);
    stable_sort(hr,hr+m+n,lessr);

    int ans=0;
    rep(i,n+m){
      rep(j,i){
	if(hr[j].h<hr[i].h && hr[j].r<hr[i].r){
	  low[i]=max(low[i],low[j]+1);
	  ans=max(ans,low[i]);
	}
      }
    }

    cout<<ans+1<<endl;
  }
}

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

struct Pair {
	int a, b;
	Pair(int _a = 0, int _b = 0){ a = _a; b = _b; }
	bool operator < (const Pair &t) const {
		return a < t.a && b < t.b;
	}
};

int main()
{
	int n;
	while (scanf("%d", &n), n){
		vector<Pair> v;
		for (int i = 0; i < n; i++){
			int x, y;
			scanf("%d %d", &x, &y);
			v.push_back(Pair(x, y));
		}
		scanf("%d", &n);
		for (int i = 0; i < n; i++){
			int x, y;
			scanf("%d %d", &x, &y);
			v.push_back(Pair(x, y));
		}
		sort(v.begin(), v.end());
		
		vector<Pair> l(v.size());
		l[0] = v[0];
		int len = 1;
		for (int i = 1; i < v.size(); i++){
			if (l[len - 1] < v[i]){
				l[len++] = v[i];
			}
			else {
				*lower_bound(l.begin(), l.begin() + len, v[i]) = v[i];
			}
		}
		printf("%d\n", len);
	}
	return 0;
}

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

typedef std::pair<int,int> P;

int dp[200];//何番目のまとりょーしかが一番大きいか＝数

int main(){
	int ichiro,jiro,M=0;
	while(true){
		P doll[200];//高さ　半径
		std::cin>>ichiro;
		if(!ichiro){
			break;
		}
		for(int i=0;i<ichiro;i++){
			std::cin>>doll[i].first>>doll[i].second;
		}
		std::cin>>jiro;
		for(int i=0;i<jiro;i++){
			std::cin>>doll[i+ichiro].first>>doll[i+ichiro].second;
		}
		std::sort(doll,doll+ichiro+jiro);

		for(int i=0;i<ichiro+jiro;i++)dp[i]=1;
		for(int i=1;i<ichiro+jiro;i++){
			for(int j=i-1;j>=0;j--){
				dp[i]=std::max(dp[i],dp[j]+(doll[i].first!=doll[j].first&&doll[i].second>doll[j].second));
			}
			M=std::max(M,dp[i]);
		}
		std::cout<<M<<std::endl;
		M=0;
		for(int i=0;i<ichiro+jiro;i++)dp[i]=0;
	}
return 0;
}

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

#define int				long long
#define FOR( i, m, n )	for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )		FOR( i, 0, n )
#define REPR( i, m )	for( int (i) = (m); (i) >= 0; (i)-- )
#define REPONE( i, n )	FOR( i, 1, n + 1 )
#define ALL( a )		(a).begin(), (a).end()
#define MP				make_pair

typedef pair<int, int> P;

template<class T>bool chmax( T& a, const T& b ) { if( a < b ) { a = b; return 1; } return 0; }
template<class T>bool chmin( T& a, const T& b ) { if( a > b ) { a = b; return 1; } return 0; }

const int INF = 1e9;
const int MOD = 1e9 + 7;

signed main() {
	int N, M;
	while( cin >> N, N ) {
		P mato[300] = {};
		int dp[300] = {};

		int cnt = 0;
		REP( i, N ) {
			cin >> mato[cnt].first >> mato[cnt].second;
			cnt++;
		}
		cin >> M;
		REP( i, M ) {
			cin >> mato[cnt].first >> mato[cnt].second;
			cnt++;
		}

		sort( mato, mato + 300, greater<>() );

		REP( i, N + M ) {
			dp[i] = 1;
			REP( j, i ) {
				if( mato[j].first > mato[i].first && mato[j].second > mato[i].second ) {
					chmax( dp[i], dp[j] + 1 );
				}
			}
		}

		cout << dp[N + M -1] << endl;
	}
}