#include <bits/stdc++.h>
using namespace std;
const int N = 202020;
inline char get(void) {
  static char buf[100000], *S = buf, *T = buf;
  if (S == T) {
    T = (S = buf) + fread(buf, 1, 100000, stdin);
    if (S == T) return EOF;
  }
  return *S++;
}
template <typename T>
inline void read(T &x) {
  static char c;
  x = 0;
  int sgn = 0;
  for (c = get(); c < '0' || c > '9'; c = get())
    if (c == '-') sgn = 1;
  for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
  if (sgn) x = -x;
}
pair<int, int> p[N];
int n, m, ans;
int S[N << 2], T[N << 2];
inline void PushUp(int p) {
  S[p] = S[p << 1] + S[p << 1 | 1];
  T[p] = max(T[p << 1], S[p << 1] + T[p << 1 | 1]);
}
void Build(int p, int l, int r) {
  if (l == r) {
    S[p] = l == 0 ? 0 : -1;
    T[p] = 0;
    return;
  }
  int mid = l + r >> 1;
  Build(p << 1, l, mid);
  Build(p << 1 | 1, mid + 1, r);
  PushUp(p);
}
void Add(int pos, int w) {
  int p = 1, l = 0, r = m;
  while (l < r) {
    int mid = l + r >> 1;
    if (pos <= mid) {
      p = p << 1;
      r = mid;
    } else {
      p = p << 1 | 1;
      l = mid + 1;
    }
  }
  T[p] = max(0, S[p] += w);
  while (p >>= 1) PushUp(p);
}
int main(void) {
  read(n);
  read(m);
  for (int i = 1; i <= n; i++) {
    read(p[i].first);
    read(p[i].second);
    p[i].second = m + 1 - p[i].second;
  }
  sort(p + 1, p + n + 1);
  Build(1, 0, m);
  for (int i = 1; i <= n; i++) {
    Add(p[i].second, 1);
    ans = max(ans, p[i].first - T[1]);
  }
  printf("%d\n", max(ans, n - m));
  return 0;
}
