#include <bits/stdc++.h>
using namespace std;
int N, M, L[200000], R[200000];
int ans = 11451419;
int check(int vq) {
  priority_queue<tuple<int, int, int>, vector<tuple<int, int, int> >,
                 greater<tuple<int, int, int> > >
      que, que2;
  for (int i = 0; i < N; i++) que.emplace(L[i], -R[i], i);
  int tail = 1;
  while (!que.empty()) {
    int l, r, idx;
    tie(l, r, idx) = que.top();
    r *= -1;
    que.pop();
    if (tail <= l && tail < vq) {
      tail++;
    } else {
      que2.emplace(-r, l, idx);
    }
  }
  int head = M;
  int ret = 0;
  while (!que2.empty()) {
    int l, r, idx;
    tie(r, l, idx) = que2.top();
    r *= -1;
    que2.pop();
    if (r <= head && head >= vq) {
      head--;
    } else {
      ++ret;
    }
  }
  ans = min(ans, ret);
  return (ret);
}
int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++) cin >> L[i] >> R[i];
  int low = 1, high = M + 1;
  while (high - low > 1) {
    int mid = (low + high) >> 1;
    (check(mid) - check(mid - 1) > 0 ? low : high) = mid;
  }
  for (int i = low - 10; i <= high + 10; i++) check(i);
  cout << ans << endl;
}
