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: Runtime Error)

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")
}

#[allow(dead_code)]
struct XorShift {
    x: u32,
    y: u32,
    z: u32,
    w: u32,
}
#[allow(dead_code)]
impl XorShift {
    fn new() -> XorShift {
        XorShift {
            x: 0x34fb2383,
            y: 0x327328fa,
            z: 0xabd4b54a,
            w: 0xa9dba8d1,
        }
    }
    fn seed(s: u32) -> XorShift {
        let mut rnd = XorShift::new();
        rnd.w = s;
        rnd
    }
    fn xor128(&mut self) -> u32 {
        let t = self.x ^ (self.x << 11);
        self.x = self.y;
        self.y = self.z;
        self.z = self.w;
        self.w = (self.w ^ (self.w >> 19)) ^ (t ^ (t >> 8));
        self.w
    }
    fn next_i32(&mut self, r: i32) -> i32 {
        (self.xor128() % r as u32) as i32
    }
    fn next_i32_lr(&mut self, l: i32, r: i32) -> i32 {
        self.next_i32(r - l + 1) + l
    }
    fn next_i64(&mut self, r: i64) -> i64 {
        ((((self.xor128() as u64) << 32) + self.xor128() as u64) % r as u64) as i64
    }
    fn next_i64_lr(&mut self, l: i64, r: i64) -> i64 {
        self.next_i64(r - l + 1) + l
    }
    fn next_f64(&mut self, r: f64) -> f64 {
        (self.xor128() as f64) / (0xffffffffu32 as f64) * r
    }
    fn next_f64_lr(&mut self, l: f64, r: f64) -> f64 {
        self.next_f64(r - l) + l
    }
}

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;
        let mut rnd = XorShift::new();
        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);
                let mut candidate = vec![];
                if l + 1 <= self.lr[i].0 {
                    candidate.push(l);
                }
                if self.lr[i].1 <= r + 1 {
                    candidate.push(r);
                }
                let t = candidate[rnd.next_i32(candidate.len() as i32) as usize];
                add_uni_edge(&mut g, &mut e, self.OUT(i), self.IN(i), 1);
                if l == t {
                    l += 1;
                } else {
                    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 110 < i && i % 100 == 0 {
                    add_uni_edge(&mut g, &mut e, self.LEFT(i), self.LEFT(i - 100), 1 << 30);
                }
                if 3010 < i && i % 3000 == 0 {
                    add_uni_edge(&mut g, &mut e, self.LEFT(i), self.LEFT(i - 3000), 1 << 30);
                }
            }
            if i != self.m - 1 {
                add_uni_edge(&mut g, &mut e, self.RIGHT(i), self.RIGHT(i + 1), 1 << 30);
                if self.m > 110 && i < self.m - 110 && i % 100 == 0 {
                    add_uni_edge(&mut g, &mut e, self.RIGHT(i), self.RIGHT(i + 100), 1 << 30);
                }
                if self.m > 3100 && i < self.m - 3100 && i % 3000 == 0 {
                    add_uni_edge(&mut g, &mut e, self.RIGHT(i), self.RIGHT(i + 3000), 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();
}