#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 5;
pair<int, int> p[maxn];
int f[maxn << 2], lz[maxn * 4], m;
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first == b.first)
    return a.second > b.second;
  else
    return a.first < b.first;
}
void build(int t, int l, int r) {
  if (l == r) {
    f[t] = l - m - 1;
    return;
  }
  int mid = (l + r) >> 1;
  build(t << 1, l, mid);
  build(t << 1 | 1, mid + 1, r);
  f[t] = max(f[t << 1], f[t << 1 | 1]);
}
void push_down(int t) {
  if (lz[t]) {
    f[t << 1] += lz[t];
    lz[t << 1] += lz[t];
    f[t << 1 | 1] += lz[t];
    lz[t << 1 | 1] += lz[t];
    lz[t] = 0;
  }
}
void add(int t, int l, int r, int x) {
  if (r <= x) {
    f[t]++;
    lz[t]++;
    return;
  }
  push_down(t);
  int mid = (l + r) >> 1;
  add(t << 1, l, mid, x);
  if (x > mid) add(t << 1 | 1, mid + 1, r, x);
  f[t] = max(f[t << 1], f[t << 1 | 1]);
}
int query(int t, int l, int r, int x) {
  if (l >= x) return f[t];
  push_down(t);
  int mid = (l + r) >> 1;
  int res = query(t << 1 | 1, mid + 1, r, x);
  if (x <= mid) res = max(res, query(t << 1, l, mid, x));
  return res;
}
int main() {
  int n, i, res = 0;
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++) scanf("%d%d", &p[i].first, &p[i].second);
  sort(p, p + n, cmp);
  build(1, 1, m);
  for (i = 0; i < n; i++) {
    add(1, 1, m, p[i].second);
    res = max(res, query(1, 1, m, p[i].first + 1) - p[i].first);
  }
  printf("%d\n", res);
  return 0;
}
