#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))

// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x)<EPS)
#define EQ(a,b) (abs(a-b)<EPS)
typedef complex<Real> P;

int n,m;
pii prefer[252521];

int calc(int id,int l,int r){
  return max(0,prefer[id].first-l+1) + max(0,r-prefer[id].second+1);
}

int main(){
  scanf("%d%d",&n,&m);
  REP(i,n){
    int l,r;
    scanf("%d%d",&l,&r);
    prefer[i] = pii(l,r);
  }
  // greedy
  set<pii> Sx, Sy;  // on board, xcase & ycase
  set<pii> Tx, Ty;  // out of board, xcase & ycase
  REP(i,n){
    int x = prefer[i].first;
    int y = prefer[i].second;
    if(x>=1 && y<=m){
      Sx.insert(pii(x,i));
      Sy.insert(pii(-y,i));
    }else if(x>=1){
      Tx.insert(pii(x,i));
    }else if(y<=m){
      Ty.insert(pii(-y,i));
    }
  }
  int lefter = 1, righter = m;
  int rest = n;
  while(true){
    if(Sx.size() + Tx.size() + Ty.size() == 0)break;
    if(rest==0)break;
    if(lefter > righter)break;
    // check minimum x+y
    int score = 252183025;
    int id = -1;
    int which = 0;
    if(Sx.size()){
      int tid = Sx.begin()->second;
      int tsc = calc(tid,lefter,righter);
      if(tsc < score){
        score = tsc;
        id = tid;
        which = 0;
      }
    }
    if(Sy.size()){
      int tid = Sy.begin()->second;
      int tsc = calc(tid,lefter,righter);
      if(tsc < score){
        score = tsc;
        id = tid;
        which = 1;
      }
    }
    if(Tx.size()){
      int tid = Tx.begin()->second;
      int tsc = calc(tid,lefter,righter);
      if(tsc < score){
        score = tsc;
        id = tid;
        which = 2;
      }
    }
    if(Ty.size()){
      int tid = Ty.begin()->second;
      int tsc = calc(tid,lefter,righter);
      if(tsc < score){
        score = tsc;
        id = tid;
        which = 3;
      }
    }
    if(id==-1)break;
    // erase
    rest--;
    if(which<=1){
      Sx.erase(pii(prefer[id].first,id));
      Sy.erase(pii(-prefer[id].second,id));
    }else if(which==2){
      Tx.erase(pii(prefer[id].first,id));
    }else{
      Ty.erase(pii(-prefer[id].second,id));
    }
    if(which <= 1){
      if(Tx.size() < Ty.size()){
        which = 1;
      }else{
        which = 0;
      }
    }
    // if(which%2==0){
    //   printf("%d -> %d\n",id,lefter);
    // }else{
    //   printf("%d -> %d\n",id,righter);
    // }
    // step
    if(which%2==0){
      lefter++;
      // x
      while(Sx.size()){
        if(Sx.begin()->first < lefter){
          int tid = Sx.begin()->second;
          Sx.erase(Sx.begin());
          Sy.erase(pii(-prefer[tid].second,tid));
          Ty.insert(pii(-prefer[tid].second,tid));
        }else{
          break;
        }
      }
      while(Tx.size()){
        if(Tx.begin()->first < lefter){
          Tx.erase(Tx.begin());
        }else{
          break;
        }
      }
    }else{
      righter--;
      // y
      while(Sy.size()){
        if(-Sy.begin()->first > righter){
          int tid = Sy.begin()->second;
          Sy.erase(Sy.begin());
          Sx.erase(pii(prefer[tid].first,tid));
          Tx.insert(pii(prefer[tid].first,tid));
        }else{
          break;
        }
      }
      while(Ty.size()){
        if(-Ty.begin()->first > righter){
          Ty.erase(Ty.begin());
        }else{
          break;
        }
      }
    }
  }
  printf("%d\n",rest);
  return 0;
}


Measured memory_bytes: 22528