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 Java (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try (Scanner in = new Scanner(System.in)) {
            int N = in.nextInt();
            int[] a = new int[N];
            int[] b = new int[N];
            int[] c = new int[N];
            int[] d = new int[N];
            for (int i = 0; i < N; i++) {
                a[i] = in.nextInt();
                b[i] = in.nextInt();
            }
            for (int i = 0; i < N; i++) {
                c[i] = in.nextInt();
                d[i] = in.nextInt();
            }

            int[][] cost = new int[N][N];
            for (int from = 0; from < N; from++) {
                for (int to = 0; to < N; to++) {
                    cost[from][to] = (int) (a[from] < c[to] && b[from] < d[to] ? 1 : 1000);
                }
            }

            System.out.println(MinCostFlow.bestMatching(cost) % 1000);
        }
    }
}

class MinCostFlow {
    class Edge {
        int to;
        int cap;
        int cost;
        int rev;

        public Edge(int to, int cap, int cost, int rev) {
            this.to = to;
            this.cap = cap;
            this.cost = cost;
            this.rev = rev;
        }
    }

    private static final int INF = (int) 1e9;
    private ArrayList<ArrayList<Edge>> G;
    private int[] dist;
    private int[] prevv;
    private int[] preve;

    public MinCostFlow(int v) {
        clear(v);
    }

    public void clear(int v) {
        G = new ArrayList<ArrayList<Edge>>();
        for (int i = 0; i < v; i++) {
            G.add(new ArrayList<Edge>());
        }
        dist = new int[v];
        prevv = new int[v];
        preve = new int[v];
    }

    public void addEdge(int from, int to, int cap, int cost) {
        assert cost >= 0;
        assert cost <= INF;

        int edgeIndex = G.get(from).size();
        int reverseEdgeIndex = G.get(to).size();
        G.get(from).add(new Edge(to, cap, cost, reverseEdgeIndex));
        G.get(to).add(new Edge(from, 0, -cost, edgeIndex));
    }

    public int run(int s, int t, int f) {
        int res = 0;
        while (f > 0) {
            Arrays.fill(dist, INF);
            dist[s] = 0;

            for (;;) {
                boolean update = false;

                for (int v = 0; v < dist.length; v++) {
                    if (dist[v] >= INF) {
                        continue;
                    }
                    for (int i = 0; i < G.get(v).size(); i++) {
                        Edge e = G.get(v).get(i);
                        if (e.cap > 0 && dist[v] + e.cost < dist[e.to]) {
                            dist[e.to] = dist[v] + e.cost;
                            prevv[e.to] = v;
                            preve[e.to] = i;
                            update = true;
                        }
                    }
                }

                if (!update) {
                    break;
                }
            }

            if (dist[t] >= INF) {
                return -1;
            }

            int d = f;
            for (int v = t; v != s; v = prevv[v]) {
                d = Math.min(d, G.get(prevv[v]).get(preve[v]).cap);
            }

            f -= d;
            res += d * dist[t];

            for (int v = t; v != s; v = prevv[v]) {
                Edge e = G.get(prevv[v]).get(preve[v]);
                e.cap -= d;
                G.get(v).get(e.rev).cap += d;
            }

        }

        return res;
    }

    public static int bestMatching(int[][] costs) {
        assert check(costs);

        int R = costs.length;
        int C = costs[0].length;

        MinCostFlow mcf = new MinCostFlow(R + C + 2);

        int s = R + C;
        int t = s + 1;

        for (int r = 0; r < R; r++) {
            mcf.addEdge(s, r, 1, 0);
        }
        for (int r = 0; r < R; r++) {
            for (int c = 0; c < C; c++) {
                if (costs[r][c] < 0) {
                    continue;
                }
                mcf.addEdge(r, R + c, 1, costs[r][c]);
            }
        }
        for (int c = 0; c < C; c++) {
            mcf.addEdge(R + c, t, 1, 0);
        }

        int minCost = mcf.run(s, t, R);
        return minCost;
    }

    private static boolean check(int[][] costs) {
        assert costs.length > 0;
        assert costs[0].length > 0;
        for (int i = 1; i < costs.length; i++) {
            assert costs[i].length == costs[i - 1].length;
        }
        return true;
    }
}