#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  for (auto i = begin(v); i != end(v); i++)
    os << *i << (i == end(v) - 1 ? "" : " ");
  return os;
}
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto i = begin(v); i != end(v); i++) is >> *i;
  return is;
}
vector<pair<int, int>> inp;
bool check(int added, int m) {
  multiset<int> rs;
  for (auto x : inp) {
    rs.insert(x.second);
    while (((int)rs.size() - added) > x.first) {
      rs.erase(rs.find(*rs.begin()));
    }
  }
  multiset<int> all_rs;
  for (auto x : inp) {
    all_rs.insert(x.second);
  }
  for (auto x : rs) {
    all_rs.erase(all_rs.find(x));
  }
  int cnt = 0;
  while (all_rs.size() > 0) {
    int val = *all_rs.rbegin();
    all_rs.erase(all_rs.find(*all_rs.rbegin()));
    if (val > m) return false;
    cnt++;
    if ((m - val + 1) < cnt) return false;
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    inp.push_back({a, b});
  }
  sort(inp.begin(), inp.end());
  check(2, m);
  int left = 0, right = (n + m + 1);
  while (left < right) {
    int mm = (left + right) / 2;
    if (check(mm, m)) {
      right = mm;
    } else {
      left = mm + 1;
    }
  }
  cout << left;
  return 0;
}
