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: Wrong Answer)

import java.awt.Point;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Main
{


  @SuppressWarnings("unchecked")
  public static void main
  (
    String[] args
  )
  {
    BufferedReader in_stream;
    String         in_str;
    String[]       in_str_split;

    int   in_N;
    ArrayList<Point> in_red;
    ArrayList<Point> in_blue;

    ArrayList<Point> tmp_blue;
    ArrayList<Point> tmp1;
    ArrayList<Point> tmp2;

    int tmp_x;
    int tmp_y;
    Point tmp_point;

    int cnt;
    int num;
    int cnt2;
    int num2;
    int ans1;
    int ans2;

    try
    {
      in_stream = new BufferedReader(new InputStreamReader(System.in));

      /*----------------
       * 引数
       *----------------*/
      in_str = new String(in_stream.readLine());
      in_N   = Integer.parseInt(in_str);

      in_red = new ArrayList<Point>();
      for(cnt=0; cnt<in_N; cnt++)
      {
        in_str       = new String(in_stream.readLine());
        in_str_split = in_str.split(" ");

        in_red.add( new Point(Integer.parseInt(in_str_split[0]), Integer.parseInt(in_str_split[1])) );
      }

      in_blue = new ArrayList<Point>();
      for(cnt=0; cnt<in_N; cnt++)
      {
        in_str       = new String(in_stream.readLine());
        in_str_split = in_str.split(" ");

        in_blue.add( new Point(Integer.parseInt(in_str_split[0]), Integer.parseInt(in_str_split[1])) );
      }

      /*----------------
       * 計算
       *----------------*/
      ans1 = 0;
      ans2 = 0;

      /* まず、xでソート */
      sort_x(in_red);
      sort_x(in_blue);

      /* アルゴリズム1 */
      tmp_blue = (ArrayList<Point>)(in_blue.clone());
      /* 赤い点をベースに、ペアの青い点を探す */
      num = in_red.size();
      for(cnt=0; cnt<num; cnt++)
      {
        tmp_point = in_red.get(cnt);

        /* 青い点群から、赤のxより大きい部分のみ抽出 */
        tmp1 = sub_array_x(tmp_blue, tmp_point);

        /* 見つかれば、一番若い部分をペアに */
        if(null != tmp1)
        {
          sort_y(tmp1);
          num2 = tmp1.size();
          for(cnt2=0; cnt2<num2; cnt2++)
          {
            if(tmp_point.y < tmp1.get(cnt2).y)
            {
              ans1++;
              tmp_blue.remove(tmp1.get(cnt2));
              break;
            }
          }
        }
      }

      /* アルゴリズム2 */
      tmp_blue = (ArrayList<Point>)(in_blue.clone());
      /* 赤い点をベースに、ペアの青い点を探す */
      num = in_red.size();
      for(cnt=0; cnt<num; cnt++)
      {
        tmp_point = in_red.get(cnt);

        /* 青い点群から、赤のxより大きい部分のみ抽出 */
        tmp1 = sub_array_x(tmp_blue, tmp_point);

        /* 見つかれば、一番若い部分をペアに */
        if(null != tmp1)
        {
          num2 = tmp1.size();
          for(cnt2=0; cnt2<num2; cnt2++)
          {
            if(tmp_point.y < tmp1.get(cnt2).y)
            {
              ans2++;
              tmp_blue.remove(tmp1.get(cnt2));
              break;
            }
          }
        }
      }

      System.out.println("" + ((ans1<ans2)?ans2:ans1) );
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }



  public static void sort_x
  (
    ArrayList<Point> array
  )
  {
    Collections.sort
    (
      array,
      new Comparator<Point>()
      {
        public int compare
        (
          Point obj1,
          Point obj2
        )
        {
          return obj2.x - obj1.x;
        }
      }
    );
  }



  public static void sort_y
  (
    ArrayList<Point> array
  )
  {
    Collections.sort
    (
      array,
      new Comparator<Point>()
      {
        public int compare
        (
          Point obj1,
          Point obj2
        )
        {
          return obj1.y - obj2.y;
        }
      }
    );
  }



  public static ArrayList<Point> sub_array_x
  (
    ArrayList<Point> array,
    Point            point
  )
  {
    ArrayList<Point> ret;
    int              x;
    int              num;
    int              cnt;


    x   = point.x;
    ret = null;

    num = array.size();
    for(cnt=0; cnt<num; cnt++)
    {
      if(x < array.get(cnt).x)
      {
        ret = new ArrayList<Point>(array.subList(cnt, num));
        break;
      }
    }

    return ret;
  }


  static class Point
  {
    public int x;
    public int y;

    public Point
    (
      int x,
      int y
    )
    {
      this.x = x;
      this.y = y;
    }
  }

}