#include <bits/stdc++.h>
using namespace std;
int n, m;
const int N = 500000;
int lb[N], rb[N], lc[N], rc[N], mx[N], tag[N], tot;
int ans;
vector<int> V[N];
void upd(int t) { mx[t] = max(mx[lc[t]], mx[rc[t]]); }
void psdw(int t) {
  tag[lc[t]] += tag[t];
  mx[lc[t]] += tag[t];
  tag[rc[t]] += tag[t];
  mx[rc[t]] += tag[t];
  tag[t] = 0;
}
int build(int l, int r) {
  int t = ++tot;
  lb[t] = l;
  rb[t] = r;
  if (l != r) {
    lc[t] = build(l, (l + r) / 2);
    rc[t] = build((l + r) / 2 + 1, r);
    upd(t);
  } else {
    mx[t] = -(m + 1 - l);
  }
  return t;
}
void modify(int t, int l, int r, int v) {
  if (l <= lb[t] && rb[t] <= r) {
    mx[t] += v;
    tag[t] += v;
    return;
  }
  psdw(t);
  if (l <= rb[lc[t]]) modify(lc[t], l, r, v);
  if (r >= lb[rc[t]]) modify(rc[t], l, r, v);
  upd(t);
}
int query(int t, int l, int r) {
  if (l <= lb[t] && rb[t] <= r) return mx[t];
  psdw(t);
  int v = 0;
  if (l <= rb[lc[t]]) v = max(v, query(lc[t], l, r));
  if (r >= lb[rc[t]]) v = max(v, query(rc[t], l, r));
  return v;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    V[l].push_back(r);
  }
  build(0, m + 1);
  for (int i = 0; i <= m; ++i) {
    for (int j = 0; j < V[i].size(); ++j) modify(1, 0, V[i][j], 1);
    ans = max(ans, query(1, i + 1, m + 1) - i);
  }
  printf("%d\n", ans);
}
