#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &res) {
  res = 0;
  T f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T>
void out(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}
int N, M;
struct node {
  int l, r;
} p[400005];
struct seg_tree {
  int l, r;
  int lz, mx;
} tr[400005 * 4];
void update(int u) { tr[u].mx = max(tr[u << 1 | 1].mx, tr[u << 1].mx); }
void addlz(int u, int v) {
  tr[u].mx += v;
  tr[u].lz += v;
}
void pushdown(int u) {
  if (tr[u].lz) {
    addlz(u << 1, tr[u].lz);
    addlz(u << 1 | 1, tr[u].lz);
    tr[u].lz = 0;
  }
}
void build(int u, int l, int r) {
  tr[u].l = l;
  tr[u].r = r;
  if (l == r) {
    tr[u].mx = r;
    return;
  }
  int mid = (l + r) >> 1;
  build(u << 1, l, mid);
  build(u << 1 | 1, mid + 1, r);
  update(u);
}
void Change(int u, int l, int r, int v) {
  if (tr[u].l == l && tr[u].r == r) {
    addlz(u, v);
    return;
  }
  pushdown(u);
  int mid = (tr[u].l + tr[u].r) >> 1;
  if (r <= mid)
    Change(u << 1, l, r, v);
  else if (l > mid)
    Change(u << 1 | 1, l, r, v);
  else {
    Change(u << 1, l, mid, v);
    Change(u << 1 | 1, mid + 1, r, v);
  }
  update(u);
}
int Query(int u, int l, int r) {
  if (tr[u].l == l && tr[u].r == r) {
    return tr[u].mx;
  }
  pushdown(u);
  int mid = (tr[u].l + tr[u].r) >> 1;
  if (r <= mid)
    return Query(u << 1, l, r);
  else if (l > mid)
    return Query(u << 1 | 1, l, r);
  else
    return max(Query(u << 1, l, mid), Query(u << 1 | 1, mid + 1, r));
}
void Solve() {
  read(N);
  read(M);
  for (int i = 1; i <= N; ++i) {
    read(p[i].l);
    read(p[i].r);
  }
  build(1, 1, M + 1);
  sort(p + 1, p + N + 1,
       [](node a, node b) { return a.l < b.l || (a.l == b.l && a.r > b.r); });
  int ans = 0;
  for (int i = 1; i <= N; ++i) {
    Change(1, p[i].l + 1, p[i].r, 1);
    int t = Query(1, p[i].l + 1, M + 1) - M - p[i].l - 1;
    ans = max(ans, t);
  }
  out(ans);
  putchar('\n');
}
int main() { Solve(); }
