#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
vector<int> G[maxn];
int a[maxn];
int n, m;
struct SegTree {
  int mx[maxn << 2], add[maxn << 2];
  void pushUp(int rt) { mx[rt] = max(mx[(rt << 1)], mx[(rt << 1 | 1)]); }
  void build(int l, int r, int rt) {
    if (l == r) {
      mx[rt] = r;
      return;
    }
    int mid = (l + r >> 1);
    build(l, mid, (rt << 1));
    build(mid + 1, r, (rt << 1 | 1));
    pushUp(rt);
  }
  void pushDown(int rt) {
    if (add[rt]) {
      add[(rt << 1)] += add[rt], add[(rt << 1 | 1)] += add[rt];
      mx[(rt << 1)] += add[rt], mx[(rt << 1 | 1)] += add[rt];
      add[rt] = 0;
    }
  }
  void update(int l, int r, int rt, int L, int R, int val) {
    if (l >= L && r <= R) {
      mx[rt] += val, add[rt] += val;
      return;
    }
    int mid = (l + r >> 1);
    pushDown(rt);
    if (L <= mid) update(l, mid, (rt << 1), L, R, val);
    if (R > mid) update(mid + 1, r, (rt << 1 | 1), L, R, val);
    pushUp(rt);
  }
  int query(int l, int r, int rt, int L, int R) {
    if (l >= L && r <= R) return mx[rt];
    int mid = (l + r >> 1), ret = -inf;
    pushDown(rt);
    if (L <= mid) ret = max(ret, query(l, mid, (rt << 1), L, R));
    if (R > mid) ret = max(ret, query(mid + 1, r, (rt << 1 | 1), L, R));
    return ret;
  }
} tr;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    int l, r;
    cin >> l >> r;
    G[l].push_back(r);
  }
  ++m;
  tr.build(1, m, 1);
  int ret = 0;
  for (int i = 0; i < m; ++i) {
    for (auto &r : G[i]) {
      tr.update(1, m, 1, 1, r, 1);
    }
    int tmp = tr.query(1, m, 1, i + 1, m);
    ret = max(ret, tmp - (m + i));
  }
  cout << ret << endl;
  return 0;
}
