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 <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
	// your code goes here
	int n,m;
	cin>>n;
	while(n!=0){
		vector<pair<int,int> > ve;
		int h,r;
		for(int i=0;i<n;i++){
		cin>>h>>r;
		ve.push_back(make_pair(h,r));
		}
		cin>>m;
		for(int i=0;i<m;i++){
		cin>>h>>r;
		ve.push_back(make_pair(h,r));
		}
		sort(ve.begin(), ve.end());
		int dp[1010][1010]={0};
		
		for(int y=0;y<n+m;y++){
			h=ve[y].first;
			r=ve[y].second;
			dp[h][r]=max(dp[h-1][r-1]+1,dp[h][r]);
			for(int e=r+1;e<1010;e++) dp[h][e]=max(dp[h][e],dp[h][r]);
			if(y!=n+m-1){
				int hr=ve[y+1].first;
				for(int u=h+1;u<=hr;u++){
					for(int uu=0;uu<1010;uu++){
					dp[u][uu]=dp[h][uu];
					}
				}
				
			}
			
			
			
		}
		
		
		
		cout<<dp[h][1001]<<endl;
		
		cin>>n;
	}
	return 0;
}

C++: // AOJ 0157
#include<iostream>
#include<map>
#include<vector>
#include<string>
#include<sstream>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
typedef pair<int,int> P;

const int NMAX = 150;

vector<P> ichiro, jiro;
int n, m;
int idp[NMAX], jdp[NMAX];
int index[NMAX], jndex[NMAX];

void init() {
  REP(i, NMAX) {
    idp[i] = jdp[i] = 0;
    index[i] = jndex[i] = 0;
  }
  jiro.clear();
  ichiro.clear();

  jiro.push_back(make_pair(0,0));
  ichiro.push_back(make_pair(0,0));
}

string str(P p) {
  stringstream ss;
  ss << "(" << p.first << "," << p.second << ")";
  return ss.str();
}

bool bigger(P big, P small) {
//  cout << str(big) << " " << str(small) << endl;
  return (big.first > small.first and big.second > small.second);
}

int lowerb(P mine, vector<P> his) {
  int len = his.size();
  for (int i = 1; i < len; i++) {
    bool f = bigger(mine,his[i]);
//    cout << (f ? "true" : "false") << endl;
    if (not f) {
//      cout << i - 1 << endl;
      return i - 1;
    }
  }
//  cout << len - 1 << endl;
  return len - 1;
}

int rec(int i, int* mine, int* his) {
//  cout << "rec(" << i << "," << mine << "," << his << ")" << endl;
  if (i < 0) throw 1;
  if (i == 0) {
    return 0;
  } else {
    return 1 + max(rec(i - 1, mine, his), rec(mine[i], his, mine));
  }
}

int solve() {
  for (int i = 1; i <= n; i++) {
    index[i] = lowerb(ichiro[i], jiro);
  }

  for (int j = 1; j <= m; j++) {
    jndex[j] = lowerb(jiro[j], ichiro);
  }

/*  REP(i, n + 1) {
    cout << index[i] << " ";
  }cout << endl;
  REP(i, m + 1) {
    cout << jndex[i] << " ";
  }cout << endl;*/

  return max(rec(n, index, jndex), rec(m, jndex, index));
}

int main() {
  while (cin >> n and n) {
    init();
    REP(i, n) {
      int h, r;
      cin >> h >> r;
      ichiro.push_back(make_pair(h, r));
    }

    cin >> m;
    REP(i, m) {
      int h, r;
      cin >> h >> r;
      jiro.push_back(make_pair(h, r));
    }

    cout << solve() << endl;
  }
}

C++: // AOJ20150414.cpp : テ」ツつウテ」ツδウテ」ツつステ」ツδシテ」ツδォ テ」ツつ「テ」ツδ療」ツδェテ」ツつアテ」ツδシテ」ツつキテ」ツδァテ」ツδウテ」ツ?ョテ」ツつィテ」ツδウテ」ツδ暗」ツδェ テ」ツδ敕」ツつ、テ」ツδウテ」ツδ暗」ツつ津・ツョツ堙ァツセツゥテ」ツ?療」ツ?セテ」ツ?凖」ツ??
//

