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++: //LIS
#include<iostream>
using namespace std;
int main(){
  int n,h1[101],r1[101],m,h2[101],r2[101],k;
  int data[201],data2[201],dp[201];

  data[0]=data2[0]=0;

  
  while(cin >>n && n!=0){
    dp[0]=0;

    k=0;
    for(int i=1;i<=n;i++){
      cin >>h1[i]>>r1[i];
      data[i]=h1[i];data2[i]=r1[i];}
    cin >>m;
    for(int i=1;i<=m;i++){
      cin >>h2[i]>>r2[i];
      data[i+n]=h2[i];data2[i+n]=r2[i];}
    int key;
    for(int i=1;i<n+m+1;i++){
      key=i;
      for(int j=i;j<n+m+1;j++){
	if(data[key]>data[j])key=j;}
      swap(data[i],data[key]);
      swap(data2[i],data2[key]);}

    for(int i=1;i<n+m+1;i++){
      key=0;
      for(int j=0;j<i;j++){
	
	if( ( ( data[j]<data[i] && data2[j]<data2[i] )  || key == 0 ) && dp[j]>dp[key]) {
	  key=j;
	}
      }
      dp[i] = dp[key]+1;
    }
   
    for(int i=1;i<n+m+1;i++){
      if(dp[i]>k)k=dp[i];

    }
    cout<<k<<endl;
    /*
	for(int i=0;i<n+m+1;i++){
      cout<<dp[i]<<endl;
      cout<<data[i]<<" "<<data2[i]<<endl;}
    */
  }
}

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]);
					//break;
				}
			}
		}
		std::cout << dp[n - 1] << std::endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
 
struct doll{
    int h,r;
};
 
bool operator<(const doll& d1,const doll& d2){
    if(d1.h == d2.h){
        return d1.r > d2.r;
    }
    return d1.h < d2.h;
}
 
int main() {
 
    while(1){
 
        int n;
 
        cin >> n;
 
        if(n==0) break;
 
        int h,r;
 
        vector<doll> v;
        doll tmp;
 
        for(int i=0;i<n;i++){
            cin >> h >> r;
            tmp.h = h;
            tmp.r = r;
            v.push_back(tmp);
        }
 
        int m;
 
        cin >> m;
 
        for(int i=0;i<m;i++){
            cin >> h >> r;
            tmp.h = h;
            tmp.r = r;
            v.push_back(tmp);
        }
 
        /*for(int i=0;i<n+m;i++){
            cout << v[i].h << " " << v[i].r << endl;
        }*/
 
        sort(v.begin(),v.end());
 
        int table[201][201];
 
        for(int i=0;i<=n+m;i++){
            for(int j=0;j<=n+m;j++){
                table[i][j] = 0;
            }
        }
 
        /*for(int i=0;i<n+m;i++){
            table[i][i] = 1;
        }*/
 
        for(int i=0;i<n+m;i++){
            for(int j=i;j<n+m;j++){
                if( (v[i].h<v[j].h && v[i].r < v[j].r) || (v[i].h > v[j].h && v[i].r > v[j].r) ){
                    table[i][j] = 1;
                    table[j][i] = 1;
                }
            }
        }
 
        /*for(int i=0;i<n+m;i++){
            for(int j=0;j<n+m;j++){
                cout << table[i][j] << " ";
            }
            cout << endl;
        }*/
 
        int dp[201][201] = {0};
 
        for(int i=0;i<=n+m;i++){
            dp[i][0] = 0;
            dp[0][i] = 1;
        }
 
        for(int i=1;i<=n+m;i++){
            for(int j=1;j<=n+m;j++){
                if(table[i-1][j-1]==1){
                    dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+1);
                }
                else{
                    dp[i][j] = dp[i-1][j];
                }
            }
 
        }
 
        /*for(int i=0;i<=n+m;i++){
            for(int j=0;j<=n+m;j++){
                cout << dp[i][j] << " ";
            }
            cout << endl;
        }*/
 
        int ans = 0;
 
        for(int i=0;i<=n+m;i++){
            //cout << dp[i] << endl;
            ans = max(dp[i][n+m],ans);
        }
 
        cout << ans << endl;
 
 
    }
 
    return 0;
}

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

typedef pair<int, int> Doll;
vector<Doll> Dolls;
int dp[201];

void debug(){
  for(int i = 0; i < Dolls.size(); i++){
    cout << "(" << Dolls[i].first << ", " << Dolls[i].second << ")" << endl;
  }
}

bool comp(const Doll& d1, const Doll& d2){
  return ((d1.first <= d2.first) && (d1.second <= d2.second));
}

int main(void){
  int n, m;
  while(cin >> n, n){
    Dolls.clear();
    Dolls.push_back(Doll(0, 0));
    for(int i = 0; i < n; i++){
      int h, r;
      cin >> h >> r;
      Dolls.push_back(Doll(h, r));
    }
    cin >> m;
    for(int i = 0; i < m; i++){
      int h, r;
      cin >> h >> r;
      Dolls.push_back(Doll(h, r));
    }
    sort(Dolls.begin(), Dolls.end(), comp);
    for(int i = 1; i < Dolls.size(); i++){
      int max_k = 0;
      for(int j = 0; j < i; j++){
        if(Dolls[j].first < Dolls[i].first && Dolls[j].second < Dolls[i].second && dp[max_k] < dp[j]){
          max_k = j;
        }
      }
      dp[i] = dp[max_k] + 1;
    }
    cout << dp[Dolls.size()-1] + 1 << endl;
  }
  return 0;
}