#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
template <typename Monoid>
struct RMQandRAQ {
  RMQandRAQ(int sz, const Monoid &UNITY, const Monoid &DEFAULT = 0)
      : UNITY(UNITY), DEFAULT(DEFAULT) {
    init(sz);
    dat.assign((n << 1) - 1, DEFAULT);
  }
  RMQandRAQ(const vector<Monoid> &a, const Monoid &UNITY,
            const Monoid &DEFAULT = 0)
      : UNITY(UNITY), DEFAULT(DEFAULT) {
    int a_sz = a.size();
    init(a_sz);
    dat.resize((n << 1) - 1);
    for (int i = (0); i < (a_sz); ++i) dat[n - 1 + i] = a[i];
    for (int i = n - 2; i >= 0; --i)
      dat[i] = max(dat[(i << 1) + 1], dat[(i << 1) + 2]);
  }
  void add(int a, int b, Monoid value) { add(a, b, value, 0, 0, n); }
  Monoid query(int a, int b) { return query(a, b, 0, 0, n); }
  Monoid operator[](const int idx) { return query(idx, idx + 1); }
  int find(int a, int b, Monoid value) { return find(a, b, value, 0, 0, n); }

 private:
  int n = 1;
  const Monoid UNITY, DEFAULT;
  vector<Monoid> dat, lazy;
  void init(int sz) {
    while (n < sz) n <<= 1;
    lazy.assign((n << 1) - 1, DEFAULT);
  }
  inline void evaluate(int node) {
    if (lazy[node] != DEFAULT) {
      dat[node] += lazy[node];
      if (node < n - 1) {
        lazy[(node << 1) + 1] += lazy[node];
        lazy[(node << 1) + 2] += lazy[node];
      }
      lazy[node] = DEFAULT;
    }
  }
  void add(int a, int b, Monoid value, int node, int left, int right) {
    evaluate(node);
    if (right <= a || b <= left) return;
    if (a <= left && right <= b) {
      lazy[node] += value;
      evaluate(node);
    } else {
      add(a, b, value, (node << 1) + 1, left, (left + right) >> 1);
      add(a, b, value, (node << 1) + 2, (left + right) >> 1, right);
      dat[node] = max(dat[(node << 1) + 1], dat[(node << 1) + 2]);
    }
  }
  Monoid query(int a, int b, int node, int left, int right) {
    evaluate(node);
    if (right <= a || b <= left) return UNITY;
    if (a <= left && right <= b) return dat[node];
    return max(query(a, b, (node << 1) + 1, left, (left + right) >> 1),
               query(a, b, (node << 1) + 2, (left + right) >> 1, right));
  }
  int find(int a, int b, Monoid value, int node, int left, int right) {
    evaluate(node);
    if (dat[node] < value || right <= a || b <= left) return -1;
    if (right - left == 1) return node - (n - 1);
    int res_l = find(a, b, value, (node << 1) + 1, left, (left + right) >> 1);
    if (res_l != -1) return res_l;
    return find(a, b, value, (node << 1) + 2, (left + right) >> 1, right);
  }
};
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<vector<int> > right(m + 1);
  while (n--) {
    int l, r;
    cin >> l >> r;
    right[l].emplace_back(r);
  }
  RMQandRAQ<int> rmq(m + 2, -INF);
  for (int i = (0); i < (m + 2); ++i) rmq.add(i, i + 1, i);
  int ans = 0;
  for (int l = (0); l < (m + 1); ++l) {
    for (int r : right[l]) rmq.add(0, r + 1, 1);
    ans = max(ans, rmq.query(0, m + 2) - (l + m + 1));
  }
  cout << ans << '\n';
  return 0;
}