#include "stdafx.h"

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct DOLL{
	int h;
	int r;
};

int f(vector<DOLL> &d1, vector<DOLL> &d2, int h, int r)
{
	int ans = 1;
	for (DOLL &d : d1){
		if (d.h>h && d.r>r){
			int a = f(d1, d2, h, r);
			ans = max(ans, a);
		}
	}
	for (DOLL &d : d2){
		if (d.h>h && d.r>r){
			int a = f(d1, d2, h, r);
			ans = max(ans, a);
		}
	}
	return ans;
}

int main() {

	while (true){
		int n, m;
		cin >> n >> m;
		if (n == 0) break;

		vector<DOLL> d1;
		vector<DOLL> d2;

		for (int i = 0; i<n; i++){
			DOLL d;
			cin >> d.h >> d.r;
			d1.push_back(d);
		}

		for (int i = 0; i<m; i++){
			DOLL d;
			cin >> d.h >> d.r;
			d2.push_back(d);
		}

		int ans = f(d1, d2, -1, -1);
		cout << ans << endl;
	}

	return 0;
}

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


int main()
{
	int n, m;
	int table[250];
	pair<int, int> data[250];

	while (cin >> n, n)
	{
		for (int i = 0; i < n; i++)
			cin >> data[i].first >> data[i].second;

		cin >> m;
		for (int i = 0; i < m; i++)
			cin >> data[n+i].first >> data[n+i].second;

		for (int i = 0; i < n + m; i++)
		{
			for (int j = i + 1; j < n + m; j++)
			{
				if (data[i].first > data[j].first)
					swap(data[i], data[j]);
			}
		}

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

		cout << ans << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <map>
#include <vector>

using namespace std;
typedef pair<int, int> P;

void solve()
{
	int n;
	while(cin >> n, n)
	{
		map<int, int> data;
		for(int i = 0; i < n; ++i)
		{
			int h, r;
			cin >> h >> r;
			data.insert(map<int, int>::value_type(h, r));
		}
		int m;
		cin >> m;
		for(int i = 0; i < m; ++i)
		{
			int h, r;
			cin >> h >> r;
			data.insert(map<int, int>::value_type(h, r));
		}
		vector<P> doll;
		map<int, int>::reverse_iterator rit = data.rbegin();
		while(rit != data.rend())
		{
			doll.push_back(P(rit->first, rit->second));
			++rit;
		}
		int max_doll_count = 0;
		for(int i = 0; i < doll.size(); ++i)
		{
			int doll_count = 1;
			P current = doll[i];
			for(int j = i + 1; j < doll.size(); ++j)
			{
				if(current.first > doll[j].first && current.second > doll[j].second)
				{
					++doll_count;
					current = doll[j];
				}
			}
			if(max_doll_count < doll_count)
			{
				max_doll_count = doll_count;
			}
		}
		cout << max_doll_count << endl;
	}
}

int main()
{
	solve();
	return(0);
}

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

typedef pair<int,int> P;
const int MAX = 201;
int data[MAX];

int main(){
  int n,m;
  while(cin >> n && n){
    P p;
    vector<P> V;
    fill(data,data+MAX,0);
    V.push_back(P(-1,-1));
    for(int i = 0; i < n; i++){
      cin >> p.first >> p.second;
      V.push_back(p);
    }
    cin >> m;
    for(int i = 0; i < m; i++){
      cin >> p.first >> p.second;
      V.push_back(p);
    }
    sort(V.begin(),V.end());

    for(int i = 1; i < V.size(); i++)
      for(int j = 0; j < i; j++)
	if(V[i].first > V[j].first && V[i].second > V[j].second)
	  data[i] = max(data[j]+1,data[i]);

    int out = 0;
    for(int i = 0; i < MAX; i++) out = max(out,data[i]);
    cout << out << endl;
  }
  return 0;
}