#include <bits/stdc++.h>
using std::cin;
using std::cout;
const int maxn = 200200;
const int mod = 998244353;
inline long long pow(long long a, int b, int ans = 1) {
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1) ans = ans * a % mod;
  return ans;
}
inline long long inverse(int x) { return pow(x, mod - 2); }
int tag[maxn << 2], tree[maxn << 2];
int n, m;
inline void put(int x, int v) { tag[x] += v, tree[x] += v; }
inline void down(int x) {
  put(x << 1, tag[x]);
  put(x << 1 | 1, tag[x]);
  tag[x] = 0;
}
inline void add(int ql, int qr, int v, int cur = 1, int l = 0, int r = m + 1) {
  if (ql <= l && r <= qr) return put(cur, v);
  int mid = l + r >> 1;
  down(cur);
  if (ql <= mid) add(ql, qr, v, cur << 1, l, mid);
  if (mid < qr) add(ql, qr, v, cur << 1 | 1, mid + 1, r);
  tree[cur] = std::max(tree[cur << 1], tree[cur << 1 | 1]);
}
inline int qry(int ql, int qr, int cur = 1, int l = 0, int r = m + 1) {
  if (ql <= l && r <= qr) return tree[cur];
  int mid = l + r >> 1, ans = -1e9;
  down(cur);
  if (ql <= mid) ans = std::max(ans, qry(ql, qr, cur << 1, l, mid));
  if (mid < qr) ans = std::max(ans, qry(ql, qr, cur << 1 | 1, mid + 1, r));
  return ans;
}
std::vector<int> v[maxn];
int main() {
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = (1), iend = (n); i <= iend; ++i) {
    int l, r;
    cin >> l >> r;
    v[l].push_back(r);
  }
  for (int i = (1), iend = (m + 1); i <= iend; ++i) add(i, i, i);
  int ans = 0;
  for (int i = (0), iend = (m); i <= iend; ++i) {
    for (int j : v[i]) add(1, j, 1);
    ans = std::max(ans, qry(i + 1, m + 1) - i - m - 1);
  }
  cout << ans << '\n';
}
