#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
using ll = long long;
using R = long double;
const R EPS = 1e-9L;
inline int sgn(const R &r) { return (r > EPS) - (r < -EPS); }
inline R sq(R x) { return sqrt(max(x, 0.0L)); }
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const pid_t pid = getpid();
const int limit = 200010;
vector<int> events[limit];
template <int depth>
struct Segment_tree {
  const static int h = depth;
  const static int n = 1 << h;
  using T = long long;
  T data[2 * n], lazy[2 * n];
  const T out = 0;
  inline T vmerge(T l, T r) { return max(l, r); }
  void init() {
    fill_n(data, 2 * n, out);
    fill_n(lazy, 2 * n, out);
  }
  void apply(T v, int p, int l, int r) {
    data[p] += v;
    lazy[p] += v;
  }
  void push(int p, int l, int r) {
    const int m = (l + r) / 2;
    if (lazy[p] == out) return;
    apply(lazy[p], 2 * p + 1, l, m);
    apply(lazy[p], 2 * p + 2, m, r);
    lazy[p] = out;
  }
  void range_update(int a, int b, T x, int k = 0, int l = 0, int r = n) {
    if (r <= a or b <= l) return;
    if (a <= l and r <= b) return apply(x, k, l, r);
    push(k, l, r);
    const int m = (l + r) / 2;
    range_update(a, b, x, k * 2 + 1, l, m);
    range_update(a, b, x, k * 2 + 2, m, r);
    data[k] = vmerge(data[k * 2 + 1], data[k * 2 + 2]);
  }
  T range_query(int a, int b, int k = 0, int l = 0, int r = n) {
    if (r <= a or b <= l) return out;
    if (a <= l and r <= b) return data[k];
    push(k, l, r);
    const int m = (l + r) / 2;
    T vl = range_query(a, b, k * 2 + 1, l, m);
    T vr = range_query(a, b, k * 2 + 2, m, r);
    return vmerge(vl, vr);
  }
};
Segment_tree<18> seg;
int main(void) {
  int n, m;
  cin >> n >> m;
  int ans = max(0, n - m);
  for (int i = int(0); i < int(n); ++i) {
    int l, r;
    cin >> l >> r;
    events[l].push_back(r);
  }
  for (int l = int(1); l < int(m + 1); ++l) {
    seg.range_update(l, m + 1, -1);
  }
  for (auto &r : events[0]) {
    seg.range_update(0, r + 1, 1);
  }
  for (int l = int(1); l < int(m + 1); ++l) {
    chmax<int>(ans, seg.range_query(l, m + 2) - l);
    for (auto &r : events[l]) {
      seg.range_update(0, r + 1, 1);
    }
    seg.range_update(l, m + 1, 1);
  }
  cout << ans << endl;
  return 0;
}
