Problem:
Score : 1000 points

Problem Statement

There are M chairs arranged in a line. The coordinate of the i-th chair (1 ≤ i ≤ M) is i.

N people of the Takahashi clan played too much games, and they are all suffering from backaches. They need to sit in chairs and rest, but they are particular about which chairs they sit in. Specifically, the i-th person wishes to sit in a chair whose coordinate is not greater than L_i, or not less than R_i. Naturally, only one person can sit in the same chair.

It may not be possible for all of them to sit in their favorite chairs, if nothing is done.
Aoki, who cares for the health of the people of the Takahashi clan, decides to provide additional chairs so that all of them can sit in chairs at their favorite positions.

Additional chairs can be placed at arbitrary real coordinates. Find the minimum required number of additional chairs.

Constraints

1 ≤ N,M ≤ 2 × 10^5

0 ≤ L_i < R_i ≤ M + 1(1 ≤ i ≤ N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
:
L_N R_N

Output

Print the minimum required number of additional chairs.

Sample Input 1

4 4
0 3
2 3
1 3
3 4

Sample Output 1

0

The four people can sit in chairs at the coordinates 3, 2, 1 and 4, respectively, and no more chair is needed.

Sample Input 2

7 6
0 7
1 5
3 6
2 7
1 6
2 6
3 7

Sample Output 2

2

If we place additional chairs at the coordinates 0 and 2.5, the seven people can sit at coordinates 0, 5, 3, 2, 6, 1 and 2.5, respectively.

Sample Input 3

3 1
1 2
1 2
1 2

Sample Output 3

2

Sample Input 4

6 6
1 6
1 6
1 5
1 5
2 6
2 6

Sample Output 4

2

Solution in Rust (1.15.1) (judge verdict: Time Limit Exceeded)

use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

type Weight = i32;
#[allow(dead_code)]
#[derive(Clone, Debug, Eq, PartialEq)]
struct Edge {
    index: usize,
    src: usize,
    dest: usize,
    weight: Weight,
}
#[allow(dead_code)]
impl Edge {
    fn new(index: usize, src: usize, dest: usize, weight: Weight) -> Edge {
        Edge {
            index: index,
            src: src,
            dest: dest,
            weight: weight,
        }
    }
}
impl std::cmp::Ord for Edge {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        (self.weight, self.src, self.dest).cmp(&(other.weight, other.src, other.dest))
    }
}
impl std::cmp::PartialOrd for Edge {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
type Edges = Vec<Edge>;
type Graph = Vec<Edges>;
#[allow(dead_code)]
fn graph_new(n: usize) -> Graph {
    vec![vec![]; n]
}

use std::collections::VecDeque;
// index ^ 1 is reverse edge
#[allow(dead_code)]
fn add_bi_edge(graph: &mut Graph, index: &mut usize, src: usize, dest: usize, weight: Weight) {
    graph[src].push(Edge::new(*index, src, dest, weight));
    *index += 1;
    graph[dest].push(Edge::new(*index, dest, src, weight));
    *index += 1;
}
#[allow(dead_code)]
fn add_uni_edge(graph: &mut Graph, index: &mut usize, src: usize, dest: usize, weight: Weight) {
    graph[src].push(Edge::new(*index, src, dest, weight));
    *index += 1;
    graph[dest].push(Edge::new(*index, dest, src, 0));
    *index += 1;
}

#[allow(dead_code)]
fn augment(
    g: &Graph,
    capacity: &mut Vec<Weight>,
    level: &Vec<usize>,
    finished: &mut Vec<bool>,
    from: usize,
    t: usize,
    cur: Weight,
) -> Weight {
    if from == t || cur == 0 {
        return cur;
    }
    if finished[from] {
        return 0;
    }
    for edge in g[from].iter() {
        let to = edge.dest;
        if level[to] != level[from] + 1 {
            continue;
        }
        let ncur = std::cmp::min(cur, capacity[edge.index]);
        let f = augment(g, capacity, level, finished, to, t, ncur);
        if f > 0 {
            capacity[edge.index] -= f;
            capacity[edge.index ^ 1] += f;
            return f;
        }
    }
    finished[from] = true;
    0
}

#[allow(dead_code)]
fn max_flow(g: &Graph, e: usize, s: usize, t: usize) -> Weight {
    let n = g.len();
    let mut capacity: Vec<Weight> = vec![0; e];
    for from in 0..n {
        for edge in g[from].iter() {
            capacity[edge.index] += edge.weight;
        }
    }
    let mut ans = 0;
    loop {
        let mut level = vec![usize::max_value(); n];
        level[s] = 0;
        let mut que = VecDeque::<usize>::new();
        que.push_back(s);
        while let Some(from) = que.pop_front() {
            if from == t {
                break;
            }
            for edge in g[from].iter() {
                let to = edge.dest;
                if capacity[edge.index] > 0 && level[to] == usize::max_value() {
                    que.push_back(to);
                    level[to] = level[from] + 1;
                }
            }
        }
        let mut finished = vec![false; n];
        let mut end = true;
        loop {
            let f = augment(
                g,
                &mut capacity,
                &level,
                &mut finished,
                s,
                t,
                Weight::max_value(),
            );
            if f == 0 {
                break;
            }
            ans += f;
            end = false;
        }
        if end {
            break;
        }
    }
    ans
}

struct Solver {
    n: usize,
    m: usize,
    lr: Vec<(usize, usize, usize)>,
}

impl Solver {
    fn new() -> Solver {
        let n = read::<usize>();
        let m = read::<usize>();
        let mut lr = vec![(0, 0, 0); n];
        for i in 0..n {
            lr[i].0 = read::<usize>();
            lr[i].1 = read::<usize>();
            lr[i].2 = lr[i].0 + (m + 1 - lr[i].1);
        }
        lr.sort_by(|lhs, rhs| lhs.2.cmp(&rhs.2));
        Solver { n: n, m: m, lr: lr }
    }
    fn IN(&self, x: usize) -> usize {
        x
    }
    fn LEFT(&self, x: usize) -> usize {
        x + self.n
    }
    fn RIGHT(&self, x: usize) -> usize {
        x + self.n + self.m
    }
    fn OUT(&self, x: usize) -> usize {
        x + self.n + self.m * 2
    }
    fn SOURCE(&self) -> usize {
        self.n + self.m * 3
    }
    fn DEST(&self) -> usize {
        self.n + self.m * 3 + 1
    }
    fn SIZE(&self) -> usize {
        self.n + self.m * 3 + 2
    }
    fn solve(&mut self) {
        let mut g = graph_new(self.SIZE());
        let mut e = 0;
        let mut l = 0;
        let mut r = self.m - 1;
        let mut ans = self.n;
        for i in 0..self.n {
            if l <= r && (l + 1 <= self.lr[i].0 || self.lr[i].1 <= r + 1) {
                ans -= 1;
                add_uni_edge(&mut g, &mut e, self.IN(i), self.SOURCE(), 1);
                if l + 1 <= self.lr[i].0 {
                    add_uni_edge(&mut g, &mut e, self.OUT(l), self.IN(i), 1);
                    l += 1;
                } else {
                    add_uni_edge(&mut g, &mut e, self.OUT(r), self.IN(i), 1);
                    r -= 1;
                }
            } else {
                add_uni_edge(&mut g, &mut e, self.SOURCE(), self.IN(i), 1);
            }
            if self.lr[i].0 != 0 {
                add_uni_edge(&mut g, &mut e, self.IN(i), self.LEFT(self.lr[i].0 - 1), 1);
            }
            if self.lr[i].1 != self.m + 1 {
                add_uni_edge(&mut g, &mut e, self.IN(i), self.RIGHT(self.lr[i].1 - 1), 1);
            }
        }
        for i in 0..self.m {
            if i != 0 {
                add_uni_edge(&mut g, &mut e, self.LEFT(i), self.LEFT(i - 1), 1 << 30);
            }
            if i != self.m - 1 {
                add_uni_edge(&mut g, &mut e, self.RIGHT(i), self.RIGHT(i + 1), 1 << 30);
            }
            add_uni_edge(&mut g, &mut e, self.LEFT(i), self.OUT(i), 1);
            add_uni_edge(&mut g, &mut e, self.RIGHT(i), self.OUT(i), 1);
            if i < l || r < i {
                add_uni_edge(&mut g, &mut e, self.DEST(), self.OUT(i), 1);
            } else {
                add_uni_edge(&mut g, &mut e, self.OUT(i), self.DEST(), 1);
            }
        }
        ans -= max_flow(&g, e, self.SOURCE(), self.DEST()) as usize;
        println!("{}", ans);
    }
}

fn main() {
    let mut solver = Solver::new();
    solver.solve();
}

#[test]
fn test() {
    let mut solver = Solver {
        n: 200000,
        m: 200000,
        lr: vec![(80000, 120000, 10); 200000],
    };
    solver.solve();
}