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::cmp::{Ordering};

#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};

#[allow(unused_imports)]
use std::iter::FromIterator;

#[allow(unused_macros)]
macro_rules! min {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::min($x, min!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! max {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::max($x, max!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! get { 
    ([$t: ty]) => { 
        { 
            let mut line = String::new(); 
            stdin().read_line(&mut line).unwrap(); 
            line.split_whitespace().map(|t|t.parse::<$t>().unwrap()).collect::<Vec<_>>()
        }
    };
    ([$t: ty];$n: expr) => {
        (0..$n).map(|_|get!([$t])).collect::<Vec<_>>()
    };
    ($t: ty) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t: ty),*) => {
        { 
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*)
        }
    };
    ($t: ty; $n: expr) => {
        (0..$n).map(|_|get!($t)).collect::<Vec<_>>()
    };
    ($($t: ty),*; $n: expr) => {
        (0..$n).map(|_|get!($($t),*)).collect::<Vec<_>>()
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*).unwrap();
    }
}

const BIG_STACK_SIZE: bool = true;

#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}

struct HopcroftKarp {
    l_size: usize,
    r_size: usize,
    list: Vec<Vec<usize>>,
    seen: Vec<bool>,
    matched: Vec<bool>,
    level: Vec<isize>,
    matching: Vec<usize>,
}
impl HopcroftKarp {
    fn new(l: usize, r: usize) -> Self {
        HopcroftKarp {
            l_size: l,
            r_size: r,
            list: vec![Vec::new(); l],
            seen: vec![false; l],
            matched: vec![false; l],
            level: vec![-1; l + 1],
            matching: vec![l; r],
        }
    }
    fn add_edge(&mut self, from: usize, to: usize) {
        self.list[from].push(to);
    }
    fn bfs(&mut self) {
        let mut queue = VecDeque::new();
        for left in 0..self.l_size {
            self.level[left] = -1;
            if !self.matched[left] {
                queue.push_back(left);
                self.level[left] = 0;
            }
        }
        self.level[self.l_size] = self.l_size as isize;
        while let Some(left) = queue.pop_front() {
            for i in 0..self.list[left].len() {
                let right = self.list[left][i];
                let next = self.matching[right];
                if self.level[next] == -1 {
                    self.level[next] = self.level[left] + 1;
                    queue.push_back(next);
                }
            }
        }
    }
    fn dfs(&mut self, left: usize) -> bool {
        if left == self.l_size {
            return true;
        } else if self.seen[left] {
            return false;
        }
        self.seen[left] = true;
        for i in 0..self.list[left].len() {
            let right = self.list[left][i];
            let next = self.matching[right];
            if self.level[next] > self.level[left] && self.dfs(next) {
                self.matching[right] = left;
                return true;
            }
        }
        false
    }
    fn solve(&mut self) -> usize {
        self.seen = vec![false; self.l_size];
        self.matched = vec![false; self.l_size];
        self.level = vec![-1; self.l_size + 1];
        self.matching = vec![self.l_size; self.r_size];
        let mut res = 0;
        loop {
            self.bfs();
            self.seen = vec![false; self.l_size];
            let mut finished = true;
            for left in 0..self.l_size {
                if !self.matched[left] && self.dfs(left) {
                    self.matched[left] = true;
                    res += 1;
                    finished = false;
                }
            }
            if finished {
                break;
            }
        }
        res
    }
}

fn solve() {
    let n = get!(usize);
    let ab = get!(usize, usize; n);
    let cd = get!(usize, usize; n);

    let mut solver = HopcroftKarp::new(n,n);

    for i in 0..n {
        for j in 0..n {
            if ab[i].0 < cd[j].0 && ab[i].1 < cd[j].1 {
                solver.add_edge(i,j);
            }
        }
    }
    println!("{}", solver.solve());
}