#include <bits/stdc++.h>
using namespace std;
const int max_n = 200005;
int n, m;
vector<int> v[max_n];
struct Seg {
  int d[1 << 20], lazy[1 << 20];
  void pushup(int rt) { d[rt] = min(d[rt << 1], d[rt << 1 | 1]); }
  void pushdown(int rt, int ln = 1, int rn = 1) {
    if (lazy[rt]) {
      d[rt << 1] += lazy[rt] * ln;
      d[rt << 1 | 1] += lazy[rt] * rn;
      lazy[rt << 1] += lazy[rt];
      lazy[rt << 1 | 1] += lazy[rt];
      lazy[rt] = 0;
    }
  }
  void init(int l, int r, int rt) {
    d[rt] = INT_MAX, lazy[rt] = 0;
    if (l == r) return;
    int mid = l + r >> 1;
    init(l, mid, rt << 1), init(mid + 1, r, rt << 1 | 1);
  }
  void ins(int x, int c, int l, int r, int rt) {
    if (l == r) {
      d[rt] = c;
      return;
    }
    int mid = l + r >> 1;
    pushdown(rt);
    if (x <= mid)
      ins(x, c, l, mid, rt << 1);
    else
      ins(x, c, mid + 1, r, rt << 1 | 1);
    pushup(rt);
  }
  void update(int L, int R, int C, int l, int r, int rt) {
    if (L > R) return;
    if (l >= L && r <= R) {
      d[rt] += C;
      lazy[rt] += C;
      return;
    }
    int mid = l + r >> 1;
    pushdown(rt);
    if (mid >= L) update(L, R, C, l, mid, rt << 1);
    if (mid < R) update(L, R, C, mid + 1, r, rt << 1 | 1);
    pushup(rt);
  }
  int query(int L, int R, int l, int r, int rt) {
    if (L > R || L > r || R < l) return INT_MAX;
    if (l >= L && r <= R) return d[rt];
    int mid = l + r >> 1;
    pushdown(rt);
    return min(query(L, R, l, mid, rt << 1),
               query(L, R, mid + 1, r, rt << 1 | 1));
  }
} Tree;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    v[l].push_back(r);
  }
  Tree.init(1, m + 1, 1);
  for (int i = 1; i <= m + 1; i++) Tree.ins(i, -i, 1, m + 1, 1);
  int ans = 0;
  for (int i = 0; i <= m; i++) {
    for (auto x : v[i]) Tree.update(1, x, -1, 1, m + 1, 1);
    int res = -Tree.query(1, m + 1, 1, m + 1, 1);
    ans = max(ans, res - i - m - 1);
  }
  printf("%d\n", ans);
  return 0;
}
