#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct LazySegtreeAdd {
  int n;
  T e;
  vector<T> dat, laz;
  typedef function<T(T a, T b)> Func;
  Func f;
  LazySegtreeAdd(int n_input, Func f, T e_input) : f(f), e(e_input) {
    n = 1;
    while (n < n_input) n <<= 1;
    dat.resize(2 * n - 1, e);
    laz.resize(2 * n - 1, 0);
  }
  void initialize(vector<T>& v) {
    for (int i = 0; i < v.size(); i++) dat[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--) dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
    fill(laz.begin(), laz.end(), 0);
  }
  void eval(int k, int l, int r) {
    if (laz[k] == 0) return;
    dat[k] += laz[k];
    if (r - l > 1) {
      laz[2 * k + 1] = laz[k];
      laz[2 * k + 2] = laz[k];
    }
    laz[k] = 0;
  }
  void add_between(int a, int b, T x) { add(a, b + 1, x, 0, 0, n); }
  void add(int a, int b, T x, int k, int l, int r) {
    eval(k, l, r);
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      laz[k] = x;
      eval(k, l, r);
    } else {
      add(a, b, x, 2 * k + 1, l, (l + r) / 2);
      add(a, b, x, 2 * k + 2, (l + r) / 2, r);
      dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]);
    }
  }
  T get_between(int a, int b) { return query(a, b + 1, 0, 0, n); }
  T query(int a, int b, int k, int l, int r) {
    eval(k, l, r);
    if (r <= a || b <= l) return e;
    if (a <= l && r <= b) return dat[k];
    T vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    T vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return f(vl, vr);
  }
};
int main() {
  int N, M;
  cin >> N >> M;
  static vector<int> L2R[200001];
  for (int i = 0; i < N; i++) {
    int l, r;
    cin >> l >> r;
    L2R[l].push_back(r);
  }
  LazySegtreeAdd<int64_t> st(
      M + 2, [](int64_t a, int64_t b) { return max(a, b); }, -1e9);
  vector<int64_t> init(M + 2);
  for (int i = 0; i <= M + 1; i++) init[i] = i - M - 1;
  st.initialize(init);
  int64_t ans = max(0, N - M);
  for (int l = 0; l < M; l++) {
    for (int r : L2R[l]) st.add_between(0, r, 1);
    ans = max(ans, st.get_between(l + 1, M + 1) - l);
  }
  cout << ans << endl;
  return 0;
}
