#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const int inf = 0x3f3f3f3f;
int n, m, l, r;
vector<int> v[maxn];
struct node {
  int l, r, lz, mmax;
  inline void update(int v) {
    lz += v;
    mmax += v;
  }
} seg[maxn << 2];
inline void pushup(int x) {
  seg[x].mmax = max(seg[x << 1].mmax, seg[x << 1 | 1].mmax);
}
inline void pushdown(int x) {
  int lz = seg[x].lz;
  if (lz) {
    seg[x << 1].update(lz);
    seg[x << 1 | 1].update(lz);
    seg[x].lz = 0;
  }
}
void build(int x, int l, int r) {
  seg[x].l = l, seg[x].r = r;
  if (l == r) {
    seg[x].mmax = l;
    return;
  }
  int m = (l + r) >> 1;
  build(x << 1, l, m);
  build(x << 1 | 1, m + 1, r);
  pushup(x);
}
void update(int x, int l, int r, int v) {
  int L = seg[x].l, R = seg[x].r;
  if (l <= L && r >= R) {
    seg[x].update(v);
    return;
  }
  pushdown(x);
  int m = (L + R) >> 1;
  if (m >= l) update(x << 1, l, r, v);
  if (m < r) update(x << 1 | 1, l, r, v);
  pushup(x);
}
int query(int x, int l, int r) {
  int L = seg[x].l, R = seg[x].r;
  if (l <= L && r >= R) return seg[x].mmax;
  pushdown(x);
  int ret = -inf, m = (L + R) >> 1;
  if (m >= l) ret = max(ret, query(x << 1, l, r));
  if (m < r) ret = max(ret, query(x << 1 | 1, l, r));
  pushup(x);
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &l, &r);
    v[l].push_back(r);
  }
  build(1, 0, m + 1);
  int res = -(0x3f3f3f3f);
  for (int i = 0; i <= m; ++i) {
    for (auto x : v[i]) update(1, 0, x, 1);
    res = max(res, query(1, i + 1, m + 1) - m - i - 1);
  }
  printf("%d\n", res);
  return 0;
}
