#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int inf = 0x3f3f3f3f;
int n, m, ans = 0;
vector<int> a[N];
struct SegmentTree {
  int l, r, mx, tag;
} t[N * 4];
void up(int x) { t[x].mx = max(t[(x << 1)].mx, t[(x << 1 | 1)].mx); }
void build(int x = 1, int l = 0, int r = m) {
  t[x].l = l, t[x].r = r;
  int mid = (l + r >> 1);
  if (l == r) {
    t[x].mx = -m + r - 1;
    return;
  }
  build((x << 1), l, mid), build((x << 1 | 1), mid + 1, r), up(x);
}
void pushdown(int x) {
  if (!t[x].tag) return;
  t[(x << 1)].tag += t[x].tag, t[(x << 1)].mx += t[x].tag;
  t[(x << 1 | 1)].tag += t[x].tag, t[(x << 1 | 1)].mx += t[x].tag;
  t[x].tag = 0;
}
void update(int l, int r, int val, int x = 1) {
  if (l <= t[x].l && t[x].r <= r) {
    t[x].tag += val, t[x].mx += val;
    return;
  }
  int mid = (t[x].l + t[x].r >> 1);
  pushdown(x);
  if (l <= mid) update(l, r, val, (x << 1));
  if (mid < r) update(l, r, val, (x << 1 | 1));
  up(x);
}
int query(int l, int r, int x = 1) {
  if (l <= t[x].l && t[x].r <= r) return t[x].mx;
  if (r < t[x].l || t[x].r < l) return -inf;
  pushdown(x);
  return max(query(l, r, (x << 1)), query(l, r, (x << 1 | 1)));
}
int main() {
  ios::sync_with_stdio(false);
  int x, y;
  cin >> n >> m, ans = max(ans, n - m);
  for (int i = 1; i <= n; i++) cin >> x >> y, a[x].push_back(y);
  build();
  for (int i = 0; i <= m - 1; i++) {
    for (int j = 0; j < a[i].size(); j++) update(1, a[i][j], 1);
    ans = max(ans, query(i + 2, m + 1) - i);
  }
  cout << ans << endl;
  return 0;
}
