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<cstdio>
#include<vector>
#include<algorithm>
int field1[1001];
int field2[1001];
using namespace std;
int main(void){
    while(1){
        int n; scanf("%d",&n);
        if(n == 0) break;
        vector<pair<int,int> > v1;
        vector<pair<int,int> > v2;
        pair<int,int> p;
        p.first = 0; p.second = 0;
        v1.push_back(p); v2.push_back(p);
        for(int i = 0;i < n;i++){
            int h,r; scanf("%d%d",&h,&r);
            p.first = h; p.second = r;
            v1.push_back(p);
            p.first = r; p.second = h;
            v2.push_back(p);
        }

        scanf("%d",&n);
        for(int i = 0;i < n;i++){
            int h,r; scanf("%d%d",&h,&r);
            p.first = h; p.second = r;
            v1.push_back(p);
            p.first = r; p.second = h;
            v2.push_back(p);
        }

        sort(v1.begin(),v1.end()); sort(v2.begin(),v2.end());
        for(int i = 1;i < v1.size();i++){
            for(int j = i - 1;j >= 0;j--){
                if(v1[i].second > v1[j].second && v1[i].first > v1[j].first && field1[i] <= field1[j]+1){
                    field1[i] = field1[j]+1;
                }
            }
        }

        for(int i = 1;i < v2.size();i++){
            for(int j = i-1;j >= 0;j--){
                if(v2[i].second > v2[j].second && v2[i].first > v2[j].first && field2[i] <= field2[j]+1){
                    field2[i] = field2[j]+1;
                }
            }
        }
        int ans = -1;
        for(int i = 0;i < v1.size();i++){
            if(field1[i] >= ans) ans = field1[i];
            if(field2[i] >= ans) ans = field2[i];
        }
        printf("%d\n",ans);
    }
    return 0;
}

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

typedef std::pair<int,int> P;

int dp[200];//何番目のまとりょーしかが一番大きいか＝数

int main(){
	int ichiro,jiro,M=0;
	P dool[200];//高さ　半径
	std::cin>>ichiro;
	for(int i=0;i<ichiro;i++){
		std::cin>>dool[i].first>>dool[i].second;
	}
	std::cin>>jiro;
	for(int i=0;i<jiro;i++){
		std::cin>>dool[i+ichiro].first>>dool[i+ichiro].second;
	}
	std::sort(dool,dool+ichiro+jiro);
	for(int i=0;i<ichiro+jiro;i++)dp[i]=1;
	for(int i=1;i<ichiro+jiro;i++){
		for(int j=i-1;j>=0;j--){
			dp[i]=std::max(dp[i],dp[j]+(dool[i].second>dool[j].second));
		}
		M=std::max(M,dp[i]);
	}
	std::cout<<M<<std::endl;
	return 0;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init));

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

struct data{
  ll h,r,s;
};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;
  while(cin >> n && n){
    vector<data> v,v2;
    ll h,r;
    rep(i,n){
      cin >> h >> r;
      v.push_back({h,r,h+r});
    }
    cin >> m;
    rep(i,m){
      cin >> h >> r;
      v2.push_back({h,r,h+r});
    }
    ll ans = 0,tmp = 1,c=0,d=0;
    h = 0,r = 0;
    while(c < n || d < m){
      if(v[c].h <= h || v[c].r <= r) ++c;
      if(v2[d].h <= h || v2[d].r <= r) ++d;
      if(c < n && d < m){
        if(((v[c].h <= v2[d].h && v[c].r <= v2[d].r)||(v2[d].h <= h || v2[d].r <= r)) && v[c].h > h && v[c].r > r){
          h = v[c].h;
          r = v[c].r;
          ++c;
          ++ans;
        }
        else if(v2[d].h > h && v2[d].r > r){
          h = v2[d].h;
          r = v2[d].r;
          ++d;
          ++ans;
        }
        else{
          ++c;
          ++d;
        }
      }
      else if(c >= n){
        if(h < v2[d].h && r < v2[d].r){
          h = v2[d].h;
          r = v2[d].r;
          ++ans;
        }
        ++d;
      }
      else if(d >= m){
        if(h < v[c].h && r < v[c].r){
          h = v[c].h;
          r = v[c].r;
          ++ans;
        }
        ++c;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 10e-8;

class mycmp{
public:
	bool operator()(const P p1, const P p2){
		return (p1.first < p2.first && p1.second < p2.second);
	}
};

int dp[202];

int main(){
	int n;

	while(~scanf("%d",&n)){
		if(n==0) break;
		vector<P> dolls;

		for(int i=0;i<n;i++){
			int h,r;
			scanf("%d %d",&h,&r);
			dolls.push_back(P(h,r));
		}

		int m;
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			int h,r;
			scanf("%d %d",&h,&r);
			dolls.push_back(P(h,r));
		}


		sort(dolls.begin(),dolls.end(),mycmp());

		memset(dp,0,sizeof(dp));
		dp[0] = 1;

		for(int i=1;i<dolls.size();i++){
			for(int j=i-1;j>=0;j--){
				if(dolls[j].first < dolls[i].first
					&& dolls[j].second < dolls[i].second){
						dp[i] = max(dp[i],dp[j] + 1);
				}
				else dp[i] = dp[i-1];
			}
		}

		cout << dp[dolls.size()-1] << endl;
	}
}