// kukan add
// kukan min
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <string>
#include <functional>
#include <utility>
#include <queue>
#include <vector>
#include <string>
#include <stack>
#define rep(i,n) for(ll i=0;i<n;i++)
using namespace std;
typedef long long int ll;
const ll  MOD = 1000000007;
typedef pair<ll,ll> P;
const ll INF = 100100100100100100;
const ll N_MAX = 1 << 18;//100000 kurai

ll N,M;
vector<P> lr;

ll cop_M;
ll dat1[2*N_MAX-1];
ll dat2[2*N_MAX-1];
ll add(ll,ll,ll,ll,ll,ll);
ll secmin(ll,ll,ll,ll,ll);

int main(){
  cin >> N >> M;
  rep(i,N){
    ll a,b;
    cin >> a >> b;
    lr.push_back(make_pair(a,b));
  }
  //init(M);
  sort(lr.begin(),lr.end());

  rep(i,M+1){
    add(i,i+1,i-M,0,0,M+1);
  }
  ll ans = 0;
  rep(i,N){
    P tmp = lr[i];
    add(0,lr[i].second,1,0,0,M+1);
    ans = max(ans,secmin(0,M+1,0,0,M+1)-lr[i].first);
  }
  cout << ans << endl;
}

// [a,b) ni x wo add
//k ha setten no bango [l,r) ni taiou
//(a,b,x,0,0,N) de yobu
ll add(ll a, ll b , ll x , ll k , ll l , ll r){
  if(a <= l && r <= b){
    dat1[k] += x;
  }
  else if(l < b && a < r){
    ll vl = add(a,b,x,k*2+1,l,(l+r)/2);
    ll vr = add(a,b,x,k*2+2,(l+r)/2,r);
    dat2[k] = max(vl,vr);
  }
  return dat1[k]+dat2[k];
}


ll secmin(ll a, ll b , ll k , ll l , ll r){
  if(b <= l || r <= a){
    return -INF;
  }
  else if(a <= l && r <= b){
    return dat1[k] + dat2[k];
  }
  else{
    ll vl = secmin(a,b,k*2+1,l,(l+r)/2);
    ll vr = secmin(a,b,k*2+2,(l+r)/2,r);
    return max(vl,vr) + dat1[k];
  }
}

