#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ld M_PI = acos(-1.0);
const ld eps = 1e-9;
const int INF = 2000000000;
const ll LINF = 1ll * INF * INF;
const ll MOD = 1000000007;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> l(n), r(n);
  for (int i = 0; i < n; ++i) cin >> l[i] >> r[i];
  vector<vector<int>> pos(m + 2);
  for (int i = 0; i < n; ++i) pos[l[i]].emplace_back(i);
  multiset<int> rgt;
  vector<int> poss(m + 2);
  for (int i = 0; i < m + 2; ++i) {
    for (int x : pos[i]) rgt.insert(r[x]);
    while (rgt.size() > i) {
      auto it = rgt.begin();
      ++poss[*it];
      rgt.erase(it);
    }
  }
  int pl = 0, ans = 0;
  for (int i = m + 2 - 1; i >= 0; --i) {
    int x = min(pl, poss[i]);
    pl -= x;
    ans += poss[i] - x;
    ++pl;
  }
  cout << ans << '\n';
}
