#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
long long INF = 1e9;
const int N = 300031;
const int MAXN = 900000;
int n, m;
int a[N], b[N];
vector<pair<int, int> > by_a, by_b;
int ans;
vector<pair<int, int> > order;
int t[4][N], toadd[4][N];
void push(int id, int v) {
  toadd[id][v * 2] += toadd[id][v];
  t[id][v * 2] += toadd[id][v];
  toadd[id][v * 2 + 1] += toadd[id][v];
  t[id][v * 2 + 1] += toadd[id][v];
  toadd[id][v] = 0;
}
void build(int id, int v, int tl, int tr) {
  if (tl == tr) {
    t[id][v] = -tl;
    return;
  }
  int sdfjahlfasfh = tl + tr;
  sdfjahlfasfh /= 2;
  build(id, v * 2, tl, sdfjahlfasfh);
  build(id, v * 2 + 1, sdfjahlfasfh + 1, tr);
  t[id][v] = max(t[id][v * 2], t[id][v * 2 + 1]);
}
int get(int id, int v, int tl, int tr, int l, int r) {
  if (l > r) return -1e9;
  if (tl == l && tr == r) return t[id][v];
  push(id, v);
  int sdfjahlfasfh = tl + tr;
  sdfjahlfasfh /= 2;
  return max(
      get(id, v * 2, tl, sdfjahlfasfh, l, min(r, sdfjahlfasfh)),
      get(id, v * 2 + 1, sdfjahlfasfh + 1, tr, max(sdfjahlfasfh + 1, l), r));
}
void add(int id, int v, int tl, int tr, int l, int r, int val) {
  if (l > r) return;
  if (tl == l && tr == r) {
    t[id][v] += val;
    toadd[id][v] += val;
    return;
  }
  push(id, v);
  int sdfjahlfasfh = tl + tr;
  sdfjahlfasfh /= 2;
  add(id, v * 2, tl, sdfjahlfasfh, l, min(r, sdfjahlfasfh), val);
  add(id, v * 2 + 1, sdfjahlfasfh + 1, tr, max(sdfjahlfasfh + 1, l), r, val);
  t[id][v] = max(t[id][v * 2], t[id][v * 2 + 1]);
}
int get_max(int id) { return t[id][1]; }
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
    b[i] = m + 1 - b[i];
    by_a.push_back(make_pair(a[i], i));
    by_b.push_back(make_pair(b[i], i));
  }
  build(1, 1, 0, m + 1);
  build(2, 1, 0, m + 1);
  ans = 1e9;
  for (int i = 1; i <= n; i++) {
    add(1, 1, 0, m + 1, a[i], m + 1, 1);
  }
  ans = get_max(1);
  order.clear();
  for (int i = 1; i <= n; i++) {
    order.push_back(make_pair(a[i], i));
  }
  sort(order.begin(), order.end());
  for (int i = 0; i < order.size(); i++) {
    int id = order[i].second;
    add(1, 1, 0, m + 1, a[id], m + 1, -1);
    add(2, 1, 0, m + 1, b[id], m + 1, 1);
    ans = min(ans, get_max(1) + get_max(2));
  }
  cout << ans << endl;
  cin.get();
  cin.get();
  return 0;
}
