#include <bits/stdc++.h>
using namespace std;
int n, m, jaw, l[300003], r[300003], maks[300003 * 4], lazy[300003 * 4];
vector<int> ve[300003];
void build(int idx, int l, int r) {
  if (l == r) {
    maks[idx] = -(m - l + 1);
    return;
  }
  int mid = (l + r) >> 1;
  build(2 * idx, l, mid);
  build(2 * idx + 1, mid + 1, r);
  maks[idx] = max(maks[idx * 2], maks[idx * 2 + 1]);
}
void upd(int idx, int l, int r, int fl, int fr, int val) {
  if (fl > r || fr < l) return;
  if (fl <= l && r <= fr) {
    maks[idx] += val;
    lazy[idx] += val;
    return;
  }
  int mid = (l + r) >> 1;
  if (lazy[idx] != 0) {
    maks[idx * 2] += lazy[idx];
    maks[idx * 2 + 1] += lazy[idx];
    lazy[idx * 2] += lazy[idx];
    lazy[idx * 2 + 1] += lazy[idx];
    lazy[idx] = 0;
  }
  upd(2 * idx, l, mid, fl, fr, val);
  upd(2 * idx + 1, mid + 1, r, fl, fr, val);
  maks[idx] = max(maks[idx * 2], maks[idx * 2 + 1]);
}
int cari(int idx, int l, int r, int fl, int fr) {
  if (fl > r || fr < l) return -1e9;
  if (fl <= l && r <= fr) return maks[idx];
  int mid = (l + r) >> 1;
  if (lazy[idx] != 0) {
    maks[idx * 2] += lazy[idx];
    maks[idx * 2 + 1] += lazy[idx];
    lazy[idx * 2] += lazy[idx];
    lazy[idx * 2 + 1] += lazy[idx];
    lazy[idx] = 0;
  }
  return max(cari(2 * idx, l, mid, fl, fr),
             cari(2 * idx + 1, mid + 1, r, fl, fr));
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> l[i] >> r[i];
    ve[l[i]].push_back(i);
  }
  jaw = max(jaw, n - m);
  build(1, 1, m);
  for (int i = 1; i <= n; i++) upd(1, 1, m, 1, r[i], 1);
  for (int i = m; i >= 0; i--) {
    for (int j : ve[i + 1]) {
      upd(1, 1, m, 1, r[j], -1);
    }
    jaw = max(jaw, cari(1, 1, m, i + 2, m) - i);
  }
  cout << jaw << endl;
}
