#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = (int)1e9 + 7;
const int N = (int)2e5 + 7;
int lazy[N << 2], tree[N << 2];
void Add(int rt, int val) {
  lazy[rt] += val;
  tree[rt] += val;
}
void pushdown(int rt) {
  if (lazy[rt]) {
    Add(rt << 1, lazy[rt]);
    Add(rt << 1 | 1, lazy[rt]);
    lazy[rt] = 0;
  }
}
void update(int ql, int qr, int val, int l, int r, int rt) {
  if (ql <= l && qr >= r) {
    Add(rt, val);
    return;
  }
  pushdown(rt);
  int m = (l + r) >> 1;
  if (ql <= m) update(ql, qr, val, l, m, rt << 1);
  if (m < qr) update(ql, qr, val, m + 1, r, rt << 1 | 1);
  tree[rt] = max(tree[rt << 1], tree[rt << 1 | 1]);
}
int query(int ql, int qr, int l, int r, int rt) {
  if (ql <= l && qr >= r) return tree[rt];
  pushdown(rt);
  int m = (l + r) >> 1;
  int res = 0;
  if (ql <= m) res = max(res, query(ql, qr, l, m, rt << 1));
  if (m < qr) res = max(res, query(ql, qr, m + 1, r, rt << 1 | 1));
  return res;
}
pair<int, int> p[N];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = (0); i < (n); ++i) {
    scanf("%d%d", &p[i].first, &p[i].second);
  }
  sort(p, p + n);
  int ans = 0;
  for (int i = (1); i < (m + 2); ++i) {
    update(i, i, i - 1, 1, m + 1, 1);
  }
  for (int i = 0, j; i < n; i = j) {
    for (j = i; j < n && p[i].first == p[j].first; j++)
      ;
    for (int k = (j)-1; k >= (i); --k) {
      update(1, p[k].second, 1, 1, m + 1, 1);
    }
    ans = max(ans, query(1, m + 1, 1, m + 1, 1) - p[i].first - m);
  }
  printf("%d\n", ans);
  return 0;
}
