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 <list>
#include <algorithm>
#define INF 10e6
#define F first
#define S second
using namespace std;

typedef pair<int,int> P;

int main(void){
	P dolls[200];
	P dolls1[200];
	int dp[200];
	int dp1[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;
		for(int i=0;i<n+m;i++)
			dolls1[i].F=dolls[i].S,dolls1[i].S=dolls[i].F;
		sort(dolls,dolls+n+m);
		sort(dolls1,dolls1+n+m);
		int cx,cy;
		fill(dp,dp+n+m,INF);
		fill(dp1,dp1+n+m,INF);
		for(int i=0;i<n+m;i++){
			*lower_bound(dp,dp+n+m,dolls[i].S)=dolls[i].S;
			*lower_bound(dp1,dp1+n+m,dolls1[i].S)=dolls1[i].S;
		}
		cx=lower_bound(dp,dp+n+m,INF)-dp;
		cy=lower_bound(dp1,dp1+n+m,INF)-dp1;
		cout << max(cx,cy) << endl;
	}
	return 0;
}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int f(int r[],int t[],int s[],int d[],int n,int c,int m){
  int i;
  int l=m;
  if(d[c]<m){
    d[c]=m;
    for(i=c+1;i<n;i++){
      if(r[i]<r[c]&&t[i]<t[c])
	l=max(l,f(r,t,s,d,n,i,m+1));
    }
    return l;
  }else
    return 0;
}  
int main(){
  int h,i;
  int m,n,o,r[200],t[200],s[200],d[200];
  for(;;){
    cin>>m;
    if(m==0)
      break;
    for(i=0;i<m;i++){
      cin>>t[i]>>r[i];
      s[i]=r[i]*r[i]*t[i];
    }
    cin>>n;
    for(;i<m+n;i++){
      cin>>t[i]>>r[i];
      s[i]=r[i]*r[i]*t[i];
    }
    do{
      h=0;
      for(i=1;i<m+n;i++){
	if(s[i-1]<s[i]){
	  swap(s[i-1],s[i]);
	  swap(t[i-1],t[i]);
	  swap(r[i-1],r[i]);
	  h=1;
	}
      }
    }while(h);
    o=0;
    memset(d,0,sizeof(d));
    for(i=0;i<m+n;i++)
      o=max(o,f(r,t,s,d,m+n,i,1));
    cout<<o<<endl;
  }
  return 0;
}

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

using P = std::pair<int, int>;

int n;

P dolls[200];

int dp[200];

int main() {
	while (1) {
		int m;
		std::cin >> n;
		if (n == 0)
			break;
		for (int i = 0; i < n; ++i)
			std::cin >> dolls[i].first >> dolls[i].second;
		std::cin >> m;
		for (int i = n; i < n + m; ++i)
			std::cin >> dolls[i].first >> dolls[i].second;
		n += m;
		std::sort(dolls, dolls + n);
		std::fill(dp, dp + n, 1);
		for (int i = 1; i < n; ++i) {
			for (int j = i - 1; j >= 0; --j) {
				if (dolls[i].first > dolls[j].first&&
					dolls[i].second > dolls[j].second) {
					dp[i] = std::max(dp[j] + 1, dp[i]);
				}
			}
		}
		int ans = 0;
		for (int i = 0; i < n; ++i)
			ans = std::max(ans, dp[i]);
		std::cout << ans << std::endl;
	}
	return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
#include <list>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

int main() {
	int n;
	while(cin >> n, n) {
		vvi h(n, vi(2));
		REP(i, n) {
			REP(j, 2) {
				cin >> h[i][j];
			}
		}

		int m;
		cin >> m;
		vvi hr(n+m, vi(2));
		REP(i, n) {
			REP(j, 2) {
				hr[i][j] = h[i][j];
			}
		}
		REP(i, m) {
			REP(j, 2) {
				cin >> hr[n+i][j];
			}
		}

		sort(ALL(hr));
		vi dp(n+m, 1);
		int res = 0;
		REP(i, n+m) {
			REP(j, i) {
				if(hr[j][0] < hr[i][0] && hr[j][1] < hr[i][1]) {
					dp[i] = max(dp[i], dp[j]+1);
				}
			}
			res = max(res, dp[i]);
		}

		cout << max(res, max(n, m)) << endl;;
	}
}

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

struct Doll{
	int h,r;
	bool operator <(const Doll &a)const{
		return h<a.h;
	}
};

int main(){
	int N,M;
	while(cin>>N,N){
		vector<Doll> d;
		for(int i=0;i<N;i++){
			Doll in; cin>>in.h>>in.r; d.push_back(in);
		}
		cin>>M;
		for(int i=0;i<M;i++){
			Doll in; cin>>in.h>>in.r; d.push_back(in);
		}

		sort(d.begin(),d.end());
		int dp[201][1000] = {}, ans = 0;
		for(int i=0,cnt=0; cnt<d.size(); cnt++){
			if(cnt!=0 && d[cnt-1].h!=d[cnt].h) i++;
			for(int j=0;j<1000;j++){
				dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
				if(j<d[cnt].r)
					ans = max(ans, dp[i+1][d[cnt].r] = max(dp[i+1][d[cnt].r], dp[i][j]+1));
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

C++: // 0157 最長増加部分列
#include <iostream>
#include <algorithm>
#include <map>

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

using namespace std;
bool compare_h(pair<int, int> a, pair<int, int> b){
	return a.first < b.first;
}
bool compare_r(pair<int, int> a, pair<int, int> b){
	return a.second < b.second;
}


int main(){
	int n, m;
	while(cin >> n && n){
		int dp[256];
		// high, r
		pair<int, int> dolls[256];
		rep(i, n){
			cin >> dolls[i].first >> dolls[i].second;
		}
		cin >> m;
		rep(i, m){
			cin >> dolls[i+n].first >> dolls[i+n].second;
		}


		// hの最長増加部分列
		// hについてソートしrの最長増加部分列を求める
		int hlis = 0;
		sort(dolls, dolls+n+m, compare_h);
		dp[0] = 1;
		for(int i = 1; i < n+m; i++){
			dp[i] = 1;
			for(int j = 0; j < i; j++){
				if(dolls[j].second < dolls[i].second && dolls[j].first != dolls[i].first){
					dp[i] = max(dp[i], dp[j] + 1);
				}
			}
			hlis = max(hlis, dp[i]);
		}					
		
		// rの最長増加部分列
		// rについてソートしhの最長増加部分列を求める
		int rlis = 0;
		sort(dolls, dolls+n+m, compare_r);
		dp[0] = 1;
		for(int i = 1; i < n+m; i++){
			dp[i] = 1;
			for(int j = 0; j < i; j++){
				if(dolls[j].first < dolls[i].first && dolls[j].second != dolls[i].second){
					dp[i] = max(dp[i], dp[j] + 1);
				}
			}
			rlis = max(rlis, dp[i]);
		}

		cout << max(rlis, hlis) << endl;
	}

	return 0;
}