#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
using i64 = int64_t;
template <class T>
class segtree {
 public:
  int n;
  vector<T> dat, sum;
  segtree(int size, T x) {
    n = 1;
    while (n < size) n *= 2;
    dat.assign(2 * n - 1, 0);
    sum.assign(2 * n - 1, 0);
    add(0, size, x);
  }
  void add(int a, int b, T x) { add(a, b, x, 0, 0, n); }
  T add(int a, int b, T x, int k, int l, int r) {
    if (b <= l or r <= a) return dat[k];
    if (a <= l and r <= b) {
      sum[k] += x;
      return dat[k] += x;
    }
    int m = (l + r) / 2;
    return dat[k] = max(add(a, b, x, 2 * k + 1, l, m),
                        add(a, b, x, 2 * k + 2, m, r)) +
                    sum[k];
  }
  T query(int a, int b) { return query(a, b, 0, 0, n); }
  T query(int a, int b, int k, int l, int r) {
    if (b <= l or r <= a) return -inf64;
    if (a <= l and r <= b) return dat[k];
    int m = (l + r) / 2;
    return max(query(a, b, 2 * k + 1, l, m), query(a, b, 2 * k + 2, m, r)) +
           sum[k];
  }
};
void solve() {
  i64 N, M;
  cin >> N >> M;
  vector<pair<i64, i64>> ps;
  for (int i = (0); i < (N); ++i) {
    i64 L, R;
    cin >> L >> R;
    ps.push_back(make_pair(L, R));
  }
  sort(ps.begin(), ps.end());
  segtree<i64> seg(M + 2, 0);
  for (int i = (1); i < (M + 1); ++i) seg.add(1, i + 1, -1);
  i64 ans = 0;
  for (auto &p : ps) {
    i64 l = p.first, r = p.second;
    seg.add(0, r + 1, 1);
    ans = max(ans, seg.query(l + 1, M + 2) - l);
  }
  cout << ans << endl;
}
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}
