#include <bits/stdc++.h>
using namespace std;
struct query {
  long long l, r;
};
bool sorter(const query& a, const query& b) {
  if (a.l == b.l) {
    return a.r > b.r;
  } else
    return a.l < b.l;
}
bool sorter2(const query& a, const query& b) {
  if (a.r == b.r) {
    return a.l < b.l;
  } else
    return a.r > b.r;
}
long long n, m;
query qs[311111];
bool used[311111];
int main() {
  cin.sync_with_stdio(false);
  cin >> n >> m;
  for (long long i = (long long)(0); i < (long long)(n); i++) {
    long long a, b;
    cin >> a >> b;
    qs[i] = (query){a, b};
  }
  long long res1 = n, res2 = n;
  {
    set<long long> ch, ch2;
    memset(used, 0, sizeof(used));
    sort(qs, qs + n, sorter);
    for (long long i = (long long)(0); i < (long long)(m); i++) {
      ch.insert(-(i + 1));
      ch2.insert(i + 1);
    }
    for (long long i = (long long)(0); i < (long long)(n); i++) {
      long long ql = qs[i].l;
      if (ql == 0) continue;
      auto tmp = ch.lower_bound(-ql);
      if (tmp == ch.end()) continue;
      if (-(*tmp) <= ql) {
        used[i] = true;
        res1--;
        ch.erase(tmp);
        ch2.erase(-(*tmp));
      }
    }
    vector<long long> qrs;
    for (long long i = (long long)(0); i < (long long)(n); i++) {
      long long qr = qs[i].r;
      if (used[i] || qr == m + 1) continue;
      qrs.push_back(qr);
    }
    sort((qrs).begin(), (qrs).end(), greater<long long>());
    for (long long qr : qrs) {
      auto tmp = ch2.lower_bound(qr);
      if (tmp == ch2.end()) continue;
      if (qr <= *tmp) {
        res1--;
        ch2.erase(tmp);
      }
    }
  }
  {
    set<long long> ch, ch2;
    memset(used, 0, sizeof(used));
    sort(qs, qs + n, sorter2);
    for (long long i = (long long)(0); i < (long long)(m); i++) {
      ch.insert(-(i + 1));
      ch2.insert(i + 1);
    }
    for (long long i = (long long)(0); i < (long long)(n); i++) {
      long long qr = qs[i].r;
      if (qr == m + 1) continue;
      auto tmp = ch2.lower_bound(qr);
      if (tmp == ch2.end()) continue;
      if (qr <= (*tmp)) {
        used[i] = true;
        res2--;
        ch2.erase(tmp);
        ch.erase(-(*tmp));
      }
    }
    vector<long long> qls;
    for (long long i = (long long)(0); i < (long long)(n); i++) {
      long long ql = qs[i].l;
      if (used[i] || ql == 0) continue;
      qls.push_back(ql);
    }
    sort((qls).begin(), (qls).end());
    for (long long ql : qls) {
      auto tmp = ch.lower_bound(-ql);
      if (tmp == ch.end()) continue;
      if (-(*tmp) <= ql) {
        res2--;
        ch.erase(tmp);
      }
    }
  }
  cout << max(min(res1, res2), m - n) << endl;
  return 0;
}
