#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int inf = 0x3f3f3f3f;
int n, m;
int tag[N << 2], mx[N << 2];
pair<int, int> a[N];
void Build(int x, int l, int r) {
  tag[x] = 0;
  if (l == r) {
    mx[x] = -l;
    return;
  }
  int mid = (l + r) >> 1;
  Build(x << 1, l, mid);
  Build(x << 1 | 1, mid + 1, r);
  mx[x] = max(mx[x << 1], mx[x << 1 | 1]);
}
inline void Pushdown(int x) {
  mx[x << 1] += tag[x];
  mx[x << 1 | 1] += tag[x];
  tag[x << 1] += tag[x];
  tag[x << 1 | 1] += tag[x];
  tag[x] = 0;
}
void Updata(int x, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr) {
    ++mx[x];
    ++tag[x];
    return;
  }
  Pushdown(x);
  int mid = (l + r) >> 1;
  if (mid >= ql) Updata(x << 1, l, mid, ql, qr);
  if (mid < qr) Updata(x << 1 | 1, mid + 1, r, ql, qr);
  mx[x] = max(mx[x << 1], mx[x << 1 | 1]);
}
int Query(int x, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr) return mx[x];
  int mid = (l + r) >> 1, ans = -inf;
  Pushdown(x);
  if (mid >= ql) {
    ans = max(ans, Query(x << 1, l, mid, ql, qr));
  }
  if (mid < qr) {
    ans = max(ans, Query(x << 1 | 1, mid + 1, r, ql, qr));
  }
  return ans;
}
void dfs(int x, int l, int r) {
  cout << x << ' ' << l << ' ' << r << ' ' << mx[x] << endl;
  Pushdown(x);
  int mid = (l + r) >> 1;
  if (l == r) return;
  dfs(x << 1, l, mid);
  dfs(x << 1 | 1, mid + 1, r);
}
bool cmp(pair<int, int> a, pair<int, int> b) { return a.second > b.second; }
int main() {
  scanf("%d%d", &n, &m);
  Build(1, 0, m + 1);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &a[i].first, &a[i].second);
  }
  sort(a + 1, a + n + 1, cmp);
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (i == 1 || a[i].second == a[i + 1].second) {
      Updata(1, 0, m + 1, a[i].first, m + 1);
    } else {
      ans = max(ans, Query(1, 0, m + 1, 0, a[i - 1].second - 1) - m +
                         a[i - 1].second - 1);
      Updata(1, 0, m + 1, a[i].first, m + 1);
    }
  }
  cout << ans << endl;
  return 0;
}
