#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) {
  multiset<int> have;
  for (int i = 0; i < x; i++) have.insert(0);
  for (int i = 1; i <= m; i++) have.insert(i);
  multiset<int> R;
  multiset<int> todo;
  for (auto p : lr) {
    if (have.empty()) return false;
    R.insert(p.second);
    if (*have.begin() <= p.first) {
      have.erase(have.begin());
      continue;
    }
    todo.insert(*R.begin());
    R.erase(R.begin());
  }
  vector<int> pos(todo.begin(), todo.end());
  reverse(pos.begin(), pos.end());
  for (int x : pos) {
    auto it = have.lower_bound(x);
    if (it == have.end()) return false;
    have.erase(it);
  }
  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";
}
