#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T& n) {
  char ch;
  int sign = 1;
  while (!isdigit(ch = getchar()))
    if (ch == '-') sign = -1;
  n = ch - '0';
  while (isdigit(ch = getchar())) n = n * 10 + ch - '0';
  n *= sign;
}
const int INF = 1e9 + 7;
const int N = 211111;
int n, m, tt(1);
pair<int, int> seg[N];
struct segtr {
  int lc, rc, mx, tag;
} tr[N * 8];
void pushdown(int first) {
  if (!tr[first].tag) return;
  tr[first].mx += tr[first].tag;
  if (tr[first].lc) tr[tr[first].lc].tag += tr[first].tag;
  if (tr[first].rc) tr[tr[first].rc].tag += tr[first].tag;
  tr[first].tag = 0;
}
void update(int first) {
  pushdown(first), pushdown(tr[first].lc), pushdown(tr[first].rc);
  tr[first].mx = max(tr[tr[first].lc].mx, tr[tr[first].rc].mx);
}
void build(int first, int l, int r) {
  if (l == r)
    tr[first].mx = -(m - l + 1);
  else {
    int mid = (l + r) >> 1;
    tr[first].lc = ++tt, build(tt, l, mid);
    tr[first].rc = ++tt, build(tt, mid + 1, r);
    update(first);
  }
}
void modify(int first, int l, int r, int nl, int nr, int k) {
  pushdown(first);
  if (nl <= l && r <= nr) {
    tr[first].tag += k;
    return;
  }
  int mid = (l + r) >> 1;
  if (nl <= mid) modify(tr[first].lc, l, mid, nl, nr, k);
  if (mid < nr) modify(tr[first].rc, mid + 1, r, nl, nr, k);
  update(first);
}
int query(int first, int l, int r, int nl, int nr) {
  pushdown(first);
  if (nl <= l && r <= nr) return tr[first].mx;
  int mid = (l + r) >> 1, ans = -INF;
  if (nl <= mid) ans = max(ans, query(tr[first].lc, l, mid, nl, nr));
  if (mid < nr) ans = max(ans, query(tr[first].rc, mid + 1, r, nl, nr));
  return ans;
}
int main() {
  read(n), read(m);
  for (int i = (1); i <= (n); ++i) read(seg[i].first), read(seg[i].second);
  sort(seg + 1, seg + n + 1);
  int pos = 1, ans = 0;
  build(1, 1, m);
  for (int i = (1); i <= (m); ++i) {
    for (; pos <= n && seg[pos].first <= i; ++pos)
      modify(1, 1, m, i + 1, seg[pos].second, 1);
    ans = max(ans, query(1, 1, m, i + 1, m) - i);
  }
  printf("%d\n", max(ans, n - m));
  return 0;
}
