#include <bits/stdc++.h>
using namespace std;
template <typename T>
class SegTreeLazy {
  size_t n;
  int h;
  vector<T> data;
  vector<T> lazy;
  const T identity_element = -2e9;
  static T combine(T L, T R) { return max(L, R); }
  void apply(size_t i, T v) {
    data[i] += v;
    if (i < n) lazy[i] += v;
  }
  void build(size_t i) {
    while (i > 1)
      i >>= 1, data[i] = combine(data[i * 2], data[i * 2 + 1]) + lazy[i];
  }
  void push(size_t p) {
    for (int s = h; s > 0; --s) {
      size_t i = p >> s;
      if (lazy[i] != 0) {
        apply(i * 2, lazy[i]);
        apply(i * 2 + 1, lazy[i]);
        lazy[i] = 0;
      }
    }
  }
  int calc_h(size_t nn) {
    int hh = 1;
    for (; nn > 1; ++hh, nn >>= 1)
      ;
    return hh;
  }

 public:
  SegTreeLazy(size_t _n)
      : n(_n), h(calc_h(n)), data(vector<T>(2 * n, 0)), lazy(vector<T>(n, 0)) {}
  SegTreeLazy(const vector<T> &src)
      : n(src.size()),
        h(calc_h(n)),
        data(vector<T>(2 * n, 0)),
        lazy(vector<T>(n, 0)) {
    for (int i = 0; i != n; ++i) data[n + i] = src[i];
    for (int i = n - 1; i != 0; --i)
      data[i] = combine(data[i * 2], data[i * 2 + 1]);
  }
  void inc(size_t L, size_t R, T v) {
    L += n;
    R += n;
    size_t L0 = L;
    size_t R0 = R;
    for (; L < R; L >>= 1, R >>= 1) {
      if (L & 1) apply(L++, v);
      if (R & 1) apply(--R, v);
    }
    build(L0);
    build(R0 - 1);
  }
  T query(size_t L, size_t R) {
    L += n;
    R += n;
    push(L);
    push(R - 1);
    T ret = identity_element;
    for (; L < R; L >>= 1, R >>= 1) {
      if (L & 1) ret = combine(ret, data[L++]);
      if (R & 1) ret = combine(data[--R], ret);
    }
    return ret;
  }
};
int solve(int N, int M, const vector<pair<int, int>> &LRs) {
  vector<vector<int>> Rs(M + 1, vector<int>());
  for (const auto &LR : LRs) Rs[LR.first].push_back(LR.second);
  vector<int> initial_values(M + 2, 0);
  for (int i = 0; i <= M + 1; ++i) initial_values[i] = i - M - 1;
  SegTreeLazy<int> seg(initial_values);
  int ans = 0;
  for (int L = 0; L <= M; ++L) {
    for (auto R : Rs[L]) {
      seg.inc(0, R + 1, 1);
      ans = max(ans, seg.query(0, M + 2) - L);
    }
  }
  ans = max(ans, M - N);
  return ans;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> LRs(N);
  for (auto &LR : LRs) cin >> LR.first >> LR.second;
  cout << solve(N, M, LRs) << endl;
  return 0;
}
