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

//  ____            _                  _                       _       _
// |  _ \ _   _ ___| |_    ___ _ __   | |_ ___ _ __ ___  _ __ | | __ _| |_ ___
// | |_) | | | / __| __|  / __| '_ \  | __/ _ \ '_ ` _ \| '_ \| |/ _` | __/ _ \
// |  _ <| |_| \__ \ |_  | (__| |_) | | ||  __/ | | | | | |_) | | (_| | ||  __/
// |_| \_\\__,_|___/\__|  \___| .__/___\__\___|_| |_| |_| .__/|_|\__,_|\__\___|
//                            |_| |_____|               |_|

//https://github.com/manta1130/Competitive_Programming_Template_Rust

#[macro_use]
mod input {

    use std;
    use std::io;

    const SPLIT_DELIMITER: char = ' ';

    #[macro_export]
    #[allow(unused_macros)]
    macro_rules! input {
    ( $($x:expr ),*) => {
        {
            let temp_str = input_line_str();
            let mut split_result_iter = temp_str.split_whitespace();
                $(
                let buf_split_result = split_result_iter.next();
                let buf_split_result = buf_split_result.unwrap();
                    ($x) = buf_split_result.parse().unwrap();
                )*
        }
    };
}

    #[allow(dead_code)]
    pub fn input_line_str() -> String {
        let mut s = String::new();
        io::stdin().read_line(&mut s).unwrap();
        s.trim().to_string()
    }

    #[allow(dead_code)]
    pub fn p<T>(t: T)
    where
        T: std::fmt::Display,
    {
        println!("{}", t);
    }

    #[allow(dead_code)]
    pub fn input_vector2d<T>(line: usize) -> Vec<Vec<T>>
    where
        T: std::str::FromStr,
    {
        let mut v: Vec<Vec<T>> = Vec::new();

        for _ in 0..line {
            let vec_line = input_vector();
            v.push(vec_line);
        }
        v
    }

    #[allow(dead_code)]
    pub fn input_vector<T>() -> Vec<T>
    where
        T: std::str::FromStr,
    {
        let mut v: Vec<T> = Vec::new();

        let s = input_line_str();
        let split_result = s.split(SPLIT_DELIMITER);
        for z in split_result {
            let buf = match z.parse() {
                Ok(r) => r,
                Err(_) => panic!("Parse Error"),
            };
            v.push(buf);
        }
        v
    }

    #[allow(dead_code)]
    pub fn input_vector_row<T>(n: usize) -> Vec<T>
    where
        T: std::str::FromStr,
    {
        let mut v = Vec::with_capacity(n);
        for _ in 0..n {
            let buf = match input_line_str().parse() {
                Ok(r) => r,
                Err(_) => panic!("Parse Error"),
            };
            v.push(buf);
        }
        v
    }

    pub trait ToCharVec {
        fn to_charvec(&self) -> Vec<char>;
    }

    impl ToCharVec for String {
        fn to_charvec(&self) -> Vec<char> {
            self.to_string().chars().collect::<Vec<_>>()
        }
    }
}

