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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <tuple>
#include <random>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef complex<double> comp;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};
const int inf = 1e9 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

int n, m;
P lr[200100];

// Range Add Query
template<typename T>
class SegmentTree {
    class Node {
    public:
        ll l, r;
        T data;
        T lazy;
        Node* child_L;
        Node* child_R;
        Node(ll l, ll r, T data) {
            this->l = l;
            this->r = r;
            this->data = data;
            this->lazy = 0;
            this->child_L = nullptr;
            this->child_R = nullptr;
        }

        void lazy_evaluate() {
            //this->data += this->lazy * (this->r - this->l); // sum
            this->data += this->lazy;                       // min, max
            if (this->r - this->l > 1) {
                if (this->child_L == nullptr) {
                    ll mid = (this->l + this->r) / 2;
                    this->child_L = new Node(this->l, mid, init);
                    this->child_R = new Node(mid, this->r, init);
                }
                this->child_L->lazy += this->lazy;
                this->child_R->lazy += this->lazy;
            }
            this->lazy = 0;
        }
    };

    Node* root;
    ll N;
    static const T init = -1000000000;
    T op(const T& a, const T& b) {
        return max(a, b);
    }
public:
    SegmentTree(ll n) {
        this->N = n;
        this->root = new Node(0, n, init);
    }

    void update(Node* node, ll k, T a) {
        if (node->l <= k && k < node->r) {
            if (node->l == k && node->r == k+1) {
                node->data = a;
            } else {
                node->lazy_evaluate();
                update(node->child_L, k, a);
                update(node->child_R, k, a);
                node->data = op(node->child_L->data, node->child_R->data);
            }
        } else {
            node->lazy_evaluate();
        }
    }
    void update(ll k, T a) {
        update(this->root, k, a);
    }

    void add(Node* node, ll k, T a) {
        if (node->l <= k && k < node->r) {
            if (node->l == k && node->r == k+1) {
                node->lazy_evaluate();
                node->data += a;
            } else {
                node->lazy_evaluate();
                add(node->child_L, k, a);
                add(node->child_R, k, a);
                node->data = op(node->child_L->data, node->child_R->data);
            }
        } else {
            node->lazy_evaluate();
        }
    }
    void add(ll k, T a) {
        add(this->root, k, a);
    }

    void add_range(Node* node, ll l, ll r, T a) {        
        if (node->r <= l || r <= node->l) {
            node->lazy_evaluate();
            return;
        }
        if (l <= node->l && node->r <= r) {
            node->lazy += a;
            node->lazy_evaluate();
        } else {
            node->lazy_evaluate();
            add_range(node->child_L, l, r, a);
            add_range(node->child_R, l, r, a);
            node->data = op(node->child_L->data, node->child_R->data);
        }
    }
    void add_range(ll l, ll r, T a) {
        if (l < r)
            add_range(this->root, l, r, a);
    }

    T query(Node* node, ll l, ll r) {
        if (node == nullptr || node->r <= l || r <= node->l) return init;
        node->lazy_evaluate();
        if (l <= node->l && node->r <= r) return node->data;
        return op(query(node->child_L, l, r), query(node->child_R, l, r));
    }
    
    T query(ll l, ll r) {
        return query(this->root, l, r);
    }

    T get(ll i) {
        return query(this->root, i, i+1);
    }
};

int solve() {
    SegmentTree<int> sgt(m+3);
    for (int i = m+1; i >= 0; i--) {
        sgt.update(i, i-m-1);
    }
    sort(lr, lr+n);
    int res = n - m;
    for (int i = 0; i < n; i++) {
        sgt.add_range(0, lr[i].second+1, 1);
        res = max(res, sgt.query(0, m+2) - lr[i].first);
    }
    return res;
}

void input() {
    cin >> n >> m;
    int l, r;
    for (int i = 0; i < n; i++) {
        cin >> l >> r;
        lr[i] = P(l, r);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);

    input();
    cout << solve() << endl;
}