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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace AtCoder
{
 
    static class Program
    {
        static void Main()
        {
            Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
            //
 
            var N = GetInt();
            var dinic = new Dinic(2 * N + 2);
 
 
            List<Tuple<int, int>> R = new List<Tuple<int, int>>();
            List<Tuple<int, int>> B = new List<Tuple<int, int>>();
            for (int i = 0; i < N; i++)
            {
                var cin = GetIntArray();
                R.Add(new Tuple<int, int>(cin[0], cin[1]));
            }
            for (int i = 0; i < N; i++)
            {
                var cin = GetIntArray();
                B.Add(new Tuple<int, int>(cin[0], cin[1]));
            }
 
            for (int i = 0; i < N; i++)
            {
                dinic.AddEdge(2 * N, i, 1);
                dinic.AddEdge(i + N, 2 * N + 1, 1);
                for (int j = 0; j < R.Count(); j++)
                {
                    if (B[i].Item1 > R[j].Item1 && B[i].Item2 > R[j].Item2)
                    {
                        dinic.AddEdge(i, N+j, 1);
                    }
                }
            }
            var ans = dinic.MaxFlow(2 * N, 2 * N + 1);
            Console.WriteLine(ans);
 
            //
            Console.Out.Flush();
            Console.ReadKey();
        }
 
        class Dinic
        {
            public Dinic(int node_size)
            {
                V = node_size;
                G = new List<List<Edge>>();
                for(int i = 0; i < V; i++)
                {
                    G.Add(new List<Edge>());
                }
                level = Enumerable.Repeat(0, V).ToList();
                iter = Enumerable.Repeat(0, V).ToList();
            }
          
          	class Edge
            {
                public Edge(int to, int cap, int rev)
                {
                    To = to; Cap = cap; Rev = rev;
                }
                public int To { get; set; }
                public int Cap;
                public int Rev { get; set; }
            }
 
            List<List<Edge>> G;
            int V;
            List<int> level;
            List<int> iter;
 
            public void AddEdge(int from, int to, int cap)
            {
                G[from].Add(new Edge(to, cap, G[to].Count));
                G[to].Add(new Edge(from, 0, G[from].Count - 1));
            }
 
            public int MaxFlow(int s, int t)
            {
                int flow = 0;
                while (true)
                {
                    BFS(s);
                    if (level[t] < 0) { return flow; }
                    iter = Enumerable.Repeat(0, V).ToList();
                    var f = DFS(s, t, 1000000007);
                    while (f > 0)
                    {
                        flow += f;
                        f = DFS(s, t, 1000000007);
                    }
                }
            }
 
            void BFS(int s)
            {
                level = Enumerable.Repeat(-1, V).ToList();
                level[s] = 0;
                var que = new Queue<int>();
                que.Enqueue(s);
                while (que.Count != 0)
                {
                    var v = que.Dequeue();
                    for (int i = 0; i < G[v].Count; i++)
                    {
                        var e = G[v][i];
                        if (e.Cap > 0 && level[e.To] < 0)
                        {
                            level[e.To] = level[v] + 1;
                            que.Enqueue(e.To);
                        }
                    }
                }
            }
 
            int DFS(int v, int t, int f)
            {
                if (v == t) return f;
                for (int i = iter[v]; i < G[v].Count; i++)
                {
                    iter[v] = i;
                    var e = G[v][i];
                    if (e.Cap > 0 && level[v] < level[e.To])
                    {
                        var d = DFS(e.To, t, Math.Min(f, e.Cap));
                        if (d > 0)
                        {
                            e.Cap -= d;
                            G[e.To][e.Rev].Cap += d;
                            return d;
                        }
                    }
                }
                return 0;
            }
        }
 
 
        static public string GetStr() { return Console.ReadLine().Trim(); }
        static public int GetInt() { return int.Parse(Console.ReadLine()); }
        static public long GetLong() { return long.Parse(Console.ReadLine()); }
        static public string[] GetStrArray() { return Console.ReadLine().Split(' '); }
        static public int[] GetIntArray() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
        static public long[] GetLongArray() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
        static public char[] GetCharArray() { return Console.ReadLine().Split(' ').Select(char.Parse).ToArray(); }
        static public List<double> GetDoubleList() { return Console.ReadLine().Split(' ').Select(double.Parse).ToList(); }
        static public void WriteObjects<T>(IEnumerable<T> values) { foreach (var o in values) { Console.Write(o + " "); } }
        static string yesno(this bool b) { return b ? "yes" : "no"; }
        static string YesNo(this bool b) { return b ? "Yes" : "No"; }
        static string YESNO(this bool b) { return b ? "YES" : "NO"; }
 
        static bool eq<T, U>() => typeof(T).Equals(typeof(U));
        static T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
        static T cv<T>(string s) => eq<T, int>() ? ct<T, int>(int.Parse(s))
                           : eq<T, long>() ? ct<T, long>(long.Parse(s))
                           : eq<T, double>() ? ct<T, double>(double.Parse(s))
                           : eq<T, char>() ? ct<T, char>(s[0])
                                             : ct<T, string>(s);
        static void Multi<T>(out T a) => a = cv<T>(GetStr());
        static void Multi<T, U>(out T a, out U b)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]);
        }
        static void Multi<T, U, V>(out T a, out U b, out V c)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]);
        }
        static void Multi<T, U, V>(out T a, out U b, out V c, out V d)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<V>(ar[3]);
        }
    }
}