#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m;
int l[N], r[N];
priority_queue<pair<int, int> > pq1;
priority_queue<int> pq2;
bool check(int val) {
  while (!pq1.empty()) {
    pq1.pop();
  }
  while (!pq2.empty()) {
    pq2.pop();
  }
  for (int i = 1; i <= n; ++i) {
    pq1.push({r[i], -l[i]});
  }
  for (int i = 1 - val; i <= m; ++i) {
    bool ok = 0;
    while (!pq1.empty()) {
      auto it = pq1.top();
      pq1.pop();
      int l = -it.second;
      int r = it.first;
      if (i <= l) {
        ok = 1;
        break;
      }
      pq2.push(-r);
    }
    if (!ok) {
      if (!pq2.empty()) {
        if (-pq2.top() <= i) {
          ok = 1;
          pq2.pop();
        }
      }
    }
  }
  return pq1.empty() && pq2.empty();
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d %d", l + i, r + i);
  }
  int l = 0;
  int r = n;
  while (l < r) {
    int mid = l + r >> 1;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  printf("%d\n", l);
}
