#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007LL;
long long base = 37;
long long large = 1000000000000000000LL;
vector<int> addv, maxi;
int ul, ur, uv;
void build(int o, int l, int r) {
  if (l == r) {
    addv[o] = -1000000000;
    maxi[o] = addv[o];
  } else {
    int m = (l + r) / 2;
    build(o * 2, l, m);
    build(o * 2 + 1, m + 1, r);
    maxi[o] = max(maxi[o * 2], maxi[o * 2 + 1]);
  }
}
void update(int o, int l, int r) {
  if (ul <= l && ur >= r) {
    addv[o] += uv;
    if (l == r)
      maxi[o] = addv[o];
    else
      maxi[o] = max(maxi[o * 2], maxi[o * 2 + 1]) + addv[o];
  } else {
    int m = (l + r) / 2;
    if (ul <= m) update(o * 2, l, m);
    if (ur > m) update(o * 2 + 1, m + 1, r);
    maxi[o] = max(maxi[o * 2], maxi[o * 2 + 1]);
    maxi[o] += addv[o];
  }
}
int ql, qr, re;
void query(int o, int l, int r, int add) {
  if (qr < ql) return;
  if (ql <= l && qr >= r) {
    re = max(re, maxi[o] + add);
  } else {
    int m = (l + r) / 2;
    if (ql <= m) query(o * 2, l, m, add + addv[o]);
    if (qr > m) query(o * 2 + 1, m + 1, r, add + addv[o]);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > e;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    e.push_back(pair<int, int>(x, y));
  }
  sort(e.begin(), e.end());
  addv.assign(4 * m + 20, 0);
  maxi.assign(4 * m + 20, -1000000000);
  build(1, 0, m + 1);
  int ans = -1000000000;
  for (int i = 0; i < n; i++) {
    int s = e[i].first;
    int t = e[i].second;
    ql = qr = t;
    re = -1000000000;
    query(1, 0, m + 1, 0);
    if (re < -100000000) {
      ul = ur = t;
      uv = 1000000000 - (m + 1 - t);
      update(1, 0, m + 1);
    }
    ul = 0;
    ur = t;
    uv = 1;
    update(1, 0, m + 1);
    ql = s + 1;
    qr = t;
    re = -1000000000;
    query(1, 0, m + 1, 0);
    ans = max(ans, re - s);
  }
  int maxs = 0;
  int mint = m + 1;
  for (int i = 0; i < n; i++) {
    maxs = max(maxs, e[i].first);
    mint = min(mint, e[i].second);
  }
  if (maxs + 1 >= mint) {
    ans = max(ans, n - m);
  } else {
    ans = max(ans, n - (maxs + m + 1 - mint));
  }
  cout << ans << endl;
  return 0;
}