mod flow {
use std;
use std::collections::VecDeque;

type CapValue = u64;
const INF: CapValue = 999999999;

#[derive(Clone, Copy)]
struct Edge {
    to: usize,
    cap: CapValue,
    rev: usize,
}

///最大流を計算(Dinicのアルゴリズム)
pub struct Dinic {
    n: usize,
    g: Vec<Vec<Edge>>,
    iter: Vec<usize>,
    level: Vec<isize>,
}

impl Dinic {
    ///要素数nのグラフを構築する。
    pub fn new(n: usize) -> Dinic {
        let res = Dinic {
            n: n,
            g: vec![vec![]; n],
            iter: vec![],
            level: vec![],
        };
        res
    }
    fn bfs(&mut self, s: usize) {
        self.level = vec![-1; self.n];
        let mut que = VecDeque::new();
        self.level[s] = 0;
        que.push_back(s);
        while !que.is_empty() {
            let v = que.pop_front().unwrap();
            for i in 0..self.g[v].len() {
                let e = &self.g[v][i];
                if e.cap > 0 && self.level[e.to] < 0 {
                    self.level[e.to] = self.level[v] + 1;
                    que.push_back(e.to);
                }
            }
        }
    }
    fn dfs(&mut self, k: usize, t: usize, f: CapValue) -> CapValue {
        if k == t {
            return f;
        }
        for i in self.iter[k]..self.g[k].len() {
            let e = self.g[k][i];
            if e.cap > 0 && self.level[k] < self.level[e.to] {
                let r = self.dfs(e.to, t, std::cmp::min(f, e.cap));
                if r > 0 {
                    self.g[k][i].cap -= r;
                    self.g[e.to][e.rev].cap += r;
                    return r;
                }
            }
        }

        0
    }
    ///最大フローを求める。
    pub fn get_maxflow(&mut self, s: usize, t: usize) -> CapValue {
        let mut res = 0;
        loop {
            self.bfs(s);
            if self.level[t] < 0 {
                return res;
            }
            self.iter = vec![0; self.n];
            loop {
                let f = self.dfs(s, t, INF);
                if f == 0 {
                    break;
                }
                res += f;
            }
        }
    }
    ///辺の追加
    ///
    /// f->tにvalueの容量を持つ辺を追加する。
    pub fn add_edge(&mut self, f: usize, t: usize, value: CapValue) {
        let idx_t = self.g[t].len();
        let idx_f = self.g[f].len();
        self.g[f].push(Edge {
            to: t,
            cap: value,
            rev: idx_t,
        });
        self.g[t].push(Edge {
            to: f,
            cap: 0,
            rev: idx_f,
        });
    }
}

///最大流を計算(Ford-Fulkersonのアルゴリズム)
pub struct FordFulkerson {
    n: usize,
    g: Vec<Vec<Edge>>,
    used: Vec<bool>,
}

impl FordFulkerson {
    ///要素数nのグラフを構築する。
    pub fn new(n: usize) -> FordFulkerson {
        let res = FordFulkerson {
            n: n,
            g: vec![vec![]; n],
            used: vec![],
        };
        res
    }
    fn dfs(&mut self, k: usize, t: usize, f: CapValue) -> CapValue {
        if k == t {
            return f;
        }
        self.used[k] = true;
        for i in 0..self.g[k].len() {
            let e = self.g[k][i];
            if !self.used[e.to] && e.cap > 0 {
                let r = self.dfs(e.to, t, std::cmp::min(f, e.cap));
                if r > 0 {
                    self.g[k][i].cap -= r;
                    self.g[e.to][e.rev].cap += r;
                    return r;
                }
            }
        }

        0
    }
    ///最大フローを求める。
    pub fn get_maxflow(&mut self, s: usize, t: usize) -> CapValue {
        let mut res = 0;
        loop {
            self.used = vec![false; self.n];
            let f = self.dfs(s, t, INF);
            if f == 0 {
                return res;
            }
            res += f;
        }
    }
    ///辺の追加
    ///
    /// f->tにvalueの容量を持つ辺を追加する。
    pub fn add_edge(&mut self, f: usize, t: usize, value: CapValue) {
        let idx_t = self.g[t].len();
        let idx_f = self.g[f].len();
        self.g[f].push(Edge {
            to: t,
            cap: value,
            rev: idx_t,
        });
        self.g[t].push(Edge {
            to: f,
            cap: 0,
            rev: idx_f,
        });
    }
}
}

use flow::*;
#[allow(unused_imports)]
use input::*;

fn main() {
    let n: usize;
    input!(n);
    let mut red = vec![];
    let mut blue = vec![];
    let mut f = Dinic::new(2 * n + 2);
    let mut count = 1;
    for _ in 0..n {
        let (a, b): (usize, usize);
        input!(a, b);
        red.push((count, a, b));
        count += 1;
    }
    for _ in 0..n {
        let (a, b): (usize, usize);
        input!(a, b);
        blue.push((count, a, b));
        count += 1;
    }

    for i in 0..red.len() {
        for j in 0..blue.len() {
            if red[i].1 < blue[j].1 && red[i].2 < blue[j].2 {
                f.add_edge(red[i].0, blue[j].0, 1);
                f.add_edge(blue[j].0, red[i].0, 1);
            }
        }
    }
    for i in 0..red.len() {
        f.add_edge(0, red[i].0, 1);
        f.add_edge(red[i].0, 0, 1);
    }
    for i in 0..blue.len() {
        f.add_edge(2 * n + 1, blue[i].0, 1);
        f.add_edge(blue[i].0, 2 * n + 1, 1);
    }
    println!("{}", f.get_maxflow(0, 2 * n + 1));
}