#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const ld EPS = 1e-10;
template <typename T>
bool chmax(T &x, const T &y) {
  return (x < y) ? (x = y, true) : false;
};
template <typename T>
bool chmin(T &x, const T &y) {
  return (x > y) ? (x = y, true) : false;
};
template <typename Monoid>
struct Starry_Sky_Tree {
  Monoid ope(Monoid a, Monoid b) { return max(a, b); }
  Monoid unit;
  int n;
  vector<Monoid> seg, lazy;
  Starry_Sky_Tree(int N, Monoid x) {
    unit = x;
    n = 1;
    while (n < N) n <<= 1;
    seg.assign(2 * n, 0);
    lazy.assign(2 * n, 0);
  }
  void add(int a, int b, Monoid x, int i = 1, int l = 0, int r = -1) {
    if (r < 0) r = n;
    if (a >= r || b <= l) return;
    if (a <= l && r <= b) {
      lazy[i] += x;
      return;
    }
    add(a, b, x, 2 * i, l, (l + r) / 2);
    add(a, b, x, 2 * i + 1, (l + r) / 2, r);
    seg[i] = ope(seg[2 * i] + lazy[2 * i], seg[2 * i + 1] + lazy[2 * i + 1]);
  }
  Monoid query(int a, int b, int i = 1, int l = 0, int r = -1) {
    if (r < 0) r = n;
    if (a >= r || b <= l) return unit;
    if (a <= l && r <= b) return seg[i] + lazy[i];
    Monoid vl = query(a, b, 2 * i, l, (l + r) / 2);
    Monoid vr = query(a, b, 2 * i + 1, (l + r) / 2, r);
    return ope(vl, vr) + lazy[i];
  }
  void update(int i = 1, int l = 0, int r = -1) {
    if (r < 0) r = n;
    seg[i] = seg[i] + lazy[i];
    if (r - l > 1) {
      lazy[2 * i] += lazy[i];
      lazy[2 * i + 1] += lazy[i];
      update(2 * i, l, (l + r) / 2);
      update(2 * i + 1, (l + r) / 2, r);
    }
    lazy[i] = 0;
  }
  Monoid at(int i) { return seg[n + i]; }
  void clear() {
    fill(seg.begin(), seg.end(), 0);
    fill(lazy.begin(), lazy.end(), 0);
  }
};
int main() {
  int N, M;
  cin >> N >> M;
  pii p[N];
  for (int i = 0; i < N; i++) {
    int L, R;
    cin >> L >> R;
    p[i] = pii(L, R);
  }
  sort(p, p + N);
  Starry_Sky_Tree<int> seg(M, -inf);
  for (int i = 0; i < M; i++) seg.add(i, i + 1, i - M);
  int ans = max(0, N - M);
  for (int i = 0; i < N; i++) {
    seg.add(0, p[i].second, 1);
    chmax(ans, seg.query(p[i].first, M) - p[i].first);
  }
  cout << ans << endl;
}
