#include <bits/stdc++.h>
using namespace std;
struct segtree {
  vector<long long> l, r, mn, tad;
  void ad(long long a, long long b, long long v, long long id) {
    if (b < l[id] || a > r[id]) return;
    if (a <= l[id] && b >= r[id]) {
      mn[id] += v;
      tad[id] += v;
      return;
    }
    prop(id);
    ad(a, b, v, 2 * id);
    ad(a, b, v, 2 * id + 1);
    pul(id);
  }
  void prop(long long id) {
    if (tad[id] != 0 && l[id] != r[id]) {
      ad(l[id], r[id], tad[id], 2 * id);
      ad(l[id], r[id], tad[id], 2 * id + 1);
    }
    tad[id] = 0;
  }
  void pul(long long id) {
    assert(l[id] != r[id]);
    mn[id] = min(mn[2 * id], mn[2 * id + 1]);
  }
  void qr(long long a, long long b, long long& tt, long long id) {
    if (b < l[id] || a > r[id]) return;
    if (a <= l[id] && b >= r[id]) {
      tt = min(tt, mn[id]);
      return;
    }
    prop(id);
    pul(id);
    qr(a, b, tt, 2 * id);
    qr(a, b, tt, 2 * id + 1);
  }
  void build(long long m) {
    long long rm = m;
    while (m & (m - 1)) ++m;
    l.clear();
    r.clear();
    tad.clear();
    mn.clear();
    l.resize(2 * m);
    r.resize(2 * m);
    tad.resize(2 * m, 0);
    mn.resize(2 * m, 0);
    for (long long i = 0; i < (m); ++i) {
      l[i + m] = r[i + m] = i;
    }
    for (long long i = m - 1; i >= 1; --i) {
      l[i] = l[2 * i];
      r[i] = r[2 * i + 1];
    }
    for (long long i = m; i < m + rm; ++i) mn[i] = m + rm - i - 1;
    for (long long i = m - 1; i >= 1; --i) {
      mn[i] = min(mn[2 * i], mn[2 * i + 1]);
    }
  }
};
segtree st;
vector<pair<long long, long long> > tvls;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  st.build(m + 1);
  long long l, r;
  for (long long z = 0; z < (n); ++z) {
    cin >> l >> r;
    tvls.push_back(make_pair(l, r - 1));
  }
  sort((tvls).begin(), (tvls).end());
  long long mx = 0;
  long long q;
  for (long long z = 0; z < (n); ++z) {
    st.ad(0, tvls[z].second, -1, 1);
    q = 1e9;
    st.qr(0, m, q, 1);
    mx = max(mx, -(tvls[z].first + q));
  }
  cout << mx << endl;
  return 0;
}
