#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define DEBUG(...)
#endif

template <class T> struct range_add {
  static constexpr T inf = numeric_limits<T>::max() / 2;
  struct node { T delta = 0, min = inf, max = -inf, len = 0, sum = 0; };
  static node op(const node& l, const node& r) {
    return {
      l.delta + r.delta,
      std::min(l.min, l.delta + r.min), std::max(l.max, l.delta + r.max),
      l.len + r.len, l.sum + l.delta * r.len + r.sum
    };
  }
  static node make_node(T a) { return {a, a, a, 1, a}; }
  const int n;
  vector<node> t;
  range_add(const vector<T>& v) : n(v.size() + 1), t(2 * n) {
    for (int i = n - 1; i--; ) t[n + i] = make_node(v[i] - (i ? v[i - 1] : 0));
    for (int i = n; i-- > 1; ) t[i] = op(t[2 * i], t[2 * i + 1]);
  }
  node fold(int l, int r) const {
    node a, b;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) a = op(a, t[l++]);
      if (r & 1) b = op(t[--r], b);
    }
    return op(a, b);
  }
  T get(int i) const { return fold(0, i + 1).delta; }
  T min(int l, int r) const { return get(l - 1) + fold(l, r).min; }
  T max(int l, int r) const { return get(l - 1) + fold(l, r).max; }
  T sum(int l, int r) const { return get(l - 1) * (r - l) + fold(l, r).sum; }
  void set(int i, const node& a) {
    t[i += n] = a;
    while (i >>= 1) t[i] = op(t[2 * i], t[2 * i + 1]);
  }
  void add(int l, int r, T a) {
    set(l, make_node(t[n + l].delta + a));
    set(r, make_node(t[n + r].delta - a));
  }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(n);
  map<int, vector<int>> mp;
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i];
    --a[i], --b[i];
    mp[a[i]].push_back(i);
  }
  vector<int> v(m + 1);
  iota(begin(v), end(v), 0);
  range_add<int> ra(v);
  int res = 0;
  for (int l = 0; l <= m; ++l) {
    for (int i : mp[l - 1]) {
      ra.add(0, b[i] + 1, 1);
    }
    res = max(res, ra.max(l, m + 1) - (m + l));
  }
  cout << res << '\n';
}
