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() {
  
  int N;
  while(cin >> N && N) {
    
    vector<pair<int, int> > v1, v2;
    
    for(int i=0; i<N; i++) {
      int h, r; cin >> h >> r;
      v1.push_back(make_pair(h, r));
      v2.push_back(make_pair(r, h));
    }

    int M; cin >> M;
    
    for(int i=0; i<M; i++) {
      int h, r; cin >> h >> r;
      v1.push_back(make_pair(h, r));
      v2.push_back(make_pair(r, h));
    }
    
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    
    int size = N+M;
    int dp1[size];
    int ans = 1;
    for(int i=0; i<size; i++) {
      dp1[i] = 1;
      for(int j=0; j<i; j++)
        if(v1[i].first > v1[j].second &&
           v1[i].second > v1[j].second) { dp1[i] = max(dp1[i], dp1[j]+1); }
      ans = max(ans, dp1[i]);
    }
    
    cout << ans << endl;
    
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

template <typename T>
T lis(vector<T>& array, T INF = numeric_limits<T>::max() / 2) {
  vector<T> dp(array.size(), INF);
  for (T& i : array) {
    *lower_bound(dp.begin(), dp.end(), i) = i;
  }
  return lower_bound(dp.begin(), dp.end(), INF) - dp.begin();
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int n;
  while (cin >> n, n) {
    vector<pii> v;
    for (int i = 0; i < n; i++) {
      int h, r;
      cin >> h >> r;
      v.emplace_back(h, r);
    }
    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
      int h, r;
      cin >> h >> r;
      v.emplace_back(h, r);
    }
    sort(v.begin(), v.end(), [&](pii a, pii b) {
      return a.first != b.first ? a.first < b.first : a.second > b.second;
    });
    vector<int> a;
    for (int i = 0; i < n + m; i++) {
      a.emplace_back(v[i].second);
    }
    cout << lis<int>(a) << endl;
  }

  return 0;
}

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


class Doll{
public:
    int h;
    int r;
    int dp;
    static bool Less(const Doll& l, const Doll& r){
	return l.h < r.h;
    }
    Doll(int x, int y, int z){
	h = x; r = y; dp = z;
    }
};

typedef vector<Doll> Dolls;

int main(){
    int n, m, h, r, result=0;
    Dolls dolls;
    while(cin >> n, n){
	for(int i=0; i<n; ++i){
	    cin >> h >> r;
	    Doll temp(h,r,1);
	    dolls.push_back(temp);
	}
	cin >> m;
	for(int i=0; i<m; ++i){
	    cin >> h >> r;
	    Doll temp(h,r,1);
	    dolls.push_back(temp);
	}
	sort(dolls.begin(), dolls.end(), Doll::Less);
	
	for(Dolls::iterator it1=dolls.begin()+1;it1 != dolls.end(); ++it1){
	    for(Dolls::iterator it2=dolls.begin();it2 != it1; ++it2){
		if(it2->h < it1->h && it2->r < it1->r && it1->dp <= it2->dp){
		    it1->dp = it2->dp + 1;
		    result = max(result, it1->dp);
		}
	    }
	}
	cout << result << endl;
	dolls.clear();
	result = 0;
    }
}

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

#define iota(i,n,b,s) for(int i=int(b);i!=int((b)+(s)*(n));i+=(s))
#define range(i,n,m) iota(i,(((n)>(m))?((n)-(m)):((m)-(n))),(n),((n)>(m)?-1:1))
#define rep(i,n) iota(i,(n),0,1)
#define loop for(;;)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a,b) (make_pair(a,b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;

typedef long long Integer;
typedef double Real;
typedef vector<int> vi;
typedef vector<string> vs;
typedef map<string,int> Dictionary;
const Real PI = acos(-1);

template<class S, class T>
ostream& operator<<(ostream& os, pair<S,T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template<class T>
ostream& operator<<(ostream& os, vector<T> v) {
  if (v.size() == 0) {
    os << "(empty)";
    return os;
  }
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

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

int main() {

more:

  vector<int> hs;
  vector<int> rs;

  {
    int n; cin >> n;
    if (!n) return 0;
    rep (_, n) {
      int h, r; cin >> h >> r;
      hs.push_back(h);
      rs.push_back(r);
    }
  }

  {
    int n; cin >> n;
    rep (_, n) {
      int h, r; cin >> h >> r;
      hs.push_back(h);
      rs.push_back(r);
    }
  }

  const int N = hs.size();
  int d[N][N];

  rep (i, N) {
    rep (j, N) {
      if ((hs[i] < hs[j] && rs[i] < rs[j])) {
        d[i][j] = 1;
      } else {
        d[i][j] = 0;
      }
    }
  }

  /*
  int ans = 0;
  vector<bool>used(N, false);
  rep (i, N) {
    if (used[i]) continue;
    stack<pair<int,int>> s;
    s.push(cons(i, 1));
    while (!s.empty()) {
      auto t = s.top(); s.pop();
      used[car(t)] = true;
      rep (j, N) {
        if (d[car(t)][j]) s.push(cons(j, cdr(t) + 1));
      }
      ans = max(ans, cdr(t));
    }
  }
  cout << ans << endl;
  */

  goto more;
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <iomanip>
  
using namespace std;

#define pi 3.141592653589793

int main()
{
	int n, m;
	while(1){
		cin >> n;
		if(n == 0) break;
		int h, r;
		int mato[1001][1001] = {};
		for(int i = 0; i < n; i++){
			cin >> h >> r;
			mato[h][r] = 1;
		}
		cin >> m;
		for(int i = 0; i < m; i++){
			cin >> h >> r;
			mato[h][r] = 1;
		}
		int result[1001][1001] = {};
		int ans = 0;
		for(int i = 1; i < 1001; i++){
			for(int j = 1; j < 1001; j++){
				result[i][j] = max(result[i - 1][j - 1] + mato[i][j], max(result[i - 1][j], result[i][j - 1]));
				ans = max(ans, result[i][j]);
			}
		}
		cout << ans << endl;
	}
	return 0;
}