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 <map>
#include <algorithm>	// require sort next_permutation count etc.
//#include <fstream>	// require freopen

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

int main()
{
//	cut here before submit 
//	freopen ("testcase.russiandolls", "r", stdin );
	int n, m;

	while (cin >> n && n ){
		vector <P> taro (n);

		for (int i = 0; i < n; ++i){
			cin >> taro[i].first >> taro[i].second;
		} // end for
		cin >> m;
		vector <P> jiro (m);
		for (int j = 0; j < m; ++j ){
			cin >> jiro[j].first >> jiro[j].second;
		} // end for

		vector <P> all (n+m );
		for (int k = 0; k < n; ++k ){
			all[k] = taro[k];
		} // end for
		for (int k = n; k < n+m; ++k ){
			all[k] = jiro[k-n];
		} // end for
		sort (all.begin(), all.end() );


		vector <int> dp(n+m, 0 );

		int res = 0;
		for (int i = 0; i < n+m; ++i ){
			dp[i] = 1;
			for (int j = 0; j < i; ++j ){
				if (all[j].first < all[i].first && all[j].second < all[i].second ){
					dp[i] = max (dp[i], dp[j] + 1 );
				} // end if
			} // end for
			res = max (res, dp[i] );
		} // end for
		cout << res << endl;
	} // end loop
		
	return 0;
}

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;
	while(cin >> n,n){
		int dp[201] = {};
		for(int i = 0;i < n;i++) {
			cin >> h >> r;
			mat.push_back(P(h,r));
			dp[i] = 1;
		}
		cin >> m;
		for(int i = n;i < n + m;i++){
			cin >> h >> r;
			mat.push_back(P(h,r));
			dp[i] = 1;
		}
		sort(mat.begin(),mat.end());
		int ma = 1;
		for(int i = 1;i < mat.size();i++){
			for(int j = 0;j < i;j++){
				if(mat[i].second > mat[j].second && mat[i].first > mat[j].first){
					dp[i] = max(dp[i],dp[j] + 1);
				}
				ma = max(dp[i],ma);
			}
		}
		cout << ma << endl;
		mat.clear();
	}
	return 0;
}

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

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define MAXN 201
#define rep(i,n) for (int i = 0; i < (n); ++i)
const ll mod = 1e9+7;

struct M {
    int r, h;
};

bool comp_r (const M& l, const M& r) {
    return l.r < r.r;
}

bool comp_h (const M& l, const M& r) {
    return l.h < r.h;
}

int main() {


    int n, m, dp[MAXN];
    M a[MAXN];


    while (cin >> n, n) {
        vector<pair <int, int> >A;
        int h, r;
        rep(i, n) {
            cin >> a[i].h >> a[i].r;
        }
        cin >> m;

        rep(i, m) {
            cin >> a[n+i].h >> a[n+i].r;
        }

        sort(a, a+n+m, comp_r);

        dp[0] = 1;

        for(int i=1; i<n+m; ++i) {
            dp[i] = 1;
            for(int j=0; j<i; ++j) {
                if ( a[j].r < a[i].r and a[j].h < a[i].h ) {
                    dp[i] = max(dp[i], dp[j]+1);
                }
            }
        }

        int res_r = dp[n+m-1];

        sort(a, a+n+m, comp_h);

        dp[0] = 1;
        for(int i=1; i<n+m; ++i) {
            dp[i] = 1;
            for(int j=0; j<i; ++j) {
                if ( a[j].r < a[i].r and a[j].h < a[i].h) {
                    dp[i] = max(dp[i], dp[j]+1);
                }
            }
        }
        int res_h = dp[n+m-1];
        cout << max(res_r, res_h) << endl;
    }
}

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

typedef pair<int,int> PI;


main(){
  int n;
  while(cin >> n && n){
    priority_queue<PI , vector<PI> , greater<PI> > Q;
    int m;
    
    for(int i=0;i<n;i++){
      int h,r;
      cin >> h >> r;
      Q.push( make_pair(h,r));
    }
    cin >> m;
    for(int i=0;i<m;i++){
      int h,r;
      cin >> h >>r;
      Q.push( make_pair(h,r));
    }
    int a[m+n][2];//a[i][0] Height a[i][1] radius
    for(int i=0;i<m+n;i++){
      a[i][0]=Q.top().first;
      a[i][1]=Q.top().second;
      Q.pop();
    }

    int res[m+n][m+n];
    for(int i=0;i<m+n;i++){
      for(int j=0;j<m+n;j++){
        res[i][j]=0;
      }
      res[i][i]=1;
    }
    
    
    
    for(int i=0;i<m+n;i++){
      int ma =1;
      for(int j=0;j <= i;j++){
        if ( a[i][0] > a[j][0] && a[i][1] > a[j][1]){
          res[i][j]=res[j][j]+1;
          ma = max(res[i][j],ma);
        }else {
          /*
            if ( j>0 && i > 0)res[i][j]=max(res[i][j-1],res[i-1][j]);
            else if (i == 0 && j > 0)res[i][j]=res[i][j-1];
            else if (j == 0 && i>0)res[i][j]=res[i-1][j];
          */
          if ( j>0)res[i][j]=max(res[i][j-1],ma);
          
        }
      }
    }

    /*
    for(int i=0;i<m+n;i++){
      for(int j=0;j<m+n;j++)cout << res[i][j]<<"  ";
      cout << endl;
    }
    */
    
    int max=0;
    for(int i=0;i<m+n;i++){
      if (res[i][i] > max)max = res[i][i];
    }

    cout << max << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> P3;
typedef pair<P ,P> PP;
constexpr ll MOD = ll(1e9) + 7;
constexpr int IINF = INT_MAX;
constexpr ll LLINF = LLONG_MAX;
constexpr int MAX_N = int(2e5) + 5;
constexpr double EPS = 1e-9;
constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i > 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()


int main() {
    int n, m;
    while(cin >> n, n){
        vector<P> v;
        for(int i=0;i<n;i++){
            int h, r;
            cin >> h >> r;
            v.push_back({h,r});
        }
        cin >> m;
        for(int i=0;i<m;i++){
            int h, r;
            cin >> h >> r;
            v.push_back({h,r});
        }
        sort(v.begin(), v.end());

        v.push_back({IINF,IINF});
        int N = v.size();
        vector<int> dp(N, 0);
        for(int i=0;i<N;i++){
            for(int j=0;j<i;j++){
                if(v[j].first < v[i].first && v[j].second < v[i].second){
                    dp[i] = max(dp[i], dp[j]+1);
                }
            }
        }
        cout << dp[N-1] << endl;
    }
    return 0;
}