#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int n, m, ans;
int tag[maxn * 4], mx[maxn * 4];
pair<int, int> a[maxn];
void pushdown(int x) {
  if (tag[x]) {
    tag[x << 1] += tag[x];
    tag[x << 1 | 1] += tag[x];
    mx[x << 1] += tag[x];
    mx[x << 1 | 1] += tag[x];
    tag[x] = 0;
  }
}
int query(int l, int r, int x, int a, int b) {
  if (l > b || r < a) {
    return -1e9;
  }
  if (l >= a && r <= b) {
    return mx[x];
  }
  pushdown(x);
  int mid = (l + r) >> 1;
  return max(query(l, mid, x << 1, a, b), query(mid + 1, r, x << 1 | 1, a, b));
}
void update(int l, int r, int x, int a, int b, int d) {
  if (l > b || r < a) {
    return;
  }
  if (l >= a && r <= b) {
    mx[x] += d;
    tag[x] += d;
    return;
  }
  pushdown(x);
  int mid = (l + r) >> 1;
  update(l, mid, x << 1, a, b, d);
  update(mid + 1, r, x << 1 | 1, a, b, d);
  mx[x] = max(mx[x << 1], mx[x << 1 | 1]);
}
void build(int l, int r, int x) {
  if (l == r) {
    mx[x] = -n + r - 1;
    return;
  }
  int mid = (l + r) >> 1;
  build(l, mid, x << 1);
  build(mid + 1, r, x << 1 | 1);
  mx[x] = max(mx[x << 1], mx[x << 1 | 1]);
}
int main() {
  scanf("%d%d", &m, &n);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &a[i].first, &a[i].second);
  }
  sort(a + 1, a + m + 1);
  build(1, n + 1, 1);
  for (int i = 0, j = 1; i <= n; ++i) {
    while (j <= m && a[j].first <= i) {
      update(1, n + 1, 1, 1, a[j].second, 1);
      ++j;
    }
    ans = max(ans, query(1, n + 1, 1, i + 1, n) - i);
  }
  printf("%d\n", max(ans, m - n));
  return 0;
}
