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

int main(void) {
    int n,m,a,b;
    while(cin >> n && n){
        vector<pair<int,int> > v;
        for(int i = 0; i < n; i++){
            cin >> a >> b;
            v.push_back(make_pair(a,b));
        }
        cin >> m;
        for(int i = 0; i < m; i++){
            cin >> a >> b;
            v.push_back(make_pair(a,b));
        }
        sort(v.begin(), v.end());

        int sz = v.size();
        vector<int> dp(sz, 0);
        int res = 0;
        for(int i = 0; i < sz; i++){
            int mx = 0;
            for(int j = 0; j < i; j++){
                if(v[j].first < v[i].first && v[j].second < v[i].second){
                    mx = max(mx, dp[j]);
                }
            }
            dp[i] = mx+1;
            res = max(res, dp[i]);
        }
        cout << res << endl;
    }
	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 << 29;
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 = 0; i < n; i++){
			int a, b;
			cin >> a >> b;
			doll[i].first = a;
			doll[i].second = b;
		}
		cin >> m;
		for (int i = n; i < n + m; i++){
			int a, b;
			cin >> a >> b;
			doll[i].first = a;
			doll[i].second = b;
		}
		sort(doll, doll + n + m);
		for (int i = 0; i < n + m; i++){
			p = lower_bound(dp[0], dp[0] + n + m, 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 = 0; i < n + m; i++){
			if (dp[1][i] == INF) break;
			*lower_bound(dp2, dp2 + n + m, dp[1][i]) = dp[1][i];
		}
		cout << lower_bound(dp2, dp2 + n + m, INF) - dp2 << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef pair<int,int> P;

int main(){
	int n,m,h,r;
	vector<P> mat;
	vector<int> l;
	while(cin >> n,n){
		for(int i = 0;i < n;i++) {
			cin >> h >> r;
			mat.push_back(P(h,r));
		}
		cin >> m;
		for(int i = n;i < n + m;i++){
			cin >> h >> r;
			mat.push_back(P(h,r));
		}
		sort(mat.begin(),mat.end());
		for(int i = n + m - 1;i > 0;i--){
			if(mat[i].first == mat[i - 1].first) mat.erase(mat.begin() + i);
		}
		for(int i = 0;i < mat.size();i++){
			if(l.empty()) l.push_back(mat[i].second);
			else{
				if(l.back() < mat[i].second) l.push_back(mat[i].second);
				else l[lower_bound(l.begin(),l.end(),mat[i].second) - l.begin()] = mat[i].second;
			}
		}
		cout << l.size() << endl;
		l.clear();
		mat.clear();
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;


int n, m;
vector<pair<int, int>> d;
vector<int> dp = { 0 };


int solve(){
	dp[0] = d[0].second;
	for (int i = 1; i < d.size(); i++){
		if (dp.back() < d[i].second){
			dp.push_back(d[i].second);
		}
		else{
			*lower_bound(dp.begin(), dp.end(), d[i].second) = d[i].second;
		}
	}
	return dp.size();
}


template<typename T>
void clear(vector<T>& v){
	vector<T> emp;
	swap(v, emp);
}


int main(){
	while (cin >> n, n){
		for (int i = 0; i < n; i++){
			int h, r;
			cin >> h >> r;
			d.push_back({ h, r });
		}
		cin >> m;
		for (int i = 0; i < m; i++){
			int h, r;
			cin >> h >> r;
			d.push_back({ h, r });
		}

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

		int ans = solve();

		cout << ans << endl;

		clear(d);
		clear(dp);
		dp.push_back(0);
	}

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int N;

int m[200][2], dp[200];
vector<pair<pair<int, int>, int>> m1, m2;
vector<int> pa[200];


int dfs(int x)
{
  if(dp[x] != -1)
    return dp[x];
  int r = 0;
  for(auto v: pa[x])
    r = max(r, dfs(v));
  return dp[x] = r + 1;
}

int main()
{
  while(true)
  {
    fill(dp, dp + 200, -1);
    for(int i = 0; i < 200; i++)
      pa[i].clear(), (m[i][0] = m[i][1] = 0);
    m1.clear(); m2.clear();

    N = 0;
    for(int i = 0; i < 2; i++)
    {
      int a;
      cin >> a;
      if(!a)
        return 0;
      for(int j = N; j < a + N; j++)
      {
        cin >> m[j][0] >> m[j][1];
        m1.push_back(make_pair(make_pair(m[j][0], m[j][1]), j));
        m2.push_back(make_pair(make_pair(m[j][1], m[j][0]), j));
      } 
      N += a;
    }
    sort(m1.begin(), m1.end());
    sort(m2.begin(), m2.end());
    for(int i = 0; i < N; i++)
    {
      bool b1 = false, b2 = false;
      for(int j = i + 1; j < N; j++)
      {
        if(!b1 && m1[j].first.second > m1[i].first.second)
          pa[m1[i].second].push_back(m1[j].second), b1 = true;
        if(!b2 && m2[j].first.second > m2[i].first.second)
          pa[m2[i].second].push_back(m2[j].second), b1 = true;
      }
    }
    cout << max(dfs(m1[0].second), dfs(m2[0].second)) - 1 << endl;;
  }
}

C++: #include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main(){
  int i,j,a,b,n,m,hn[100],rn[100],hm[100],rm[100],h[201],r[201],k[2],L[201],max;
  while(1){
    cin >> n;
    if(n == 0) break;
    for(i=0;i<n;i++) cin >> hn[i] >> rn[i];
    cin >> m;
    for(i=0;i<m;i++) cin >> hm[i] >> rm[i];

    a = 0;
    b = 0;
    h[0] = -1;
    r[0] = -1;
    for(i=1;i<=m+n;i++){
      if(a < n && b < m && hn[a] < hm[b] || a < n && b >= m){
	  h[i] = hn[a];
	  r[i] = rn[a++];
	}
      else{
	h[i] = hm[b];
	r[i] = rm[b++];
      }
    }

      k[0] = 0;
      L[0] = 0;
      for(i=1;i<=m+n;i++){
	max = 0;
	for(j=0;j<i;j++){
	  if(L[j] > max && h[j] < h[i] && r[j] < r[i]) max = L[j];
	}
	L[i] = max + 1;
	if(k[0] < L[i]) k[0] = L[i];
      }

    for(i=1;i<=m+n;i++){
      if(a < n && b < m && rn[a] < rm[b] || a < n && b >= m){
	  h[i] = hn[a];
	  r[i] = rn[a++];
	}
      else{
	h[i] = hm[b];
	r[i] = rm[b++];
      }
    }

      k[1] = 0;
      L[0] = 0;
      for(i=1;i<=m+n;i++){
	max = 0;
	for(j=0;j<i;j++){
	  if(L[j] > max && h[j] < h[i] && r[j] < r[i]) max = L[j];
	}
	L[i] = max + 1;
	if(k[1] < L[i]) k[1] = L[i];
      }

      if(k[0] > k[1]) cout << k[0] << endl;
      else cout << k[1] << endl;

  }
  return 0;
}