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<map>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
  int n;
  for(;cin>>n,n;)
    {
      vector<pair<int,int> > data;
      vector<pair<int,int> > sw;
      for(int i=0;i<n;i++)
	{
	  int h,r;
	  cin>>h>>r;
	  data.push_back(make_pair(h,r));
	  sw.push_back(make_pair(r,h));
	}
      int m;
      cin>>m;
      for(int i=0;i<m;i++)
	{
	  int h,r;
	  cin>>h>>r;
	  data.push_back(make_pair(h,r));
	  sw.push_back(make_pair(r,h));
	}
      sort(data.begin(),data.end(),greater<pair<int,int> >());
      sort(sw.begin(),sw.end(),greater<pair<int,int> >());

      pair<int,int> tmp[2]={data[0],sw[0]};
      int k[2]={1,1};
      for(int i=1;i<data.size();i++)
	{
	  if(tmp[0].first>data[i].first &&tmp[0].second>data[i].second)
	    {
	      k[0]++;
	      tmp[0]=data[i];
	    }
	  if(tmp[1].first>sw[i].first &&tmp[1].second>sw[i].second)
	    {
	      k[1]++;
	      tmp[1]=sw[i];
	    }
	}
      cout<<max(k[0],k[1])<<endl;
	/*
	  cout<<endl;
	  for(int i=0;i<data.size();i++)
	  cout<<data[i].first<<" "<<data[i].second<<endl;
	*/

    }

}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <utility>

#define ll (long long)
#define REP(i,n) for(int i = 0;i<(int)n;i++)
#define FOR(i,a,b) for(int i = (a);i < (b);i++)
#define RFOR(i,a,b) for(int i = (a);i>(b);i--)
#define COUNTER int i= 0,j = 0,k = 0,l = 0,m = 0, n= 0;
#define RETURN cout << endl;
/*vector<vector<int>>ArrayName = vector<vector<int>>(sizeofx, vector<int>(sizeofy, 0));*/
using namespace std;


int main() {
    int answer = 0;
    while (true) {
        /*pair??§????????§??±???*/
        vector< pair<int, int> > vp;
        int dp[205];
        int n; cin >> n; if (!n) break;
        for (int i = 0; i < n; i++) {
            int h, r; cin >> h >> r;
            vp.push_back(pair<int, int>(h, r));
        }
        int m; cin >> m;
        for (int i = 0; i < m; i++) {
            int h, r; cin >> h >> r;
            vp.push_back(pair<int, int>(h, r));
        }
        sort(vp.begin(), vp.end());
        //REP(i,vp.size()){cout << vp[i].first <<" " << vp[i].second<< endl;}
           int checkh = 0,checkr = 0;
        answer = 0;
        REP(i, m+n)
        {
            //dp???i?????????????????????????´???§??????????????????
            if(!i)
                answer++;
            else
                if(vp[i].first > checkr && vp[i].second >checkh)
                {
                    checkr = vp[i].first;
                    checkh = vp[i].second;
                    answer++;
                }
            
            
        }
        cout << answer << endl;
    }
}

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

#define	mp	make_pair
#define	pb	push_back
#define	h	first
#define	r	second

using namespace std;

typedef	pair<int,int>	pii;

vector< vector<int> > child;
vector<int> memo;
// max number of matrioshka which i-th matrioshka can have (i-th one is included)

int rec(int i)
{
	if(memo[i]!=-1)	return memo[i];

	int nmax=1;
	for(int j=0;j<child[i].size();j++)
		nmax=max(nmax,1+rec(child[i][j]));
	return memo[i]=nmax;
}

int main()
{
	while(1){
		vector<pii>	mat;
		int n;	cin>>n;
		for(int i=0,h,r;i<n;i++)	cin>>h>>r,mat.pb(mp(h,r));
		int m;	cin>>m;
		for(int i=0,h,r;i<m;i++)	cin>>h>>r,mat.pb(mp(h,r));
		if(n==0)	break;

		child=vector< vector<int> >(n+m);
		for(int i=0;i<n+m;i++){
			for(int j=0;j<n+m;j++){
				if(mat[i].h>mat[j].h && mat[i].r>mat[j].r)
					child[i].pb(j);
			}
		}

		memo=vector<int>(n+m,-1);
		int ans=0;
		for(int i=0;i<n+m;i++)	ans=max(ans,rec(i));
		cout<<ans<<endl;
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it = (c).begin(); it != (c).end(); it++)
#define H first
#define R second

using namespace std;

typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
const int N = 201;

int dp[N];

int main(void){
    for(int n; cin >> n, n;){
        vector<pii> in(N);
        rep(i, n) cin >> in[i].H >> in[i].R;

        int m; cin >> m;
        rep(i, m) cin >> in[n + i].H >> in[n + i].R;

        rep(i, n + m)  dp[i] = 0;

        sort(in.begin(), in.begin() + n + m);

        rep(i, n + m) rep(j, i) if(in[j].R < in[i].R) dp[i] = max(dp[i], dp[j] + 1);

        cout << *max_element(dp, dp + m + n) + 1 << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n,m;
  while(true){
      cin>>n;
      if(n==0) break;
      std::vector<P> v;
      for(int i=0;i<n;i++){
          int h,r;cin>>h>>r;
          v.push_back(P(h,r));
      }
      cin>>m;
      for(int i=0;i<m;i++){
          int h,r;cin>>h>>r;
          v.push_back(P(h,r));
      }
      sort(v.begin(),v.end());
      int res=0,hmin=0,rmin=0;
      for(int i=0;i<(int)v.size();i++){
          int h=v[i].first,r=v[i].second;
          if(hmin<h&&rmin<r){
              rmin=r;hmin=h;
              res++;
          }
      }
      cout<<res<<endl;
  }
}

C++: #include <bits/stdc++.h>

using namespace std;

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

int main(){
  while(1){
    cin>>n;
    if(n==0) break;
    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<i;j++){
        if(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();
  }
  return 0;
}