#include <bits/stdc++.h>
using namespace std;
namespace std {
template <class S, class T>
struct hash<pair<S, T>> {
  size_t operator()(const pair<S, T> &p) const {
    return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
  }
};
template <class T>
struct hash<vector<T>> {
  size_t operator()(const vector<T> &v) const {
    size_t h = 0;
    for (auto i : v) h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
    return h;
  }
};
}  // namespace std
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  for (int i = 0; i < (int)v.size(); i++)
    os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{ ";
  for (const auto &i : v) os << i << ", ";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
  os << "{";
  for (const auto &i : v) os << " " << i.first << ": " << i.second << ",";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
  cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
  dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
const int MX = 262411;
int N, dat[2 * MX], lazy[2 * MX];
int query(int a, int b, int k, int l, int r) {
  if (b <= l || a >= r) return -inf;
  if (a <= l && r <= b) return dat[k] + lazy[k];
  return lazy[k] + max(query(a, b, 2 * k + 1, l, (l + r) / 2),
                       query(a, b, 2 * k + 2, (l + r) / 2, r));
}
void add(int a, int b, int x, int k, int l, int r) {
  if (b <= l || a >= r) return;
  if (a <= l && r <= b) {
    lazy[k] += x;
    return;
  }
  lazy[k * 2 + 1] += lazy[k];
  lazy[k * 2 + 2] += lazy[k];
  lazy[k] = 0;
  add(a, b, x, 2 * k + 1, l, (l + r) / 2);
  add(a, b, x, 2 * k + 2, (l + r) / 2, r);
  dat[k] =
      max(dat[k * 2 + 1] + lazy[k * 2 + 1], dat[k * 2 + 2] + lazy[k * 2 + 2]);
}
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  m++;
  assert(n > 3);
  for (N = 1; N < m; N *= 2)
    ;
  for (int i = 0; i < (int)2 * N; i++) dat[i] = -inf;
  for (int i = 1; i < m; i++) dat[i + N - 1] = -i;
  for (int i = N - 2; i >= 0; i--) dat[i] = max(dat[i * 2 + 1], dat[i * 2 + 2]);
  vector<pair<int, int>> v;
  for (int i = 0; i < (int)n; i++) {
    int a, b;
    cin >> a >> b;
    v.emplace_back(b, a);
  }
  sort((v).begin(), (v).end(), greater<pair<int, int>>());
  int ans = max(0, n - m + 1);
  for (pair<int, int> i : v) {
    add(i.second, m, 1, 0, 0, N);
    ans = max(ans, query(0, m, 0, 0, N) - m + i.first);
  }
  cout << ans << endl;
  return 0;
}
