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<algorithm>

using P = std::pair<int, int>;

int n;

P dolls[200];

int dp[200];

int main() {
	while (1) {
		int m;
		std::cin >> n;
		if (n == 0)
			break;
		for (int i = 0; i < n; ++i)
			std::cin >> dolls[i].first >> dolls[i].second;
		std::cin >> m;
		for (int i = n; i < n + m; ++i)
			std::cin >> dolls[i].first >> dolls[i].second;
		n += m;
		std::sort(dolls, dolls + n);
		dp[0] = 1;
		for (int i = 1; i < n; ++i) {
			dp[i] = dp[i - 1];
			for (int j = i - 1; j >= 0; --j) {
				if (dolls[i].first > dolls[j].first&&
					dolls[i].second > dolls[j].second) {
					dp[i] = std::max(dp[j] + 1, dp[i]);
				}
			}
		}
		std::cout << dp[n - 1] << std::endl;
	}
	return 0;
}

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

int dp[200];
vector<pair<int, int>> m;

int main()
{
  while(true)
  {
    fill(dp, dp + 200, -1);
    m.clear();

    for(int i = 0; i < 2; i++)
    {
      int a;
      cin >> a;
      if(!a)
        return 0;
      for(int j = 0; j < a; j++)
      {
        int x, y;
        cin >> x >> y;
        m.push_back(make_pair(x, y));
      } 
    }
    sort(m.begin(), m.end());
    int ans = 0;
    for(int i = 0; i < m.size(); i++)
    {
      dp[i] = 1;
      for(int j = 0; j < i; j++)
        if(m[j].first < m[i].first && m[j].second < m[i].second)
          dp[i] = max(dp[i], dp[j] + 1);
      ans = max(ans, dp[i]);
    }
    cout << ans << endl;;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
int n, m, h[201], r[201];
int dp[201];
int rec( int idx){
  if(~dp[idx]) return dp[idx];
  int ret = 0;
  for(int i = 0; i < n + m; i++){
    if(h[idx] > h[i] && r[idx] > r[i]){
      ret = max( ret, rec(i) + 1);
    }
  }
  return dp[idx] = ret;
}
int main(){
  while( cin >> n, n){
    fill_n( dp, 201, -1);
    for(int i = 0; i < n; i++){
      cin >> h[i] >> r[i];
    }
    cin >> m;
    for(int i = 0; i < m; i++){
      cin >> h[n + i] >> r[n + i];
    }
    h[n + m] = 1000, r[n + m] = 1000;
    cout << rec( n + m ) << endl;
  }
}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return ~n & 1; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

main {
  int n;
  while (cin >> n, n) {
    int p;
    vpii v;
    {
      vpii a(n);
      cin >> a;
      int m;
      cin >> m;
      vpii b(m);
      cin >> b;
      p = n + m;
      v.reserve(p);
      for (auto &x : a) v.push_back(move(x));
      for (auto &x : b) v.push_back(move(x));
    }
    sort(all(v), [](auto &a, auto &b) {
      if (a.first != b.first) return a.first < b.first;
      return a.second > b.second;
    });
    vi dp(p);
    rep(i, p) {
      int s = 0;
      rep(j, i) if (v[j].second < v[i].second) cmax(s, dp[j]);
      dp[i] = s + 1;
    }
    cout << *max_element(all(dp)) << endl;
  }
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int N,M;
int dp[222];
main()
{
	while(cin>>N,N)
	{
		vector<pair<int,int> >A;
		for(int i=0;i<N;i++)
		{
			int a,b;cin>>a>>b;
			A.push_back(make_pair(a,-b));
		}
		cin>>M;
		for(int i=0;i<M;i++)
		{
			int a,b;cin>>a>>b;
			A.push_back(make_pair(a,-b));
		}
		sort(A.begin(),A.end());
		for(int i=1;i<=N+M+4;i++)dp[i]=1e9;
		for(pair<int,int>p:A)
		{
			int r=-p.second;
			int L=0,R=N+M+1;
			while(R-L>1)
			{
				int X=L+R>>1;
				if(dp[X]<r)L=X;
				else R=X;
			}
			dp[R]=r;
		}
		int ans=0;
		while(dp[ans+1]<1e9)ans++;
		cout<<ans<<endl;
	}
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
vector<pair<int,int> >data;
map<pair<int,int>,int> dp[201];

int check(int i,pair<int,int> last)
{
  //  cout<<i<<" "<<endl;
  if(i<0)
    return 0;

  if(dp[i].count(last)!=0)
    return dp[i][last];


  int ans=0;
  if(last.first>data[i].first &&
     last.second>data[i].second )
    ans=check(i-1,data[i])+1;

  ans=max(ans,check(i-1,last));
  dp[i][last]=ans;
  return ans;
}

int main()
{
  int n;
  for(;cin>>n,n;)
    {
      data.clear();
      for(int i=0;i<n;i++)
	{
	  int h,r;
	  cin>>h>>r;
	  data.push_back(make_pair(h,r));
	}
      int m;
      cin>>m;
      for(int i=0;i<m;i++)
	{
	  int h,r;
	  cin>>h>>r;
	  data.push_back(make_pair(h,r));
	}
      sort(data.begin(),data.end());
      // memset(dp,-1,sizeof(dp));
      //int ans=;
      for(int i=0;i<201;i++)
	dp[i].clear();

      cout<<check(data.size()-1,make_pair(9999,9999))<<endl;
    }

}