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

// #define int long long

struct RLUQ {
  struct query {
    int type;
    int value;
    query(int a = 0, int b = 0): type(a), value(b) {}
  };
  vector<query> s;
  vector<int> t;
  RLUQ() {
    // fill(t, t+(1<<20), INT_MAX);
    s.resize(1<<20);
    t.resize(1<<20,INT_MAX);
  }
  inline void compute(int k, int l, int r) {
    query q = s[k];
    s[k].type = 0;
    if ( q.type == 0 || r-l == 1 ) return;
    s[k*2+1] = s[k*2+2] = q;
    t[k*2+1] = t[k*2+2] = q.value;
  }

  inline void Set(int a, int b, int x, int k = 0, int l = 0, int r = (1<<19)) {
    if ( b <= l || r <= a ) return;
    compute(k, l, r);
    if ( a <= l && r <= b ) {
      s[k] = query(1, x);
      t[k] = x;
    } else {
      int m = (l+r)/2;
      Set(a, b, x, k*2+1, l, m);
      Set(a, b, x, k*2+2, m, r);
      t[k] = min(t[k*2+1], t[k*2+2]);
    }
  }

  inline int Get(int a, int b, int k = 0, int l = 0, int r = (1<<19)) {    
    if ( b <= l || r <= a ) return INT_MAX;
    compute(k, l, r);
    if ( a <= l && r <= b ) {
      return t[k];
    } else {
      int m = (l+r)/2;
      int lv = Get(a, b, k*2+1, l, m);
      int rv = Get(a, b, k*2+2, m, r);
      return min(lv, rv);
    }
  }
};

signed main() {
  int N, M;
  scanf("%d %d", &N, &M);

  vector<int> L(N), R(N); 
  int ma_v = 0;
  unordered_map<int, int> zip, unzip;
  int cnt = 0;
  {
    map<int, bool> use;
    use[0] = use[M] = true;
    for ( int i = 0; i < N; i++ ) {
      scanf("%d %d", &L[i], &R[i]);
      ma_v = max(ma_v, R[i]);      
      use[L[i]] = true;
      use[R[i]] = true;
    }
    for ( auto e : use ) {
      zip[e.first] = cnt++;
      unzip[cnt-1] = e.first;
    }
  }

  RLUQ rmin, lmax;
  /*
  for ( int i = 0; i < N; i++ ) {
    int l = zip[L[i]], r = zip[R[i]];   
    rmin.Set(l, r+1, r);
    lmax.Set(l, r+1, -l);
  }
  */
  using P = pair<int, int>;
  {
    vector<P> v(N);
    for ( int i = 0; i < N; i++ ) v[i] = P(L[i], R[i]);
    sort(v.begin(), v.end());
    for ( int i = 0; i < N; i++ ) {
    int l = zip[v[i].first], r = zip[v[i].second];
    lmax.Set(l, r+1, -v[i].first);  
    }
  }
  {
    vector<P> v(N);
    for ( int i = 0; i < N; i++ ) v[i] = P(R[i], L[i]);
    sort(v.begin(), v.end(), greater<P>());
    for ( int i = 0; i < N; i++ ) {
      int l = zip[v[i].second], r = zip[v[i].first];
      rmin.Set(l, r+1, v[i].first);
    }
  }

  vector<int> imos(cnt+1, 0);
  for ( int i = 0; i < N; i++ ) {
    int l = zip[L[i]], r = zip[R[i]];
    imos[l]++;
    imos[r+1]--;
  }
  
  int ma = 0;
  vector<int> mas;  
  for ( int i = 1; i < cnt+1; i++ ) {
    imos[i] += imos[i-1];
    ma = max(ma, imos[i]);    
  }

  if ( ma == 1 ) {
    cout << ma_v << endl;
    return 0;
  }

  long long  ans;
  if ( ma % 2 == 0 ) ans = 1e9+7;
  else ans = 0;  
  for ( int i = 1; i < cnt+1; i++ ) {
    if ( imos[i] != ma ) continue;
    if ( ma % 2 ) ans = max(ans, (long long)rmin.Get(i, i+1));
    else ans = min(ans, (long long)(-lmax.Get(i, i+1)));
    if(i>50000)break;
  }
  for ( int j = 1; j < cnt+1; j++ ) {
    int i=cnt+1-j;
    if ( imos[i] != ma ) continue;
    if ( ma % 2 ) ans = max(ans, (long long)rmin.Get(i, i+1));
    else ans = min(ans, (long long)(-lmax.Get(i, i+1)));
    if(j>50000)break;
    
  }

  if ( ma % 2 == 0 ) ans = M - (ans);
  cout << (long long)M*(long long)(ma-1) + ans << endl;
  
  // return 0;
}



Measured memory_bytes: 64564