#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> s[200005];
int t[200005 << 2], f[200005 << 2];
void build(int x, int l, int r) {
  if (l == r) {
    t[x] = l;
    return;
  }
  int mid = (l + r) >> 1;
  build(x << 1, l, mid);
  build(x << 1 | 1, mid + 1, r);
  t[x] = max(t[x << 1], t[x << 1 | 1]);
}
void add(int x, int l, int r, int ll, int rr) {
  if (l >= ll && r <= rr) {
    t[x]++;
    f[x]++;
    return;
  }
  int mid = (l + r) >> 1;
  if (ll <= mid) add(x << 1, l, mid, ll, rr);
  if (rr > mid) add(x << 1 | 1, mid + 1, r, ll, rr);
  t[x] = f[x] + max(t[x << 1], t[x << 1 | 1]);
}
int qur(int x, int l, int r, int ll, int rr) {
  if (l >= ll && r <= rr) return t[x];
  int mid = (l + r) >> 1;
  if (ll > mid) return f[x] + qur(x << 1 | 1, mid + 1, r, ll, rr);
  if (rr <= mid) return f[x] + qur(x << 1, l, mid, ll, rr);
  return f[x] +
         max(qur(x << 1, l, mid, ll, rr), qur(x << 1 | 1, mid + 1, r, ll, rr));
}
int main() {
  scanf("%d%d", &n, &m);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int t1, t2;
    scanf("%d%d", &t1, &t2);
    s[t1].push_back(t2);
  }
  build(1, 0, m + 1);
  for (int i = 0; i <= m; i++) {
    for (auto j : s[i]) add(1, 0, m + 1, 0, j);
    ans = max(ans, qur(1, 0, m + 1, i + 1, m + 1) - i - m - 1);
  }
  printf("%d\n", ans);
  return 0;
}
