#include <bits/stdc++.h>
using namespace std;
const int LG = 20;
const int MN = -1e9;
int mx = 0, crfinvl = 0, n, m, seg[1 << LG] = {}, lazy[1 << LG] = {};
vector<pair<int, int> > interval;
inline void prop(int idx, int L, int R) {
  if (lazy[idx]) {
    seg[idx] -= lazy[idx];
    if (L < R) {
      lazy[2 * idx] += lazy[idx];
      lazy[2 * idx + 1] += lazy[idx];
    }
    lazy[idx] = 0;
  }
}
void build(int idx, int L, int R) {
  if (L < R) {
    int M = (L + R) >> 1;
    build(2 * idx, L, M);
    build(2 * idx + 1, M + 1, R);
    seg[idx] = max(seg[2 * idx], seg[2 * idx + 1]);
  } else {
    seg[idx] = L;
  }
}
void upd(int idx, int L, int R, int x) {
  prop(idx, L, R);
  if (x <= R) {
    if (x <= L) {
      lazy[idx]++;
      prop(idx, L, R);
    } else {
      int M = (L + R) >> 1;
      upd(2 * idx, L, M, x);
      upd(2 * idx + 1, M + 1, R, x);
      seg[idx] = max(seg[2 * idx], seg[2 * idx + 1]);
    }
  }
}
int qry(int idx, int L, int R, int l, int r) {
  prop(idx, L, R);
  if (l <= r) {
    if (l == L && r == R) {
      return seg[idx];
    }
    int M = (L + R) >> 1;
    return max(qry(2 * idx, 1, M, l, min(r, M)),
               qry(2 * idx + 1, M + 1, R, max(l, M + 1), r));
  }
  return MN;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  mx = max(0, n - m);
  for (int i = 0; i < n; i++) {
    pair<int, int> a;
    cin >> a.first >> a.second;
    a.first++;
    a.second--;
    if (a.first <= a.second) {
      interval.push_back(a);
    }
  }
  build(1, 1, m + 1);
  sort(interval.begin(), interval.end());
  int j = 0;
  for (int i = 1; i <= m; i++) {
    crfinvl = max(crfinvl, i);
    for (; j < interval.size() && interval[j].first <= i; j++) {
      upd(1, 1, m + 1, interval[j].second + 1);
      crfinvl = max(crfinvl, interval[j].second);
    }
    mx = max(mx, qry(1, 1, m + 1, i, crfinvl) + j - i - m + 1);
  }
  cout << mx << "\n";
  return 0;
}
