#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed main() {
  Int n, m;
  cin >> n >> m;
  vector<Int> s(m + 2, 0), t(m + 2, 0);
  vector<vector<Int> > a(m + 2), b(m + 2);
  vector<Int> ls, rs;
  for (Int i = 0; i < n; i++) {
    Int l, r;
    cin >> l >> r;
    s[l]++;
    t[r]++;
    assert(r != 0);
    a[l].emplace_back(i);
    b[r].emplace_back(i);
    ls.emplace_back(l);
    rs.emplace_back(r);
  }
  for (Int i = 1; i <= m; i++) t[i] += t[i - 1];
  for (Int i = m; i >= 0; i--) s[i] += s[i + 1];
  vector<Int> e(n, 0);
  Int cnt = 0, cur = 0;
  for (Int i = 1; i <= m; i++) {
    if (s[i] == 0 && !b[i].empty() && t[i] - cur == 1) {
      Int k = b[i][0];
      if (!e[k]) {
        e[k] = 1;
        cnt++;
        cur++;
      }
    }
    t[i] -= cur;
  }
  cur = 0;
  for (Int i = m; i >= 1; i--) {
    if (t[i] == 0 && !a[i].empty() && s[i] - cur == 1) {
      Int k = a[i][0];
      if (!e[k]) {
        e[k] = 1;
        cnt++;
        cur++;
      }
    }
    s[i] -= cur;
  }
  vector<Int> s2(m + 2, 0), t2(m + 2, 0);
  for (Int i = 0; i < n; i++) {
    if (e[i]) continue;
    s2[ls[i]]++;
    t2[rs[i]]++;
  }
  for (Int i = 1; i <= m; i++) t2[i] += t2[i - 1];
  for (Int i = m; i >= 0; i--) s2[i] += s2[i + 1];
  vector<Int> c(m);
  for (Int i = 1; i <= m; i++) c[i - 1] = s2[i] + t2[i];
  sort(c.rbegin(), c.rend());
  auto check = [&](Int x) -> Int {
    for (Int i = 0; i < x; i++)
      if (c[i] < x - i) return 0;
    return 1;
  };
  Int l = -1, r = min(n, m) + 1;
  while (l + 1 < r) {
    Int m = (l + r) >> 1;
    if (check(m))
      l = m;
    else
      r = m;
  }
  cout << n - (l + cnt) << endl;
  return 0;
}
