#include <bits/stdc++.h>
using namespace std;
template <typename T>
void get_int(T &x) {
  char t = getchar();
  bool neg = false;
  x = 0;
  for (; (t > '9' || t < '0') && t != '-'; t = getchar())
    ;
  if (t == '-') neg = true, t = getchar();
  for (; t <= '9' && t >= '0'; t = getchar()) x = x * 10 + t - '0';
  if (neg) x = -x;
}
template <typename T>
void print_int(T x) {
  if (x < 0) putchar('-'), x = -x;
  short a[20] = {}, sz = 0;
  while (x > 0) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
const int inf = 0x3f3f3f3f;
const long long Linf = 1ll << 61;
const double pi = acos(-1.0);
struct SGT {
  struct segment {
    int l, r, mx, delta;
  } seg[1050111];
  void pushdown(int t) {
    seg[(t << 1)].delta += seg[t].delta;
    seg[((t << 1) | 1)].delta += seg[t].delta;
    seg[(t << 1)].mx += seg[t].delta;
    seg[((t << 1) | 1)].mx += seg[t].delta;
    seg[t].delta = 0;
  }
  void build(int l, int r, int t = 1) {
    seg[t].l = l;
    seg[t].r = r;
    seg[t].mx = -inf;
    seg[t].delta = 0;
    if (l == r) return;
    int mid = (l + r) >> 1;
    build(l, mid, (t << 1));
    build(mid + 1, r, ((t << 1) | 1));
  }
  void add(int l, int r, int v, int t = 1) {
    if (l > r) return;
    if (seg[t].l == l && seg[t].r == r) {
      seg[t].delta += v;
      seg[t].mx += v;
      return;
    }
    pushdown(t);
    int mid = (seg[t].l + seg[t].r) >> 1;
    if (r <= mid)
      add(l, r, v, (t << 1));
    else if (l > mid)
      add(l, r, v, ((t << 1) | 1));
    else {
      add(l, mid, v, (t << 1));
      add(mid + 1, r, v, ((t << 1) | 1));
    }
    seg[t].mx = max(seg[(t << 1)].mx, seg[((t << 1) | 1)].mx);
  }
  void set(int p, int v, int t = 1) {
    if (seg[t].l == seg[t].r) {
      seg[t].mx = v;
      return;
    }
    pushdown(t);
    int mid = (seg[t].l + seg[t].r) >> 1;
    if (p <= mid)
      set(p, v, (t << 1));
    else
      set(p, v, ((t << 1) | 1));
    seg[t].mx = max(seg[(t << 1)].mx, seg[((t << 1) | 1)].mx);
  }
  int query(int l, int r, int t = 1) {
    if (l > r) return 0;
    if (seg[t].l == l && seg[t].r == r) return seg[t].mx;
    pushdown(t);
    int mid = (seg[t].l + seg[t].r) >> 1;
    if (r <= mid)
      return query(l, r, (t << 1));
    else if (l > mid)
      return query(l, r, ((t << 1) | 1));
    else
      return max(query(l, mid, (t << 1)), query(mid + 1, r, ((t << 1) | 1)));
  }
} m1;
int n, m, l[200111], r[200111];
vector<int> st[200111], ed[200111];
int main() {
  get_int(n), get_int(m);
  m1.build(1, m);
  for (int i = 1; i <= n; i++) {
    get_int(l[i]), get_int(r[i]);
    st[l[i] + 1].push_back(i);
    m1.set(i, i);
  }
  int ans = max(n, m);
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j < (int)st[i].size(); j++) {
      int id = st[i][j];
      m1.add(l[id] + 1, r[id] - 1, 1);
    }
    ans = max(ans, m1.query(i + 1, m) - i + 1);
  }
  print_int(ans - m), puts("");
  return 0;
}
