#include <bits/stdc++.h>
using namespace std;
long long Mod(long long val, long long m) {
  long long res = val % m;
  if (res < 0) res += m;
  return res;
}
vector<long long> isu(200002);
long long isudata[200002];
long long RMQdata[200002];
long long m;
long long M;
void update(long long l, long long r, long long x) {
  long long id = l;
  long long L, q, R;
  while (r >= id) {
    L = id;
    q = id / m;
    R = min((q + 1) * m - 1, r);
    if (R - L + 1 == m) {
      isudata[q] += x;
      RMQdata[q] += x;
    } else {
      for (long long i = L; i < R + 1; i++) {
        isu[i] += x;
      }
      long long res = 1e+16;
      long long test;
      for (long long i = m * q; i < min(M + 2, m * (q + 1)); i++) {
        test = isu[i] + isudata[i];
        res = min(res, test);
      }
      RMQdata[q] = res;
    }
    id = R + 1;
  }
}
long long RMQ(long long l, long long r) {
  long long id = l;
  long long L, q, R;
  long long res = 1e+16;
  while (r >= id) {
    L = id;
    q = id / m;
    R = min((q + 1) * m - 1, r);
    if (R - L + 1 == m) {
      res = min(res, RMQdata[q]);
    } else {
      long long test;
      for (long long i = L; i < R + 1; i++) {
        test = isu[i] + isudata[q];
        res = min(res, test);
      }
    }
    id = R + 1;
  }
  return res;
}
int main() {
  long long N;
  scanf("%lld", &N);
  scanf("%lld", &M);
  while (M + 2 >= m * m) {
    m += 1;
  }
  m -= 1;
  for (long long i = 1; i < M + 1; i++) {
    update(0, i, 1);
  }
  long long add = M - N;
  vector<pair<long long, long long> > hito(N);
  long long l, r;
  for (long long i = 0; i < N; i++) {
    scanf("%lld", &l);
    scanf("%lld", &r);
    pair<long long, long long> p = {l, r};
    hito[i] = make_pair(l, r);
  }
  sort(hito.begin(), hito.end());
  for (long long i = 0; i < N; i++) {
    long long l, r;
    l = hito[i].first;
    r = hito[i].second;
    update(0, r, -1);
    long long t = RMQ(l + 1, M + 1) + l;
    add = min(add, t);
  }
  long long zero = 0;
  long long ans = max(-add, zero);
  cout << ans << endl;
}
