#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
int tree[4 * MAXN], lazy[4 * MAXN], A[MAXN];
vector<int> pos[MAXN];
void updatenode(int now, int L, int R, int val) {
  tree[now] += val;
  lazy[now] += val;
}
void pushdown(int now, int L, int R) {
  int mid = (L + R) / 2;
  updatenode(2 * now, L, mid, lazy[now]);
  updatenode(2 * now + 1, mid + 1, R, lazy[now]);
  lazy[now] = 0;
}
void build(int now, int L, int R) {
  if (L == R) {
    tree[now] = A[L];
    return;
  }
  int mid = (L + R) / 2;
  build(2 * now, L, mid);
  build(2 * now + 1, mid + 1, R);
  tree[now] = max(tree[2 * now], tree[2 * now + 1]);
}
int query(int now, int L, int R, int x, int y) {
  if (L >= x && R <= y) return tree[now];
  if (L > y || R < x) return INT_MIN;
  pushdown(now, L, R);
  int mid = (L + R) / 2;
  return max(query(2 * now, L, mid, x, y),
             query(2 * now + 1, mid + 1, R, x, y));
}
void update(int now, int L, int R, int x, int y, int val) {
  if (L >= x && R <= y) {
    updatenode(now, L, R, val);
    return;
  }
  if (L > y || R < x) return;
  int mid = (L + R) / 2;
  pushdown(now, L, R);
  update(2 * now, L, mid, x, y, val);
  update(2 * now + 1, mid + 1, R, x, y, val);
  tree[now] = max(tree[2 * now], tree[2 * now + 1]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m, l, r;
  cin >> n >> m;
  int ans = n - m;
  for (int i = 1; i <= n; i++) {
    cin >> l >> r;
    pos[l].push_back(r);
  }
  for (int i = 1; i <= m; i++) A[i] = -m + i - 1;
  build(1, 1, m);
  for (int i = 0; i <= m; i++) {
    if (i) update(1, 1, m, 1, m, -1);
    for (auto isi : pos[i]) update(1, 1, m, 1, isi, 1);
    ans = max(ans, query(1, 1, m, i + 2, m));
  }
  cout << max(ans, 0) << endl;
  return 0;
}
