#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using ld = long double;
using pll = pair<ll, ll>;
vector<pii> lr;
int n, m;
bool can(int x) {
  vector<int> have;
  for (int i = 0; i < x; i++) have.push_back(0);
  for (int i = 1; i <= m; i++) have.push_back(i);
  priority_queue<int> R;
  vector<int> pos;
  reverse(have.begin(), have.end());
  for (auto p : lr) {
    if (have.empty()) return false;
    if (have.back() <= p.first) {
      have.pop_back();
      continue;
    }
    R.push(-p.second);
    pos.push_back(-R.top());
    R.pop();
  }
  sort(pos.begin(), pos.end());
  int ptr = 0;
  reverse(have.begin(), have.end());
  for (int p : pos) {
    while (ptr < (int)have.size() && have[ptr] < p) ptr++;
    if (ptr == (int)have.size()) return false;
    ptr++;
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.setf(ios::fixed);
  cout.precision(12);
  cin >> n >> m;
  lr.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> lr[i].first >> lr[i].second;
  }
  sort(lr.begin(), lr.end());
  int low = -1;
  int high = n;
  while (high - low > 1) {
    int mid = (low + high) / 2;
    if (can(mid))
      high = mid;
    else
      low = mid;
  }
  cout << high << "\n";
}
