#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, m, col[N << 2], mx[N << 2];
vector<int> V[N];
inline void read(int &i) {
  i = 0;
  char c = getchar();
  bool j = 0;
  for (; !isdigit(c); c = getchar())
    if (c == '-') j = 1;
  for (; isdigit(c); c = getchar()) i = (i << 1) + (i << 3) + c - '0';
  i = j ? -i : i;
}
void build(int u, int l, int r) {
  if (l == r) {
    mx[u] = -(m - l + 1);
    return;
  }
  int mid = (l + r) >> 1;
  build(u << 1, l, mid);
  build(u << 1 | 1, mid + 1, r);
  mx[u] = max(mx[u << 1], mx[u << 1 | 1]);
}
inline void color(int u, int v) { mx[u] += v, col[u] += v; }
inline void push(int u) {
  if (col[u]) color(u << 1, col[u]), color(u << 1 | 1, col[u]), col[u] = 0;
}
void mdf(int u, int l, int r, int L, int R) {
  if (L <= l && r <= R) {
    color(u, 1);
    return;
  }
  int mid = (l + r) >> 1;
  push(u);
  if (L <= mid) mdf(u << 1, l, mid, L, R);
  if (mid < R) mdf(u << 1 | 1, mid + 1, r, L, R);
  mx[u] = max(mx[u << 1], mx[u << 1 | 1]);
}
int qmx(int u, int l, int r, int L, int R) {
  if (L <= l && r <= R) return mx[u];
  int mid = (l + r) >> 1, as = -1e9;
  push(u);
  if (L <= mid) as = max(as, qmx(u << 1, l, mid, L, R));
  if (mid < R) as = max(as, qmx(u << 1 | 1, mid + 1, r, L, R));
  return as;
}
int main() {
  read(n);
  read(m);
  for (int i = 1, x, y; i <= n; ++i) read(x), read(y), V[x].push_back(y);
  build(1, 1, m + 1);
  int ans = 0;
  for (int i = 0; i <= m; ++i) {
    for (int j = 0; j < (int)V[i].size(); ++j) {
      int x = V[i][j];
      mdf(1, 1, m + 1, 1, x);
    }
    ans = max(qmx(1, 1, m + 1, 1, m + 1) - i, ans);
  }
  printf("%d\n", ans);
  return 0;
}
