#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 100;
int n, m;
struct data_type {
  int l, r, idx;
  bool operator<(const data_type &rhs) const {
    if (this->r == rhs.r) {
      if (this->l == rhs.l) return this->idx < rhs.idx;
      return this->l < rhs.l;
    }
    return this->r > rhs.r;
  }
} arr[MAXN];
vector<int> which_l[int(1e6 + 3)];
int solve(int mid) {
  vector<data_type> not_taken;
  vector<bool> occ(m + 1, false);
  for (int i = 0; i <= m; i++) {
    which_l[i].clear();
  }
  for (int i = 0; i < n; i++) {
    if (arr[i].l > mid || arr[i].l == 0) {
      not_taken.push_back(arr[i]);
    } else {
      which_l[arr[i].l].push_back(i);
    }
  }
  set<data_type> s;
  for (int i = mid; i > 0; i--) {
    for (auto &elem : which_l[i]) {
      s.insert(arr[elem]);
    }
    if (s.size() > 0) {
      occ[i] = true;
      s.erase(s.begin());
    }
  }
  for (auto &elem : s) {
    not_taken.push_back(elem);
  }
  sort(not_taken.begin(), not_taken.end(),
       [&](const data_type &lhs, const data_type &rhs) {
         return lhs.r < rhs.r;
       });
  int iter = 1, idx = 0;
  for (; idx < not_taken.size();) {
    if (occ[iter] == false && not_taken[idx].r <= iter) {
      idx++;
    }
    if (idx == not_taken.size()) break;
    iter++;
  }
  return max(max(mid, m), iter);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> arr[i].l >> arr[i].r;
    arr[i].idx = i;
  }
  int lower = 1, upper = 1e6, mid;
  while (lower < upper) {
    mid = (lower + upper) / 2;
    if (solve(mid) < solve(mid + 1)) {
      upper = mid;
    } else {
      lower = mid + 1;
    }
  }
  cout << solve(lower) - m << '\n';
  return 0;
}
