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: Time Limit Exceeded)

use std::str::FromStr;
use std::fmt::Debug;
use std::cmp;

#[allow(dead_code)]
fn read_as_vec<T>() -> Vec<T>
where T: FromStr, <T as FromStr>::Err : Debug{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().split_whitespace().map(|c| T::from_str(c).unwrap()).collect()

}

#[derive(Clone, Debug)]
struct Edge{
    to: usize,
    capacity: usize,
    flow: usize,
    is_rev: bool,
    idx: usize
}


impl Edge{
    fn new(to: usize, capacity: usize, flow: usize, is_rev: bool, idx: usize) -> Edge{
        Edge{to: to,
             capacity: capacity,
             flow: flow,
             is_rev: is_rev,
             idx: idx}
    }

    fn to(&self) -> usize{
        self.to
    }

    fn idx(&self) -> usize{
        self.idx
    }

    fn is_rev(&self) -> bool{
        self.is_rev
    }

    fn mergin(&self) -> usize{
        self.capacity - self.flow
    }

    fn increase_flow(&mut self, val: usize){
        assert!(self.mergin() >= val);
        self.flow += val;
    }

    fn decrease_flow(&mut self, val: usize){
        assert!(self.flow >= val);
        self.flow -= val;
    }

    fn is_full(&self) -> bool{
        self.flow >= self.capacity
    }
}

#[derive(Debug)]
struct FordFulkerson{
    pub residual: Vec<Vec<Edge>>,
    start: usize,
    goal: usize
}

impl FordFulkerson{
    fn new(graph: Vec<Vec<(usize, usize)>>, start: usize, goal: usize) -> FordFulkerson{
        let mut residual: Vec<Vec<Edge>> = vec![vec![];graph.len()];
        for from in 0..graph.len(){
            for j in 0..graph[from].len(){
                let (to, capacity) = graph[from][j];
                let from_idx = residual[to].len();
                let to_idx = residual[from].len();
                residual[from].push(Edge::new(to, capacity, 0, false, from_idx));
                residual[to].push(Edge::new(from, capacity, capacity, true, to_idx));
            }
        }
        FordFulkerson{
            residual: residual,
            start: start,
            goal: goal
        }
    }

    fn find_augmenting_path_recur(&self, stack: &mut Vec<Edge>,
                                  visited: &mut Vec<bool>, cur_v: usize, goal: usize)
                                  -> bool{

        if cur_v == goal{
            return true;
        }

        //println!("current = {}", cur_v);
        //println!("nexts = {:?}",  self.residual[cur_v]);
        for edge in self.residual[cur_v].iter().filter(|e| !e.is_full()){
            //println!("{:?}", edge);
            if visited[edge.to()] == true{
                continue;
            }

            stack.push(edge.clone());
            visited[edge.to()] = true;
            if self.find_augmenting_path_recur(stack, visited, edge.to(), goal){
                return true;
            }
            stack.pop();
            visited[edge.to()] = false;
        }

        return false;
    }

    fn find_augmenting_path(&self) -> Option<Vec<Edge>>{
        let mut stack: Vec<Edge> = vec![];
        let mut visited: Vec<bool> = vec![false ;self.residual.len()];
        let (start, goal) = (0, self.residual.len()-1);
        visited[start] = true;
        if self.find_augmenting_path_recur(&mut stack, &mut visited, start, goal){
            return Some(stack);
        }else{
            return None;
        }
    }

    fn update_if_possible(&mut self) -> bool{
        let argumenting_path = self.find_augmenting_path();
        if argumenting_path.is_none(){
            return false;
        }

        let argumenting_path = argumenting_path.unwrap();
         let additional: usize = argumenting_path.iter().map(|e| e.mergin()).min().unwrap();
        assert!(additional > 0);
        for path in &argumenting_path{
            let (rev_i, rev_j) = (path.to(), path.idx());
            self.residual[rev_i][rev_j].decrease_flow(additional);

            let (i, j) = (self.residual[rev_i][rev_j].to(), self.residual[rev_i][rev_j].idx);
            self.residual[i][j].increase_flow(additional);
        }
        return true;
    }

    fn get_max_flow(&mut self) -> usize{
        self.residual[self.goal].iter().filter(|e| e.is_rev).map(|e| e.mergin()).sum::<usize>()
    }
}


//21:45
fn main(){
    let n: usize = read_as_vec()[0];

    let mut r: Vec<(i64, i64)> = vec![];

    for _ in 0..n{
        let line = read_as_vec::<i64>();
        r.push((line[0], line[1]));
    }

    let mut b: Vec<(i64, i64)> = vec![];

    for _ in 0..n{
        let line = read_as_vec::<i64>();
        b.push((line[0], line[1]));
    }

    let mut v: Vec<Vec<(usize, usize)>> = vec![vec![];2*n+2];

    for i in 0..n{
        for j in 0..n{
            if r[i].0 < b[j].0 && r[i].1 < b[j].1{
                v[i+1].push((j+n+1, 1));
            }
        }
    }
    for i in 0..n{
        v[0].push((i+1, 1));
        v[n+i+1].push((2*n+1, 1));
    }

    let mut ff = FordFulkerson::new(v, 0, 2*n+1);

    loop{
        if !ff.update_if_possible(){
            break;
        }
    }

    println!("{}", ff.get_max_flow());
}