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>
using namespace std;

int n, m;
int d[200][2], dp[200];

int main(){
    for(;;){
        if(cin >> n){
            for(int i = 0; i < 200; i++){
                dp[i] = 0;
            }
            if(n == 0){
                break;
            }
            for(int i = 0; i < n; i++){
                cin >> d[i][0] >> d[i][1];
                for(int j = i; j > 0; j--){
                    if(d[j][0] > d[j - 1][0]){
                        break;
                    }else{
                        swap(d[j][0], d[j - 1][0]);
                        swap(d[j][1], d[j - 1][1]);
                    }
                }
            }
            cin >> m;
            for(int i = n; i < n + m; i++){
                cin >> d[i][0] >> d[i][1];
                for(int j = i; j > 0; j--){
                    if(d[j][0] > d[j - 1][0]){
                        break;
                    }else{
                        swap(d[j][0], d[j - 1][0]);
                        swap(d[j][1], d[j - 1][1]);
                    }
                }
            }
            int maax = 0;
            for(int i = 0; i < n + m; i++){
                dp[i]++;
                int a = 0;
                for(int j = 0; j < i; j++){
                    if(a <= dp[j]){
                        if(d[j][1] < d[i][1]){
                            a = dp[j];
                        }
                    }
                }
                dp[i] += a;
                if(maax <= dp[i]){
                    maax = dp[i];
                }
            }
            cout << maax << endl;
        }else{
            break;
        }
    }
    return 0;
}

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, ans, p, s, s2, s3;
pair<int,int> doll[201];
int dp[2][201],dp2[201];
int d[2][1000];
int main(){
	while (cin >> n){
		if (!n)
			return 0;
		fill(dp[0], dp[0] + 1000, INF);
		fill(dp[1], dp[1] + 1000, 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++){
			*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<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
int dp[210];
pair<int,int> a[210];
int main(){
  int n,m;
  while(cin>>n&&n){
    for(int i=0;i<n;i++){
      int x,y;cin>>x>>y;
      a[i]=(make_pair(x,y));
      dp[i]=mod;
    }
    cin>>m;
    for(int i=0;i<m;i++){
      int x,y;cin>>x>>y;
      a[i+n]=(make_pair(x,y));
      dp[i+n]=mod;
    }
    dp[n+m]=mod;
    sort(a,a+n+m);
    for(int i=0;i<n+m;i++){
      int it=lower_bound(dp,dp+n+m,a[i].second)-dp;
      dp[it]=a[i].second;
    }
    int ans=0;
    while(true){
      if(dp[ans]==mod)break;
      ans++;
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
const int MAX_NM = 200;

struct M{
	int r, h;
};

bool ascending_M_r(const M& l, const M& r){
	return l.r < r.r;
}

bool ascending_M_h(const M& l, const M& r){
	return l.h < r.h;
}

int main(){
	int n, m, dp[MAX_NM];
	M a[MAX_NM];
	while(std::cin >> n, n){
		for(int i=0;i<n;i++){
			std::cin >> a[i].h >> a[i].r;
		}
		std::cin >> m;
		for(int i=0;i<m;i++){
			std::cin >> a[n+i].h >> a[n+i].r;
		}
	
		int r_res = 0, h_res = 0;//hの最長増加部分列とrの最長増加部分列
		std::sort(a, a+n+m, ascending_M_r);

		dp[0] = 1;
		for(int i=1;i<n+m;i++){
			dp[i] = 1;
			for(int j=0;j<i;j++){
				if(a[j].r < a[i].r && a[j].h < a[i].h){
					dp[i] = std::max(dp[i], dp[j]+1);
				}
			}
		}
		r_res = dp[n+m-1];

		std::sort(a, a+n+m, ascending_M_h);
		dp[0] = 1;
		for(int i=1;i<n+m;i++){
			dp[i] = 1;
			for(int j=0;j<i;j++){
				if(a[j].r < a[i].r && a[j].h < a[i].h){
					dp[i] = std::max(dp[i], dp[j]+1);
				}
			}
		}
		h_res = dp[n+m-1];

		printf("%d\n", std::max(h_res, r_res));
	}
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
constexpr int MOD = 1000000007;
constexpr int INF = 2000000000;
using namespace std;


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

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, ans, p, s, s2, s3;
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++){
			*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;
	}
}