#include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int N,M;
int L[222222],R[222222];
vint lis[222222];

int sum[4444444];

bool C(int x){
    priority_queue<int,vint,greater<int>>que;

    vint v;

    rep(i,M+1){
        for(auto r:lis[i])que.push(r);
        while(que.size()>i){
            v.pb(que.top());
            que.pop();
        }
    }

    rep(i,v.size())v[i]=M+1-v[i]+x;
    memset(sum,0,sizeof(sum));
    rep(i,v.size())sum[v[i]]++;
    rep(i,444444){
        if(i)sum[i]+=sum[i-1];
        if(sum[i]>i)return false;
    }
    return true;
}

signed main(){
    cin>>N>>M;
    rep(i,N)cin>>L[i]>>R[i];

    rep(i,N)lis[L[i]].pb(R[i]);

    int lb=max(0ll,N-M)-1,ub=N;
    while(ub-lb>1){
        int mid=(ub+lb)/2;
        if(C(mid))ub=mid;
        else lb=mid;
    }
    cout<<ub<<endl;
    return 0;
}
