#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using ld = long double;
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "[";
  for (ll i = ll(0); i < ll((ll)v.size()); i++) {
    if (i) os << ",";
    os << v[i];
  }
  os << "]";
  return os;
}
ll read() {
  ll i;
  scanf("%lld", &i);
  return i;
}
void printSpace() { printf(" "); }
void printEoln() { printf("\n"); }
void print(ll x, ll suc = 1) {
  printf("%lld", x);
  if (suc == 1) printEoln();
  if (suc == 2) printSpace();
}
string readString() {
  static char buf[3341919];
  scanf("%s", buf);
  return string(buf);
}
char* readCharArray() {
  static char buf[3341919];
  static ll bufUsed = 0;
  char* ret = buf + bufUsed;
  scanf("%s", ret);
  bufUsed += strlen(ret) + 1;
  return ret;
}
template <class T, class U>
void chmax(T& a, U b) {
  if (a < b) a = b;
}
template <class T, class U>
void chmin(T& a, U b) {
  if (a > b) a = b;
}
template <class T>
T Sq(const T& t) {
  return t * t;
}
const ll inf = LLONG_MAX / 3;
signed main() {
  ll n = read(), m = read();
  vector<vi> rng(m + 1);
  vi cnt(m + 1, 0);
  for (ll i = ll(0); i < ll(n); i++) {
    ll l = read(), r = read() - 1;
    rng[l].push_back(r);
    cnt[l]++;
  }
  for (ll i = m; i >= 1; i--) cnt[i - 1] += cnt[i];
  ll edgeL = -1, mn = n;
  for (ll i = ll(0); i < ll(m); i++) {
    mn = min(mn - 1, cnt[i + 1] - 1);
    if (mn < 0) break;
    edgeL = i;
  }
  priority_queue<ll> pq;
  for (ll i = m; i >= 0; i--) {
    if (i <= edgeL) pq.pop();
    for (auto t : rng[i]) pq.push(t);
  }
  cnt.assign(m + 1, 0);
  mn = pq.size();
  while (!pq.empty()) {
    ll t = pq.top();
    pq.pop();
    cnt[t]++;
  }
  for (ll i = ll(0); i < ll(m); i++) cnt[i + 1] += cnt[i];
  ll edgeR = m;
  for (ll i = m - 1; i >= 0; i--) {
    mn = min(mn - 1, cnt[i] - 1);
    if (mn < 0) break;
    edgeR = i;
  }
  cout << n - min(n, (edgeL + 1) + (m - edgeR)) << endl;
}
