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++: #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){
		if(p1.first < p2.first) return true;
		else if(p1.second < p2.second) return true;
		else false;
	}
};

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* dp = new int[n+m];
		memset(dp,0,sizeof(dp));
		for(int i=0;i<n+m;i++) dp[i] = 1;

		for(int i=0;i<dolls.size();i++){
			for(int j=0;j<dolls.size();j++){
				if(dolls[j].first < dolls[i].first
					&& dolls[j].second < dolls[i].second){
						dp[i] = max(dp[i],dp[j] + 1);
				}
			}
		}

		cout << dp[dolls.size()-1] << endl;
		delete[] dp;
	}
}

C++: //Solution for aoj:0157 Russian Dolls
#include<iostream>
#include<algorithm>
using namespace std;
typedef pair<int, int>P;
const int INF = 1 << 30;
int n, m, p;
pair<int, int> doll[201];
int dp[2][201], dp2[201];
int main(){
	while (cin >> n){
		if (!n)
			return 0;
		fill(dp[0], dp[0] + 201, INF);
		fill(dp[1], dp[1] + 201, INF);
		fill(dp2, dp2 + 201, INF);
		for (int i = 1; i <= n; i++){
			int a, b;
			cin >> a >> b;
			doll[i].first = a;
			doll[i].second = b;
		}
		cin >> m;
		for (int i = n + 1; i <= n + m; i++){
			int a, b;
			cin >> a >> b;
			doll[i].first = a;
			doll[i].second = b;
		}
		sort(doll + 1, doll + 1 + n + m);
		for (int i = 1; i <= n + m; i++){
			p = lower_bound(dp[0] + 1, dp[0] + n + m + 1, doll[i].first) - dp[0];
			if (dp[1][p] > doll[i].second){
				dp[0][p] = doll[i].first;
				dp[1][p] = doll[i].second;
			}
		}
		for (int i = 1; i <= n + m; i++){
			if (dp[1][i] == INF) break;
			*lower_bound(dp2 + 1, dp2 + n + m + 1, dp[1][i]) = dp[1][i];
		}
		cout << lower_bound(dp2 + 1, dp2 + n + m + 1, INF) - dp2 - 1 << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> mp;
#define inf 1e9


int main(){
    while(1){
	int n;
	cin>>n;
	if(n==0)break;
	stack<mp> s1;
	int hm=0,rm=0;
	for(int i=0;i<n;i++){
		int h,r;
		cin>>h>>r;
		s1.push(mp(h,r) );
		hm = max(hm,h);
		rm = max(rm,r);
	}
	int m;
	cin>>m;
	stack<mp> s2;
	for(int i=0;i<m;i++){
		int h,r;
		cin>>h>>r;
		s2.push(mp(h,r) );
		hm = max(hm,h);
		rm = max(rm,r);
	}
	vector<vector<int> > dp(hm+2 , vector<int>(rm+2,0) );
	while(!s1.empty()){
		mp now = s1.top();
		s1.pop();
		dp[now.first][now.second] = 1;
	}
	while(!s2.empty()){
		mp now = s2.top();
		s2.pop();
		dp[now.first][now.second] = 1;
	}
	for(int i=hm;i>=0;i--){
		for(int j=rm;j>=0;j--){
		    if(dp[i][j] == 1) dp[i][j] = dp[i+1][j+1] + 1;
			dp[i][j] =  max(dp[i][j], max(dp[i+1][j],dp[i][j+1]) ) ;
		}
	}
	cout<<dp[0][0]<<endl;
	}

	return 0;
}

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

using namespace std;

vector< pair<int,int> > doll;
int dp[200];
int n,m,tmp1,tmp2;

int main(){
  while(cin>>n && n){
    for(int i=0;i<n;i++){
      cin>>tmp1>>tmp2;
      doll.push_back(make_pair(tmp1,tmp2));
    }
    cin>>m;
    for(int i=0;i<m;i++){
      cin>>tmp1>>tmp2;
      doll.push_back(make_pair(tmp1,tmp2));
    }

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

    int ans=0;

    for(int i=0;i<n+m;i++){
      dp[i]=1;
      for(int j=0;j<n+m;j++){
        if(doll[i].first>doll[j].first && doll[i].second>doll[j].second){
          dp[i]=max(dp[i],dp[j]+1);
        }
      }
      ans=max(ans,dp[i]);
    }

    cout<<ans<<endl;

    while(doll.size()) doll.pop_back();
    memset(dp,0,sizeof(dp));
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
main(){
  int n;
  while(cin>>n,n){
    vector<pair<int,int> >v;
    for(int i=0;i<n;i++){
      int p1,p2;
      cin>>p1>>p2;
      v.push_back(make_pair(p2,p1));
    }
    int m,pp=0;
    cin>>m;
    for(int i=0;i<m;i++){
      int p1,p2;
      cin>>p1>>p2;
      v.push_back(make_pair(p2,p1));
    }
    sort(v.begin(),v.end(),greater<pair<int,int> >());
    for(int i=0;i<m+n;i++)
      swap(v[i].second,v[i].first);
    stable_sort(v.begin(),v.end(),greater<pair<int,int> >());
    for(int k=0;k<n+m;k++){
    int sum=1,mh=v[k].first,mw=v[k].second;
    for(int i=k;i<n+m;i++){
      if(v[i].second<mw&&v[i].first<mh){
        mh=v[i].first;
        mw=v[i].second;
        sum++;
      }
    }
    pp=max(sum,pp);
    }
    cout<<pp<<endl;
  }
}