#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int n, m, delta[maxn << 2], t[maxn << 2], ans = -1e7;
struct peo {
  int l, r;
} a[maxn];
bool cmp(peo a, peo b) { return a.r < b.r; }
void pushdown(int n) {
  int x = delta[n];
  delta[(n << 1)] += x;
  delta[(n << 1 | 1)] += x;
  t[(n << 1)] += x;
  t[(n << 1 | 1)] += x;
  delta[n] = 0;
}
void add(int n, int l, int r, int ll, int rr, int v) {
  if (ll <= l && r <= rr) {
    t[n] += v;
    delta[n] += v;
    return;
  }
  int mid = (l + r) >> 1;
  if (delta[n]) pushdown(n);
  if (ll <= mid) add((n << 1), l, mid, ll, rr, v);
  if (rr > mid) add((n << 1 | 1), mid + 1, r, ll, rr, v);
  t[n] = max(t[(n << 1)], t[(n << 1 | 1)]);
}
int main() {
  scanf("%d%d", &n, &m);
  for (register int i = 1; i <= n; ++i) scanf("%d%d", &a[i].l, &a[i].r);
  sort(a + 1, a + 1 + n, cmp);
  for (register int i = 1; i <= n; ++i) add(1, 1, m, max(1, a[i].l), m, 1);
  for (register int i = 1; i <= m; ++i) add(1, 1, m, i, i, -i);
  int p = 1;
  ans = max(ans, t[1] - m - 1);
  for (register int r = 1; r <= m; ++r) {
    while (a[p].r < r && p <= n) {
      add(1, 1, m, max(1, a[p].l), m, -1);
      p++;
    }
    ans = max(ans, t[1] + r - m - 1);
  }
  ans = max(ans, n - m);
  printf("%d", ans);
  return 0;
}
