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: Accepted)

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)]
#[derive(Clone)]
struct Node {
    v: i32,
}

#[allow(dead_code)]
impl Node {
    fn new(v: i32) -> Node {
        Node { v: v }
    }
    fn default_value() -> Node {
        Node { v: 0 }
    }
}
#[allow(dead_code)]
fn merge(left: &Node, right: &Node) -> Node {
    Node::new(std::cmp::max(left.v, right.v))
}
// add op
#[allow(dead_code)]
fn resolve_delay(lhs: &Node, rhs: &Node) -> Node {
    Node::new(lhs.v + rhs.v)
}

#[allow(dead_code)]
struct DelaySegmentTree {
    n: usize,
    max_depth: usize,
    data: Vec<Node>,
    delay_data: Vec<Node>,
    updated: Vec<bool>,
}
#[allow(dead_code)]
impl DelaySegmentTree {
    fn new(n: usize) -> DelaySegmentTree {
        let mut max_depth = 0;
        while (1 << max_depth) < n {
            max_depth += 1;
        }
        DelaySegmentTree {
            n: n,
            max_depth: max_depth,
            data: vec![Node::default_value(); 1 << (max_depth + 1)],
            delay_data: vec![Node::default_value(); 1 << (max_depth + 1)],
            updated: vec![false; 1 << (max_depth + 1)],
        }
    }
    // [left, right)
    fn range_op(&mut self, left: usize, right: usize, value: Node) {
        assert!(left < right);
        assert!(right <= self.n);
        self.in_range_op(value, 0, 1, left, right);
    }
    // [left, right)
    fn get(&mut self, left: usize, right: usize) -> Node {
        assert!(left < right);
        assert!(right <= self.n);
        self.in_get(0, 1, left, right)
    }
    // private
    fn in_range_op(&mut self, value: Node, depth: usize, node: usize, left: usize, right: usize) {
        let width = 1 << (self.max_depth - depth);
        let index = node - (1 << depth);
        let node_left = index * width;
        let node_mid = node_left + (width >> 1);
        self.divide(node);
        if right - left == width && left == node_left {
            self.updated[node] = true;
            self.delay_data[node] = value;
            self.divide(node);
        } else {
            if right <= node_mid {
                self.in_range_op(value, depth + 1, node * 2, left, right);
                self.divide(node * 2 + 1);
            } else if node_mid <= left {
                self.divide(node * 2);
                self.in_range_op(value, depth + 1, node * 2 + 1, left, right);
            } else {
                self.in_range_op(value.clone(), depth + 1, node * 2, left, node_mid);
                self.in_range_op(value, depth + 1, node * 2 + 1, node_mid, right);
            }
            self.data[node] = merge(&self.data[node * 2], &self.data[node * 2 + 1]);
        }
    }
    fn in_get(&mut self, depth: usize, node: usize, left: usize, right: usize) -> Node {
        let width = 1 << (self.max_depth - depth);
        let index = node - (1 << depth);
        let node_left = index * width;
        let node_mid = node_left + (width >> 1);
        self.divide(node);
        if right - left == width && left == node_left {
            return self.data[node].clone();
        } else if right <= node_mid {
            return self.in_get(depth + 1, node * 2, left, right);
        } else if node_mid <= left {
            return self.in_get(depth + 1, node * 2 + 1, left, right);
        }
        merge(
            &self.in_get(depth + 1, node * 2, left, node_mid),
            &self.in_get(depth + 1, node * 2 + 1, node_mid, right),
        )
    }
    fn divide(&mut self, node: usize) {
        if !self.updated[node] {
            return;
        }
        if node < (1 << self.max_depth) {
            for i in 0..2 {
                if self.updated[node * 2 + i] {
                    self.delay_data[node * 2 + i] =
                        resolve_delay(&self.delay_data[node * 2 + i], &self.delay_data[node]);
                } else {
                    self.delay_data[node * 2 + i] = self.delay_data[node].clone();
                    self.updated[node * 2 + i] = true;
                }
            }
        }
        self.updated[node] = false;
        self.data[node] = resolve_delay(&self.data[node], &self.delay_data[node]);
    }
}

#[test]
fn test_delay_segment_tree() {
    let mut stree = DelaySegmentTree::new(10);
    stree.range_op(1, 4, Node::new(3));
    assert!(stree.get(0, 1).v == 0);
    assert!(stree.get(1, 2).v == 3);
    assert!(stree.get(3, 4).v == 3);
    assert!(stree.get(4, 5).v == 0);
    assert!(stree.get(0, 10).v == 3);

    stree.range_op(3, 6, Node::new(7));
    assert!(stree.get(2, 3).v == 3);
    assert!(stree.get(3, 4).v == 10);
    assert!(stree.get(5, 6).v == 7);
    assert!(stree.get(6, 7).v == 0);
    assert!(stree.get(0, 10).v == 10);

    stree.range_op(3, 6, Node::new(10));
    stree.range_op(3, 6, Node::new(10));
    stree.range_op(3, 6, Node::new(10));
    assert!(stree.get(3, 4).v == 40);
}

fn print_stree(stree: &mut DelaySegmentTree) {
    for i in 0..stree.n {
        if i != 0 {
            print!(" ");
        }
        print!("{}", stree.get(i, i + 1).v);
    }
    println!("");
}
fn solve() {
    let n = read::<usize>();
    let m = read::<usize>();
    let mut lr = vec![(0, 0); n];
    for i in 0..n {
        lr[i].0 = read::<usize>();
        lr[i].1 = read::<usize>();
    }
    lr.sort();
    let mut ans = std::cmp::max(0, n as i32 - m as i32);
    let mut stree = DelaySegmentTree::new(m + 2);
    for i in (0..m + 1).rev() {
        stree.range_op(i, i + 1, Node::new(-(m as i32 + 1 - i as i32)));
    }
    for i in 0..n {
        if lr[i].0 == m {
            break;
        }
        stree.range_op(0, lr[i].1 + 1, Node::new(1));
        // print_stree(&mut stree);
        let mut lans = -(lr[i].0 as i32) + stree.get(lr[i].0 + 2, m + 2).v;
        // println!("{} {}", lans, stree.get(lr[i].0 + 2, m + 2).v,);
        ans = std::cmp::max(ans, lans);
    }
    println!("{}", ans);
}

fn main() {
    solve();
}