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.util.*;

public class Main{
    
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        Map<Integer, Integer> map1 = new TreeMap<>();
        Map<Integer, Integer> map2 = new TreeMap<>();
        for(int i = 0; i < N; i++){
            map1.put(sc.nextInt(), sc.nextInt());
        }
        for(int i = 0; i < N; i++){
            map2.put(sc.nextInt(), sc.nextInt());
        }
        sc.close();

        int[][] a1 = new int[2][N];
        int[][] c1 = new int[2][N];
        int i = 0;
        for(Integer key: map1.keySet()){
            a1[0][i] = key;
            a1[1][i] = map1.get(key);
            i++;
        }
        int j = 0;
        for(Integer key: map2.keySet()){
            c1[0][j] = key;
            c1[1][j] = map2.get(key);
            j++;
        }
        
        Map<Integer, Integer> map3 = new TreeMap<>();
        Map<Integer, Integer> map4 = new TreeMap<>();
        for(Integer key: map1.keySet()){
            map3.put(map1.get(key), key);
        }
        for(Integer key: map2.keySet()){
            map4.put(map2.get(key), key);
        }
        int[][] a2 = new int[2][N];
        int[][] c2 = new int[2][N];
        i = 0; 
        j = 0;
        for(Integer key: map3.keySet()){
            a2[0][i] = map3.get(key);
            a2[1][i] = key;
            i++;
        }
        for(Integer key: map4.keySet()){
            c2[0][j] = map4.get(key);
            c2[1][j] = key;
            j++;
        }

        int count1 = 0;
        loop1: for(int k = N-1; k >= 0; k--){
            for(int l = 0; l < N; l++){
                if(a1[0][k] < c2[0][l]){
                    if(a1[1][k] < c2[1][l]) {
                        count1++;
                        //System.out.println("(" + a1[0][k] + "," + a1[1][k] + "), (" + c1[0][l] + "," + c1[1][l] + ")");
                        c2[0][l] = -1;
                        continue loop1;
                    }
                }
            }
        }
        int count2 = 0;
        loop2: for(int k = N-1; k >= 0; k--){
            for(int l = 0; l < N; l++){
                if(a2[1][k] < c1[1][l]){
                    if(a2[0][k] < c1[0][l]) {
                        count2++;
                        //System.out.println("(" + a2[0][k] + "," + a2[1][k] + "), (" + c2[0][l] + "," + c2[1][l] + ")");
                        c1[1][l] = -1;
                        continue loop2;
                    }
                }
            }
        }
        System.out.println(Math.max(count1, count2));
    }
}