#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << ' ' << H;
  debug_out(T...);
}
const int INF = 1e9;
const long long MOD = 1e9 + 7;
const int N = 2e5 + 5;
int n, m, SEG[N << 2], LAZY[N << 2];
vector<int> Q[N];
void Apply(int id, int x) { SEG[id] += x, LAZY[id] += x; }
void Shift(int id) {
  if (LAZY[id] == 0) return;
  Apply(id << 1, LAZY[id]), Apply(id << 1 ^ 1, LAZY[id]);
  LAZY[id] = 0;
}
void Update(int l, int r, int x, int id = 1, int s = 0, int e = m + 1) {
  if (l >= e || s >= r) return;
  if (l <= s && e <= r) return Apply(id, x);
  Shift(id);
  Update(l, r, x, id << 1, s, (s + e) >> 1),
      Update(l, r, x, id << 1 ^ 1, (s + e) >> 1, e);
  SEG[id] = max(SEG[id << 1], SEG[id << 1 ^ 1]);
}
int Get(int l, int r, int id = 1, int s = 0, int e = m + 1) {
  if (l >= e || s >= r) return -INF;
  if (l <= s && e <= r) return SEG[id];
  Shift(id);
  return max(Get(l, r, id << 1, s, (s + e) >> 1),
             Get(l, r, id << 1 ^ 1, (s + e) >> 1, e));
}
void Build(int id = 1, int s = 0, int e = m + 1) {
  if (e - s < 2) return Apply(id, s);
  Build(id << 1, s, (s + e) >> 1), Build(id << 1 ^ 1, (s + e) >> 1, e);
  SEG[id] = max(SEG[id << 1], SEG[id << 1 ^ 1]);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  mt19937 Rnd(time(0));
  cin >> n >> m;
  Build();
  for (int i = 0; i < n; i++) {
    int l, r;
    cin >> l >> r;
    Q[l].push_back(r);
  }
  int ans = max(n - m, 0);
  for (int i = 0; i <= m + 1; i++) {
    for (int r : Q[i]) Update(i + 1, r + 1, 1);
    ans = max(ans, Get(i, m + 1) - i - m - 1);
  }
  cout << ans << '\n';
  return 0;
}
