Problem:
Score : 400 points

Problem Statement

On a two-dimensional plane, there are N red points and N blue points.
The coordinates of the i-th red point are (a_i, b_i), and the coordinates of the i-th blue point are (c_i, d_i).

A red point and a blue point can form a friendly pair when, the x-coordinate of the red point is smaller than that of the blue point, and the y-coordinate of the red point is also smaller than that of the blue point.

At most how many friendly pairs can you form? Note that a point cannot belong to multiple pairs.

Constraints

All input values are integers.

1 \leq N \leq 100

0 \leq a_i, b_i, c_i, d_i < 2N

a_1, a_2, ..., a_N, c_1, c_2, ..., c_N are all different.

b_1, b_2, ..., b_N, d_1, d_2, ..., d_N are all different.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
:
a_N b_N
c_1 d_1
c_2 d_2
:
c_N d_N

Output

Print the maximum number of friendly pairs.

Sample Input 1

3
2 0
3 1
1 3
4 2
0 4
5 5

Sample Output 1

2

For example, you can pair (2, 0) and (4, 2), then (3, 1) and (5, 5).

Sample Input 2

3
0 0
1 1
5 2
2 3
3 4
4 5

Sample Output 2

2

For example, you can pair (0, 0) and (2, 3), then (1, 1) and (3, 4).

Sample Input 3

2
2 2
3 3
0 0
1 1

Sample Output 3

0

It is possible that no pair can be formed.

Sample Input 4

5
0 0
7 3
2 2
4 8
1 6
8 5
6 9
5 4
9 1
3 7

Sample Output 4

5

Sample Input 5

5
0 0
1 1
5 5
6 6
7 7
2 2
3 3
4 4
8 8
9 9

Sample Output 5

4

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.*;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

@SuppressWarnings("unused")
public class Main {
	
	//final boolean isDebug = true;
	final boolean isDebug = false;
	String fileName = "input.txt";
	FastScanner sc;
	PrintWriter out;
	final int MOD = (int)1e9+7;
	final int INF = Integer.MAX_VALUE / 2;
	
	void solve() throws Exception{
		int N = sc.nextInt();
		int s = 0, t = N+N+1;
		int[] a = new int[N+1];
		int[] b = new int[N+1];
		int[] c = new int[N+1];
		int[] d = new int[N+1];
		Dinic dinic = new Dinic(N+N+2);
		
		for(int i = 1; i <= N; i++){
			a[i] = sc.nextInt();
			b[i] = sc.nextInt();
			dinic.addEdge(s, i, 1);
		}
		for(int i = 1; i <= N; i++){
			c[i] = sc.nextInt();
			d[i] = sc.nextInt();
			dinic.addEdge(N+i, t, 1);
			for(int j = 1; j <= N; j++) if(a[j] < c[i] && b[j] < d[i]) dinic.addEdge(j, N+i, 1);
		}
		
		System.out.println(dinic.maxFlow(s, t));
	}
	
	/* end solve */
	
	/* main */
	public static void main(String[] args) throws Exception {
		new Main().m();
	}
	
	void m() throws Exception {
		long S = System.currentTimeMillis();
		sc = (isDebug) ? new FastScanner(new FileInputStream(fileName)) : new FastScanner(System.in);
		out = new PrintWriter(System.out);
		solve();
		out.flush();
		long G = System.currentTimeMillis();
		if(isDebug){
			System.out.println("---Debug---");
			System.out.printf("%8d ms", (G-S));
		}
	}
	/* end main */
}
/* end Main */

class Dinic{
	class Edge{
		int to;
		long cap;
		int rev;
		boolean isRev;
		public Edge(int t, long c, int r, boolean i){
			to = t; cap = c; rev = r; isRev = i;
		}
	}
	
	int V;
	ArrayList<ArrayList<Edge>> graph;
	int[] minCost, iter;
	
	public Dinic(int V){
		this.V = V;
		graph = new ArrayList<>();
		for(int i = 0; i < V; i++) graph.add(new ArrayList<>());
		minCost = new int[V];
		iter = new int[V];
	}
	
	void addEdge(int from, int to, long cap){
		graph.get(from).add(new Edge(to, cap, graph.get(to).size(), false));
		graph.get(to).add(new Edge(from, 0, graph.get(from).size()-1, true));
	}
	
	boolean bfs(int s, int t){
		Arrays.fill(minCost, -1);
		Deque<Integer> queue = new ArrayDeque<Integer>();
		minCost[s] = 0;
		queue.addLast(s);
		while(!queue.isEmpty() && minCost[t] == -1){
			int p = queue.getFirst();
			queue.pop();
			for(Edge e : graph.get(p)){
				if(e.cap > 0 && minCost[e.to] == -1){
					minCost[e.to] = minCost[p] + 1;
					queue.push(e.to);
				}
			}
		}
		
		return minCost[t] != -1;
	}
	
	long dfs(int index, int t, long flow){
		if(index == t) return flow;
		for(int i = iter[index]; i < graph.get(index).size(); i++){
			Edge e = graph.get(index).get(i);
			if(e.cap > 0 && minCost[index] < minCost[e.to]){
				long d = dfs(e.to, t, Math.min(flow, e.cap));
				if(d > 0){
					e.cap -= d;
					graph.get(e.to).get(e.rev).cap += d;
					return d;
				}
			}
		}
		return 0;
	}
	
	long maxFlow(int s, int t){
		long flow = 0;
		while(bfs(s, t)){
			Arrays.fill(iter, 0);
			long f = 0;
			while((f = dfs(s, t, Long.MAX_VALUE / 2)) > 0) flow += f;
		}
		return flow;
	}
	
	void output(){
		for(int i = 0; i < graph.size(); i++){
			for(Edge e : graph.get(i)){
				if(e.isRev) continue;
				Edge revEdge = graph.get(e.to).get(e.rev);
				System.out.println(i + " -> " + e.to + " (flow: " + revEdge.cap + " / " + (e.cap + revEdge.cap) + ")");
			}
		}
	}
}

class FastScanner {
    private InputStream in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    public FastScanner(InputStream in) {
		this.in = in;
	}
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() {
    	if (hasNextByte()) return buffer[ptr++];
    	else return -1;
    }
    private static boolean isPrintableChar(int c){
    	return 33 <= c && c <= 126;
    }
    public boolean hasNext() {
    	while(hasNextByte() && !isPrintableChar(buffer[ptr]))
    		ptr++; return hasNextByte();
    }
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public String nextLine() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(b != 10) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() {
    	return Double.parseDouble(next());
    }
}