#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, inf = 1e9;
int n, m;
pair<int, int> a[N];
int tree[4 * N], lazy[4 * N];
void dispatch(int id, int curL, int curR) {
  if (lazy[id]) {
    tree[id] += lazy[id];
    if (curL != curR) {
      lazy[2 * id] += lazy[id];
      lazy[2 * id + 1] += lazy[id];
    }
    lazy[id] = 0;
  }
}
void update(int id, int curL, int curR, int uL, int uR, int uVal) {
  dispatch(id, curL, curR);
  if (uL > uR || curR < uL || curL > uR) return;
  if (uL <= curL && curR <= uR) {
    lazy[id] += uVal;
    dispatch(id, curL, curR);
    return;
  }
  int mid = (curL + curR) / 2;
  update(2 * id, curL, mid, uL, uR, uVal);
  update(2 * id + 1, mid + 1, curR, uL, uR, uVal);
  tree[id] = max(tree[2 * id], tree[2 * id + 1]);
}
int query(int id, int curL, int curR, int qL, int qR) {
  dispatch(id, curL, curR);
  if (qL > qR || curL > qR || curR < qL) return -inf;
  if (qL <= curR && curR <= qR) return tree[id];
  int mid = (curL + curR) / 2;
  return max(query(2 * id, curL, mid, qL, qR),
             query(2 * id + 1, mid + 1, curR, qL, qR));
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].first, &a[i].second);
  for (int i = 1; i <= m; i++) update(1, 1, m, i, i, (i - 1));
  sort(a + 1, a + n + 1);
  int ans = n - m;
  for (int i = 1; i <= n; i++) {
    if (a[i].first + 1 == a[i].second)
      update(1, 1, m, 1, m, +1);
    else
      update(1, 1, m, a[i].second, m, +1);
    int tmp = query(1, 1, m, a[i].first + 1, m);
    ans = max(ans, tmp - m - a[i].first);
  }
  printf("%d\n", max(ans, 0));
  return 0;
}
