#include <bits/stdc++.h>
using namespace std;
int n, m, a[200005], b[200005];
vector<int> vec[200050];
class segtree {
 public:
  long long seg[(1 << 19)];
  long long lazy[(1 << 19)];
  void init() {
    memset(seg, 0LL, sizeof(seg));
    memset(lazy, 0LL, sizeof(lazy));
  }
  void lazy_evaluate(int k) {
    if (k * 2 + 2 >= (1 << 18) * 2) return;
    lazy[k * 2 + 2] += lazy[k];
    lazy[k * 2 + 1] += lazy[k];
    seg[k * 2 + 2] += lazy[k];
    seg[k * 2 + 1] += lazy[k];
    lazy[k] = 0;
  }
  long long update(int beg, int end, int idx, int lb, int ub, long long num) {
    if (ub < beg || end < lb) {
      return seg[idx];
    }
    if (beg <= lb && ub <= end) {
      lazy[idx] += num;
      seg[idx] += num;
      return seg[idx];
    }
    if (lazy[idx]) {
      lazy_evaluate(idx);
    }
    return seg[idx] =
               max(update(beg, end, idx * 2 + 1, lb, (lb + ub) / 2, num),
                   update(beg, end, idx * 2 + 2, (lb + ub) / 2 + 1, ub, num));
  }
  long long query(int beg, int end, int idx, int lb, int ub) {
    if (ub < beg || end < lb) {
      return -1000000000000000000LL;
    }
    if (beg <= lb && ub <= end) {
      return seg[idx];
    }
    if (lazy[idx]) {
      lazy_evaluate(idx);
    }
    return max(query(beg, end, idx * 2 + 1, lb, (lb + ub) / 2),
               query(beg, end, idx * 2 + 2, (lb + ub) / 2 + 1, ub));
  }
} S;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i], &b[i]);
    vec[a[i]].push_back(b[i]);
  }
  int lb = -1, ub = n;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    S.init();
    for (int i = 1; i <= m + 1; i++) S.update(i, i, 0, 0, (1 << 18) - 1, i);
    for (int i = 0; i <= m; i++) {
      for (int j = 0; j < vec[i].size(); j++) {
        S.update(0, vec[i][j], 0, 0, (1 << 18) - 1, 1);
      }
      if (i + m + 1 + mid < S.query(i + 1, m + 1, 0, 0, (1 << 18) - 1)) {
        lb = mid;
        goto bad;
      }
    }
    ub = mid;
  bad:;
  }
  cout << ub << endl;
}
