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<bits/stdc++.h>

using namespace std;
inline void read(long long &x) {
    char ch;
    bool flag = false;
    for (ch = getchar(); !isdigit(ch); ch = getchar())if (ch == '-') flag = true;
    for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
    x = flag ? -x : x;
}
inline void write(long long x) {
    static const long long maxlen = 100;
    static char s[maxlen];
    if (x < 0) {   putchar('-'); x = -x;}
    if (!x) { putchar('0'); return; }
    long long len = 0; for (; x; x /= 10) s[len++] = x % 10 + '0';
    for (long long i = len - 1; i >= 0; --i) putchar(s[i]);
}

inline void read(int &x){
    char ch;
    bool flag = false;
    for (ch = getchar(); !isdigit(ch); ch = getchar())if (ch == '-') flag = true;
    for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
    x = flag ? -x : x;
}

const long long MAXN =310000;

int n,m;

struct kt{
int l,r;
}a[ MAXN ];

bool cmp_l(kt A,kt B){
if ( A.l!=B.l )
    return A.l<B.l;
return A.r<B.r;
}

int tree[ MAXN*7 ];


int query_ans(int p,int l,int r){
if ( l==r )
    return l;
int mid=(l+r)/2;
if ( tree[p*2+1]<(r-(mid+1)+1) )
    return query_ans(p*2+1,mid+1,r);
return query_ans(p*2,l,mid);
}

int query_r(int p,int l,int r,int a,int b){
if( (l==a) && ( r==b) )
    {
        if (tree[p]==(r-l+1))
            return -1;
        return query_ans(p,l,r);
    }
int mid=(l+r)/2;
if ( b<=mid )
    return query_r(p*2,l,mid,a,b);
else
    if ( a>mid )
        return query_r(p*2+1,mid+1,r,a,b);
    else
        {
            int tmp=query_r(p*2+1,mid+1,r,mid+1,b);
            if ( tmp!=-1)
                return tmp;
            return query_r(p*2,l,mid,a,mid);
        }
}

void add(int p,int l,int r,int x){
tree[p]++;
if ( l==r )
    return ;
int mid=(l+r)/2;
if( x<=mid )
    add(p*2,l,mid,x);
else
    add(p*2+1,mid+1,r,x);
}


int main(){
    read(n);read(m);
    int cnt=0;
    int cnt_all=0;
    for (int i=1;i<=n;i++)
        {
            int x,y;
            read(x);read(y);
            if (x==y-1)
            {
                cnt_all++;
                continue;
            }
            ++cnt;
            a[cnt].l=x;
            a[cnt].r=y;
        }
    sort(a+1,a+cnt+1,cmp_l);

    int ans=0;
    int pre=0;
    int l_tot=0;
    for (int i=1;i<=cnt;i++)
        if ( pre<a[i].l )
            {
                pre++;
                l_tot++;
                ans++;
            }

    for (int i=1;i<=cnt-l_tot;i++)
        {
            if ( a[i].r>m )
                continue;
            a[i].r=max(a[i].r,l_tot+1);
            int pos=query_r(1,1,m,1,m-a[i].r+1);
            if ( pos!=-1 )
            {
                add(1,1,m,pos);
                ans++;
            }
        }
    ans=min(m,ans+cnt_all);
    cout<<n-ans<<endl;
    return 0;
}