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

#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;
const int MAX_V = 5010;
ll INF = 100000000000;
struct edge{ll to,cap,rev;};
vector<edge> G[MAX_V];
bool used[MAX_V];
ll b[MAX_V],f[MAX_V],can[MAX_V][MAX_V];
vector<int> v,w;
void add_edge(ll from,ll to,ll cap){
    G[from].push_back({to,cap,(ll)G[to].size()});
    G[to].push_back({from,0,(ll)G[from].size() - 1});
}

ll dfs(int s,int t,ll val){
    for(edge e:G[s]){
        if(used[e.to]) continue;
        b[e.to] += e.cap;
        if(b[e.to]>=val){
            used[e.to] = true;
            v.push_back(e.to); w.push_back(e.to);
        }
    }
    if(v.size()==0) return 0;
    int vv = v.back();
    if(vv==t) return 1;
    v.pop_back();
    return dfs(vv,t,val);
}

ll rev_dfs(int s,int t,ll val){
    for(int i=0;i<MAX_V;i++) f[i] = 0;
    f[t] = val;
    while(w.size()){
        int now = w.back();
        w.pop_back();
        used[now] = false;
        for(edge ee:G[now]){
            int from = ee.to;
            if(!used[from]) continue;
            int j = can[from][now];
            if(j==-1) continue;
            edge &e = G[from][j];
            ll f_ = min(f[now],e.cap);
            e.cap -= f_;
            G[e.to][e.rev].cap += f_;
            f[now] -= f_; f[from] += f_;
        }
    }
}

ll max_flow(int s,int t){
    ll flow = 0,val = 0;
    int i,j;
    for(i=0;i<MAX_V;i++){
        b[i] = 0;
        for(j=0;j<MAX_V;j++){
            can[i][j]= -1;
        }
        for(j=0;j<G[i].size();j++){
            can[i][G[i][j].to] = j;
            val = max(val,G[i][j].cap);
        }
    }
    while(val){
        v.clear(); w.clear(); w.push_back(s);
        for(i=0;i<MAX_V;i++){
            b[i] = 0; used[i] = false;
        }
        used[s] = true;
        if(!dfs(s,t,val)){
            val /= 2;
            continue;
        }
        rev_dfs(s,t,val);
        flow += val;
    }
    return flow;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int s = n + 3 * m, t = s + 1;
  for (int i = 0; i < n; ++i) {
    int l, r;
    cin >> l >> r;
    --r;
    add_edge(s, i, 1);
    if (l) {
      add_edge(i, n + (l - 1), 1);
    }
    if (r < m) {
      add_edge(i, n + m + r, 1);
    }
  }
  for (int j = 0; j < m; ++j) {
    add_edge(n + j, n + 2 * m + j, 1);
    add_edge(n + m + j, n + 2 * m + j, 1);
    add_edge(n + 2 * m + j, t, 1);
    if (j) {
      add_edge(n + j, n + (j - 1), INF);
    }
    if (j < m - 1) {
      add_edge(n + m + j, n + m + (j + 1), INF);
    }
  }
  cout << n - max_flow(s, t) << endl;
}