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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

pub trait InputValue {
    fn parse(s: &str) -> Self;
}

pub fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

pub fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

pub fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B);

// ===

struct Matching {
    n: usize,
    graph: Vec<Vec<Vec<usize>>>,
    deg: Vec<usize>,
    itr: Vec<usize>,
    lvl: Vec<i32>,
}

impl Matching {
    fn new(n: usize) -> Self {
        Matching {
            n: n,
            graph: vec![vec![]; n],
            deg: vec![0; n],
            itr: vec![0; n],
            lvl: vec![0; n]
        }
    }

    fn add_edge(&mut self, u: usize, v: usize, cap: usize) {
        let udeg = self.deg[u];
        let vdeg = self.deg[v];
        self.graph[u].push(vec![v, cap, vdeg]);
        self.graph[v].push(vec![u, 0, udeg]);
        self.deg[u] += 1;
        self.deg[v] += 1;
    }

    fn dfs(&mut self, v: usize, t: usize, f: usize) -> usize {
        if v == t {
            return f;
        }
        let mut it = self.itr[v];
        while it < self.graph[v].len() {
            self.itr[v] = it;

            let to = self.graph[v][it][0];
            let left_cap = self.graph[v][it][1];
            let rev_idx = self.graph[v][it][2];
            if left_cap > 0 && self.lvl[v] < self.lvl[to] {
                let d = self.dfs(to, t, min(left_cap, f));
                if d > 0 {
                    self.graph[v][it][1] -= d;
                    self.graph[to][rev_idx][1] += d;
                    return d;
                }
            }
            it += 1;
        }
        0
    }

    fn bfs(&mut self, s: usize) {
        for i in 0..self.n {
            self.lvl[i] = -1;
        }
        self.lvl[s] = 0;

        let mut queue = VecDeque::new();
        queue.push_back(s);
        while queue.len() >= 1 {
            let v = queue.pop_front().unwrap();
            let lv = self.lvl[v];
            let en = self.graph[v].len();
            for i in 0..en {
                let to = self.graph[v][i][0];
                let cap = self.graph[v][i][1];
                if cap > 0 && self.lvl[to] < 0 {
                    self.lvl[to] = lv + 1;
                    queue.push_back(to);
                }
            }
        }
    }

    fn max_flow(&mut self, s: usize, t: usize) -> usize {
        let mut flow = 0;
        loop {
            self.bfs(s);
            if self.lvl[t] < 0 {
                return flow;
            }
            for i in 0..self.n {
                self.itr[i] = 0;
            }
            loop {
                let f = self.dfs(s, t, 100000);
                if f == 0 {
                    break;
                }
                flow += f;
            }
        }
    }
}

fn main() {
    let n: usize = read();
    let red_points: Vec<(i32, i32)> = readn(n);
    let blue_points: Vec<(i32, i32)> = readn(n);

    let mut matching = Matching::new(2*n+2);

    for i in 0..n {
        matching.add_edge(2*n, i, 1);
        matching.add_edge(i+n, 2*n+1, 1);
    }
    for i in 0..n {
        for j in 0..n {
            if red_points[i].0 < blue_points[j].0 && red_points[i].1 < blue_points[j].1 {
                matching.add_edge(i, n+j, 1);
            }
        }
    }

    println!("{}", matching.max_flow(2*n, 2*n+1));
}