#include <bits/stdc++.h>
using namespace std;
using Int = long long;
template <typename T, typename E>
struct SegmentTree {
  using F = function<T(T, T)>;
  using G = function<T(T, E)>;
  using H = function<E(E, E)>;
  using P = function<E(E, size_t)>;
  Int n;
  F f;
  G g;
  H h;
  T ti;
  E ei;
  P p;
  vector<T> dat;
  vector<E> laz;
  SegmentTree(
      Int n_, F f, G g, H h, T ti, E ei,
      P p =
          [](E a, size_t b) {
            b++;
            return a;
          })
      : f(f), g(g), h(h), ti(ti), ei(ei), p(p) {
    init(n_);
  }
  void init(Int n_) {
    n = 1;
    while (n < n_) n *= 2;
    dat.assign(2 * n - 1, ti);
    laz.assign(2 * n - 1, ei);
  }
  void build(Int n_, vector<T> v) {
    for (Int i = 0; i < n_; 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]);
  }
  inline void eval(Int len, Int k) {
    if (laz[k] == ei) return;
    if (k * 2 + 1 < n * 2 - 1) {
      laz[k * 2 + 1] = h(laz[k * 2 + 1], laz[k]);
      laz[k * 2 + 2] = h(laz[k * 2 + 2], laz[k]);
    }
    dat[k] = g(dat[k], p(laz[k], len));
    laz[k] = ei;
  }
  T update(Int a, Int b, E x, Int k, Int l, Int r) {
    eval(r - l, k);
    if (r <= a || b <= l) return dat[k];
    if (a <= l && r <= b) {
      laz[k] = h(laz[k], x);
      return g(dat[k], p(laz[k], r - l));
    }
    return dat[k] = f(update(a, b, x, k * 2 + 1, l, (l + r) / 2),
                      update(a, b, x, k * 2 + 2, (l + r) / 2, r));
  }
  T update(Int a, Int b, E x) { return update(a, b, x, 0, 0, n); }
  T query(Int a, Int b, Int k, Int l, Int r) {
    eval(r - l, k);
    if (r <= a || b <= l) return ti;
    if (a <= l && r <= b) return dat[k];
    T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return f(vl, vr);
  }
  T query(Int a, Int b) { return query(a, b, 0, 0, n); }
  void update(Int k, T x) {
    query(k, k + 1);
    k += n - 1;
    dat[k] = x;
    while (k) {
      k = (k - 1) / 2;
      dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
};
template <typename T1, typename T2>
inline void chmin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, T2 b) {
  if (a < b) a = b;
}
signed main() {
  Int n, m;
  cin >> n >> m;
  using P = pair<Int, Int>;
  vector<P> vp;
  for (Int i = 0; i < n; i++) {
    Int l, r;
    cin >> l >> r;
    vp.emplace_back(l, r);
  }
  SegmentTree<Int, Int>::F f = [](Int a, Int b) { return max(a, b); };
  SegmentTree<Int, Int>::G g = [](Int a, Int b) { return a + b; };
  const Int INF = 1e9;
  SegmentTree<Int, Int> seg(m + 2, f, g, g, -INF, 0);
  vector<Int> v(m + 2);
  for (Int i = 0; i < m + 2; i++) v[i] = i - (m + 1);
  v.back() = -INF;
  seg.build(m + 2, v);
  Int ans = 0;
  sort(vp.begin(), vp.end());
  for (Int i = 0; i < n; i++) {
    Int l, r;
    tie(l, r) = vp[i];
    seg.update(0, r + 1, 1);
    chmax(ans, seg.query(l + 1, m + 2) - l);
  }
  cout << max(n - m, ans) << endl;
  return 0;
}
