#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int, int> > seg;
int st[800010], lazy[800010];
int ans = 0;
void build(int first, int second, int i) {
  if (first > second) return;
  if (first == second) {
    st[i] = second - m - 1;
    return;
  }
  int m = (first + second) >> 1;
  build(first, m, i << 1);
  build(m + 1, second, 1 + (i << 1));
  st[i] = max(st[i << 1], st[1 + (i << 1)]);
}
void propagate(int first, int second, int i) {
  if (lazy[i]) {
    if (first != second) {
      lazy[i << 1] += lazy[i];
      lazy[1 + (i << 1)] += lazy[i];
    }
    st[i] += lazy[i];
  }
  lazy[i] = 0;
}
void update(int first, int second, int ls, int rs, int i) {
  propagate(first, second, i);
  if (second < ls || rs < first) return;
  if (ls <= first && second <= rs) {
    lazy[i] += 1;
    propagate(first, second, i);
    return;
  }
  int m = (first + second) >> 1;
  update(first, m, ls, rs, i << 1);
  update(m + 1, second, ls, rs, 1 + (i << 1));
  st[i] = max(st[i << 1], st[1 + (i << 1)]);
}
int query(int first, int second, int ls, int rs, int i) {
  propagate(first, second, i);
  if (second < ls || rs < first) return 0;
  if (ls <= first && second <= rs) return st[i];
  int m = (first + second) >> 1;
  return max(query(first, m, ls, rs, i << 1),
             query(m + 1, second, ls, rs, 1 + (i << 1)));
}
int main() {
  scanf("%d%d", &n, &m);
  seg.resize(n);
  for (int i = 0; i < n; i++) scanf("%d%d", &seg[i].first, &seg[i].second);
  sort(seg.begin(), seg.end());
  build(1, m, 1);
  for (auto k : seg) {
    update(1, m, 1, k.second, 1);
    ans = max(ans, query(k.first + 1, m, 1, m, 1) - k.first);
  }
  ans = max(ans, n - m);
  printf("%d\n", ans);
  return 0;
}
