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 <algorithm>
using namespace std;

struct D{int h, r;};
bool ascendDr(const D& d1, const D& d2){
  return d1.r < d2.r;
}
bool ascendDh(const D& d1, const D& d2){
  return d1.h < d2.h;
}

int main(void){
  int n, m, dp[200];
  D dolls[200];
  while(cin >> n, n){
    for(int i = 0; i < n; i++) cin >> dolls[i].h >> dolls[i].r;
    cin >> m;
    for(int i = n; i < n + m; i++) cin >> dolls[i].h >> dolls[i].r;

    int lis_r = 0, lis_h = 0;
    sort(dolls, dolls + n + m, ascendDr);

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

    sort(dolls, dolls + n + m, ascendDh);

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

    cout << max(lis_h, lis_r) << endl;
  }
  return 0;
}

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

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

P data[202];
int dp[202];

int main(){

	int n, m;
	while (std::cin >> n, n){

		memset(data, 0, sizeof(data));
		memset(dp, 0, sizeof(dp));

		for (int i = 1; i <= n; i++){
			std::cin >> data[i].first >> data[i].second;
		}

		std::cin >> m;
		for (int i = 1; i <= m; i++){
			std::cin >> data[i + n].first >> data[i + n].second;
		}

		std::sort(data, data + 1 + n + m);

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

		int max = 0;
		for (int i = 0; i <= n + m; i++){
			if (dp[i] > max)max = dp[i];
		}
		std::cout << max << std::endl;
	}
	return 0;
}

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

using namespace std;

#define ALL(co) co.begin(), co.end()
#define tpl(...) make_tuple(__VA_ARGS__)

typedef long long LL;
typedef pair<int, int> P; typedef pair<int, P> IP; typedef pair<P, P> PP;
typedef vector<int> Array; typedef vector<vector<int> > Array2;
typedef vector<LL> LArray; typedef vector<P> PArray; typedef vector<string> SArray;

const int INF = 1 << 29;
const LL LINF = 1LL << 60;

inline int getInt() { int itiv; return (cin >> itiv, itiv); }
template <typename T> void readAll(vector<T>& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i]; }
template <typename T, typename U> void readAll(vector<pair<T, U> >& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i].first >> vec[i].second; }
template < typename T > inline string toString( const T &a ){ ostringstream oss; oss << a; return oss.str(); };
inline bool between(int min, int max, int n) { return min <= n && n <= max; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }

int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
int dr[] = { 0, -1, 0, 1 }, dc[] = { -1, 0, 1, 0 };

int m, n;

int dp[210];

int main(void)
{
	ios::sync_with_stdio(false);
	while(cin >> n && n)
	{
		PArray arr;
		for (int i = 0; i < n; i++)
		{
			int h, r; cin >> h >> r;
			arr.emplace_back(h, r);
		}
		cin >> m;
		for (int i = 0; i < m; i++)
		{
			int h, r; cin >> h >> r;
			arr.emplace_back(h, r);
		}

		sort(ALL(arr));
		fill(dp, dp + n + m, INF);
		for (int i = 0; i < n + m; i++)
		{
			*(lower_bound(dp, dp + n + m, arr[i].second)) = arr[i].second;
		}
		cout << (lower_bound(dp, dp + n + m, INF) - dp) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
void print(vector<vector<int> > dp){
  cout << "---------------" << endl;
  for(int i = 0;i  < dp.size();i++){
	for(int j = 0;j < dp.size();j++){
	  cout << dp[i][j] << " ";
	}
	cout << endl;
  }
}
int cal(vector<pair<int,int> > vec){
  vector<vector<int> > dp(vec.size(),vector<int>(vec.size(),0));  
  for(int i = 0;i < vec.size()-1;i++){
	for(int j = 0;j < vec.size();j++){
	  for(int k = j+1;k < vec.size();k++){
		if(vec[j].first > vec[k].first && vec[j].second > vec[k].second){
		  dp[i+1][k] = max(dp[i][j]+1,dp[i+1][k]);

		}
	  }
	}
  }
  int ans = 0;
  for(int i = 0;i < vec.size();i++){
	ans = max(ans,dp[vec.size()-1][i]);
  }
  return ans;
}
void print2(vector<int> vec){
  for(int i = 0;i < vec.size();i++)
	cout << vec[i] << " ";
  cout << endl;

}
int cal2(vector<pair<int,int> > vec){
  vector<int> dp(vec.size(),0);
  for(int i = 0;i < vec.size();i++){
	for(int j =i+1;j < vec.size();j++){
	  if(vec[i].first > vec[j].first && vec[i].second > vec[j].second){
		dp[j] = max(dp[i]+1,dp[j]);
	  }
	}
//	print2(dp);
  }
  int ans = 0;
  for(int i = 0;i < dp.size();i++){
	ans = max(ans,dp[i]);
  }
  return ans;
}
int main(){
  int n;

  while(cin >> n && n!=0){
	vector<pair<int,int> > vec;
	vec.emplace_back(10000,10000);
	for(int i = 0;i < n;i++){
	  int h,r;
	  cin >> h >> r;
	  vec.emplace_back(h,r);  
	}
	cin >> n;
	for(int i = 0;i < n;i++){
	  int h,r;
	  cin >> h >> r;
	  vec.emplace_back(h,r);
	}
	sort(vec.begin(),vec.end(),greater<pair<int,int> >());
	cout << cal(vec) << endl;
//	cout << cnt << endl;
  }
  return 0;
}