#include <bits/stdc++.h>
using namespace std;
const int ST_MAX = (1 << 17);
template <class T, class U>
class ST {
  T x[ST_MAX * 2];
  U lazy[ST_MAX * 2];
  T eval(T a, T b) { return max(a, b); }
  U syn(U a, U b) { return a + b; }
  void work(int a) {
    if (a < ST_MAX) {
      lazy[a * 2] = syn(lazy[a], lazy[a * 2]);
      lazy[a * 2 + 1] = syn(lazy[a], lazy[a * 2 + 1]);
    }
    x[a] += lazy[a];
    lazy[a] = 0;
  }
  void ren(int a, int b, int e, int s, int f, U z) {
    if (e <= s || f <= b) {
      work(a);
      return;
    }
    if (s <= b && e <= f) {
      lazy[a] = syn(lazy[a], z);
      work(a);
      return;
    }
    work(a);
    ren(2 * a, b, (b + e) / 2, s, f, z);
    ren(2 * a + 1, (b + e) / 2, e, s, f, z);
    x[a] = eval(x[a * 2], x[a * 2 + 1]);
  }
  T read_ST(int a, int b, int e, int s, int f) {
    work(a);
    if (e <= s || f <= b) return -1;
    if (s <= b && e <= f) return x[a];
    return eval(read_ST(2 * a, b, (b + e) / 2, s, f),
                read_ST(2 * a + 1, (b + e) / 2, e, s, f));
  }

 public:
  ST() {
    fill(lazy, lazy + ST_MAX * 2, 0);
    for (int i = (int)(0); i < (int)(ST_MAX); ++i) x[ST_MAX + i] = i;
    for (int i = (int)(ST_MAX - 1); i > (int)(0); --i)
      x[i] = max(x[i * 2], x[i * 2 + 1]);
  }
  void put(int t) { ren(1, 0, ST_MAX, 0, t, 1); }
  T get(int s, int f) { return read_ST(1, 0, ST_MAX, s, f); }
};
int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> V(N);
  for (int i = (int)(0); i < (int)(N); ++i) cin >> V[i].first >> V[i].second;
  sort(V.begin(), V.end());
  ST<int, int> solve;
  int ans = max(0, N - M);
  int p = 0;
  for (int i = (int)(0); i < (int)(M); ++i) {
    while (p < N && V[p].first == i) {
      solve.put(V[p].second);
      ++p;
    }
    ans = max(ans, solve.get(i + 1, M + 1) - M - i);
  }
  cout << ans << endl;
  return 0;
}
