#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100010;
const double eps = 1e-6;
const long long mod = 1000000007;
const int inf = 1 << 30;
void Debug() { cout << '\n'; }
template <class FIRST, class... REST>
void Debug(FIRST arg, REST... rest) {
  cout << arg << " ";
  Debug(rest...);
}
template <class T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  out << "[";
  if (!v.empty()) {
    for (int i = 0; i < (int)v.size() - 1; i++) out << v[i] << ", ";
    out << v.back();
  }
  out << "]";
  return out;
}
template <class S, class T>
ostream& operator<<(ostream& out, const pair<S, T>& v) {
  out << "(" << v.first << ", " << v.second << ")";
  return out;
}
struct node {
  long long sum, lazy;
  node() : sum(0), lazy(0) {}
  node(long long sum, long long lazy) : sum(sum), lazy(lazy) {}
};
int M;
node seg[200000 * 4];
inline void lazy_eval(int k, int l, int r) {
  if (seg[k].lazy) {
    seg[k * 2 + 1].sum += seg[k].lazy;
    seg[k * 2 + 1].lazy += seg[k].lazy;
    seg[k * 2 + 2].sum += seg[k].lazy;
    seg[k * 2 + 2].lazy += seg[k].lazy;
    seg[k].lazy = 0;
  }
}
void update(int a, int b, long long s, int k = 0, int l = 0, int r = M + 1) {
  if (b <= l || r <= a) return;
  if (a <= l && r <= b) {
    seg[k].sum += s;
    seg[k].lazy += s;
  } else {
    int m = (l + r) / 2;
    lazy_eval(k, l, r);
    update(a, b, s, k * 2 + 1, l, m);
    update(a, b, s, k * 2 + 2, m, r);
    seg[k].sum = max(seg[k].sum, seg[k * 2 + 1].sum);
    seg[k].sum = max(seg[k].sum, seg[k * 2 + 2].sum);
  }
}
long long get_sum(int a, int b, int k = 0, int l = 0, int r = M + 1) {
  if (b <= l || r <= a) return 0;
  if (a <= l && r <= b)
    return seg[k].sum;
  else {
    int m = (l + r) / 2;
    lazy_eval(k, l, r);
    long long lsum = get_sum(a, b, k * 2 + 1, l, m);
    long long rsum = get_sum(a, b, k * 2 + 2, m, r);
    return max(lsum, rsum);
  }
}
void show_seg() {
  vector<int> vector<long long>;
  for (int i = 0; i < M; i++) vector<long long>.push_back(get_sum(i, i + 1));
  cout << vector<long long> << "\n";
}
int N;
vector<int> P[MAX_N];
void solve() {
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    P[a].push_back(b);
  }
  M++;
  for (int i = 0; i < M; i++) update(i, i + 1, i);
  long long res = max(N - M + 1, 0);
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < (int)P[i].size(); j++) {
      int n = P[i][j];
      update(0, n + 1, 1);
    }
    long long t = get_sum(i + 2, M + 1);
    res = max(res, t - M - i);
  }
  cout << res << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed;
  cout.precision(20);
  solve();
  return 0;
}
