#include <bits/stdc++.h>
using namespace std;
inline int gi() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - 48;
    ch = getchar();
  }
  return x * f;
}
template <typename T>
inline void Max(T &a, T b) {
  if (a < b) a = b;
}
template <typename T>
inline void Min(T &a, T b) {
  if (a > b) a = b;
}
const int N = 4e5 + 7;
int n, m, ans;
int adv[N << 2], mx[N << 2];
void st1(int o, int v) {
  adv[o] += v;
  mx[o] += v;
}
void pushdown(int o) {
  if (!adv[o]) return;
  st1(o << 1, adv[o]);
  st1((o << 1 | 1), adv[o]);
  adv[o] = 0;
}
void pushup(int o) { mx[o] = max(mx[o << 1], mx[(o << 1 | 1)]); }
void build(int l, int r, int o) {
  if (l == r) {
    mx[o] = l;
    return;
  }
  int mid = l + r >> 1;
  build(l, mid, o << 1);
  build(mid + 1, r, (o << 1 | 1));
  pushup(o);
}
void modify(int L, int R, int l, int r, int o, int v) {
  if (L > R) return;
  if (L <= l && r <= R) {
    st1(o, v);
    return;
  }
  pushdown(o);
  int mid = l + r >> 1;
  if (L <= mid) modify(L, R, l, mid, o << 1, v);
  if (R > mid) modify(L, R, mid + 1, r, (o << 1 | 1), v);
  pushup(o);
}
int query(int L, int R, int l, int r, int o) {
  if (L > R) return 0;
  if (L <= l && r <= R) return mx[o];
  pushdown(o);
  int mid = l + r >> 1;
  if (R <= mid) return query(L, R, l, mid, o << 1);
  if (L > mid) return query(L, R, mid + 1, r, (o << 1 | 1));
  return max(query(L, R, l, mid, o << 1),
             query(L, R, mid + 1, r, (o << 1 | 1)));
}
vector<int> h[N];
int main() {
  n = gi(), m = gi();
  for (int i = 1; i <= n; ++i) {
    int l = gi(), r = gi();
    h[l].push_back(r);
  }
  build(0, m + 1, 1);
  for (int l = 0; l <= m + 1; ++l) {
    for (auto r : h[l]) {
      modify(0, r, 0, m + 1, 1, 1);
    }
    Max(ans, query(l + 1, m + 1, 0, m + 1, 1) - l - m - 1);
  }
  printf("%d\n", ans);
  return 0;
}
