#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7, inf = 1e9 + 7;
int n, m, ans = 0;
struct Opt {
  int l, r;
} p[maxn];
struct Node {
  int lazy, maxx;
} node[maxn * 4];
bool cmp(const Opt &A, const Opt &B) { return A.l < B.l; }
void pushdown(int num, int nl, int nr) {
  if (!node[num].lazy) return;
  int lz = node[num].lazy;
  node[num].lazy = 0;
  node[(num * 2)].maxx += lz;
  node[(num * 2)].lazy += lz;
  node[(num * 2 + 1)].maxx += lz;
  node[(num * 2 + 1)].lazy += lz;
}
void update(int num) {
  node[num].maxx = max(node[(num * 2)].maxx, node[(num * 2 + 1)].maxx);
}
void build(int num, int nl, int nr) {
  node[num].lazy = 0;
  if (nl == nr) {
    node[num].maxx = nr - m - 1;
    return;
  }
  int mid = (nl + nr) / 2;
  build((num * 2), nl, mid);
  build((num * 2 + 1), mid + 1, nr);
  update(num);
}
void add(int num, int nl, int nr, int ql, int qr, int ad) {
  if (ql <= nl && nr <= qr) {
    node[num].maxx += ad;
    node[num].lazy += ad;
    return;
  }
  if (nr < ql || qr < nl) return;
  int mid = (nl + nr) / 2;
  pushdown(num, nl, nr);
  add((num * 2), nl, mid, ql, qr, ad);
  add((num * 2 + 1), mid + 1, nr, ql, qr, ad);
  update(num);
  return;
}
int quary(int num, int nl, int nr, int ql, int qr) {
  if (ql <= nl && nr <= qr) return node[num].maxx;
  if (nr < ql || qr < nl) return -inf;
  int mid = (nl + nr) / 2;
  pushdown(num, nl, nr);
  return max(quary((num * 2), nl, mid, ql, qr),
             quary((num * 2 + 1), mid + 1, nr, ql, qr));
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d%d", &p[i].l, &p[i].r);
  sort(p + 1, p + n + 1, cmp);
  p[n + 1].l = inf;
  build(1, 1, m);
  for (int i = 1; i <= n; i++) {
    add(1, 1, m, 1, min(p[i].r, m), 1);
    if (p[i].l != p[i + 1].l && p[i].l <= m - 2)
      ans = max(ans, quary(1, 1, m, p[i].l + 2, m) - p[i].l);
  }
  ans = max(ans, n - m);
  printf("%d\n", ans);
  return 0;
}
