#include <bits/stdc++.h>
using namespace std;
const int N_ = int(1.1e5);
using lint = long long;
int N, M;
class SegmentTree {
  lint tree[530000], lazy[530000];
  void init(int s, int e, int p, int *x) {
    if (s == e) {
      tree[p] = x[s];
      return;
    }
    int m = (s + e) / 2;
    init(s, m, 2 * p, x);
    init(m + 1, e, 2 * p + 1, x);
    tree[p] = max(tree[2 * p], tree[2 * p + 1]);
  }
  void lazydown(int p) {
    tree[2 * p] += lazy[p];
    tree[2 * p + 1] += lazy[p];
    lazy[2 * p] += lazy[p];
    lazy[2 * p + 1] += lazy[p];
    lazy[p] = 0;
  }
  void add(int s, int e, int ps, int pe, int p, lint v) {
    if (e < ps || pe < s) return;
    if (s <= ps && pe <= e) {
      tree[p] += v;
      lazy[p] += v;
      return;
    }
    lazydown(p);
    int pm = (ps + pe) / 2;
    add(s, e, ps, pm, 2 * p, v);
    add(s, e, pm + 1, pe, 2 * p + 1, v);
    tree[p] = max(tree[2 * p], tree[2 * p + 1]);
  }
  lint query(int s, int e, int ps, int pe, int p) {
    if (e < ps || pe < s) return -1e18;
    if (s <= ps && pe <= e) return tree[p];
    lazydown(p);
    int pm = (ps + pe) / 2;
    return max(query(s, e, ps, pm, 2 * p), query(s, e, pm + 1, pe, 2 * p + 1));
  }

 public:
  void add(int s, int e, lint v) { add(s, e, 0, M + 1, 1, v); }
  lint query(int s, int e) { return query(s, e, 0, M + 1, 1); }
} seg;
const int M_ = int(2.1e5);
vector<int> points[M_];
int main() {
  scanf("%d%d", &N, &M);
  int maxl = -1, minr = 1e9;
  for (int i = 1; i <= N; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    maxl = max(l, maxl);
    minr = min(r, minr);
    points[l].push_back(r);
  }
  int ans = 0;
  if (maxl >= minr || minr - maxl == 1) {
    ans = max(ans, N - M);
  }
  for (int i = 1; i <= M; i++) {
    seg.add(i, M + 1, +1);
  }
  for (int a = 0; a < M; a++) {
    for (int b : points[a]) {
      seg.add(1, b, +1);
    }
    ans = max(ans, (int)seg.query(a + 1, M) - (a + M + 1));
  }
  printf("%d\n", ans);
  return 0;
}
