#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, m, it[4 * N], lazy[4 * N];
vector<int> go[N];
void build(int i, int l, int r) {
  if (l == r) {
    it[i] = l - m;
    return;
  }
  int mid = (l + r) >> 1;
  build(i << 1, l, mid);
  build(i << 1 | 1, mid + 1, r);
  it[i] = max(it[i << 1], it[i << 1 | 1]);
}
void push(int i, int l, int r) {
  if (!lazy[i]) return;
  it[i] += lazy[i];
  if (l != r) {
    lazy[i << 1] += lazy[i], lazy[i << 1 | 1] += lazy[i];
  }
  lazy[i] = 0;
}
void update(int i, int l, int r, int u, int v) {
  push(i, l, r);
  if (l > v || u > r) return;
  if (u <= l && r <= v) {
    lazy[i] = 1, push(i, l, r);
    return;
  }
  int mid = (l + r) >> 1;
  update(i << 1, l, mid, u, v);
  update(i << 1 | 1, mid + 1, r, u, v);
  it[i] = max(it[i << 1], it[i << 1 | 1]);
}
int get(int i, int l, int r, int u, int v) {
  push(i, l, r);
  if (l > v || u > r) return 0;
  if (u <= l && r <= v) return it[i];
  int mid = (l + r) >> 1;
  return max(get(i << 1, l, mid, u, v), get(i << 1 | 1, mid + 1, r, u, v));
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  m++, build(1, 1, m);
  for (int i = 0; i < n; ++i) {
    int l, r;
    cin >> l >> r;
    go[l].push_back(r);
  }
  int res = 0;
  for (int i = 0; i < m; ++i) {
    for (auto j : go[i]) update(1, 1, m, i + 1, j);
    res = max(res, get(1, 1, m, i + 1, m) - i);
  }
  cout << res;
}
