#include <bits/stdc++.h>
using namespace std;
void _dbg(string) { cerr << endl; }
template <class H, class... T>
void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cerr << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.first << "," << p.second << ")";
  return o;
}
template <class T>
ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}
template <typename T>
class SegTree {
 public:
  int n;
  vector<T> segMax, segAdd;
  inline void calc(int v) {
    int vl = 2 * v, vr = 2 * v + 1;
    segMax[v] = max(segMax[vl] + segAdd[vl], segMax[vr] + segAdd[vr]);
  }
  inline void apply(int v, T x) { segAdd[v] += x; }
  void add(int l, int r, T x) {
    bool cl = false, cr = false;
    for (l += n, r += n; l < r; l /= 2, r /= 2) {
      if (cl) calc(l - 1);
      if (cr) calc(r);
      if (l & 1) apply(l++, x), cl = true;
      if (r & 1) apply(--r, x), cr = true;
    }
    for (--l; r > 0; l /= 2, r /= 2) {
      if (cl) calc(l);
      if (cr && (!cl || l != r)) calc(r);
    }
  }
  inline void upd(T &val, int v) { val = max(val, segAdd[v] + segMax[v]); }
  T query(int l, int r) {
    T res = -1120000000;
    for (l += n, r += n; l < r; l /= 2, r /= 2) {
      if (l & 1) upd(res, l++);
      if (r & 1) upd(res, --r);
    }
    return res;
  }
  SegTree(int n_) {
    n = 1;
    while (n < n_) n *= 2;
    segMax.resize(2 * n, 0);
    segAdd.resize(2 * n, 0);
  }
  SegTree(const vector<T> &v) {
    n = 1;
    while (n < (int)v.size()) n *= 2;
    segMax.resize(2 * n);
    segAdd.resize(2 * n, 0);
    for (int i = (int)(0); i < (int)(v.size()); i++) segMax[i + n] = v[i];
    for (int i = n - 1; i > 0; i--) calc(i);
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(n), r(n);
  for (int i = (int)(0); i < (int)(n); i++) cin >> l[i] >> r[i];
  vector<vector<int>> pos(m + 2);
  for (int i = (int)(0); i < (int)(n); i++) pos[l[i]].push_back(r[i]);
  vector<int> iv(m + 2);
  for (int i = (int)(0); i < (int)(m + 2); i++) iv[i] = -(m + 1 - i);
  SegTree<int> st(iv);
  int ans = n - m;
  for (int i = (int)(0); i < (int)(m + 2); i++) {
    for (auto x : pos[i]) st.add(i + 1, x + 1, 1);
    ans = max(ans, st.query(i + 1, m + 2) - i);
  }
  cout << ans << endl;
  return 0;
}
