#include <bits/stdc++.h>
using namespace std;
inline void up(int &x, const int &y) {
  if (x < y) x = y;
}
const int maxn = 210000;
int n, m;
vector<int> V[maxn];
int v[maxn];
struct segment {
  int c, flag;
} seg[maxn << 2];
void pushdown(const int x) {
  if (seg[x].flag) {
    int lc = x << 1, rc = lc | 1, fl = seg[x].flag;
    seg[x].flag = 0;
    seg[lc].c += fl, seg[lc].flag += fl;
    seg[rc].c += fl, seg[rc].flag += fl;
  }
}
void pushup(const int x) { seg[x].c = max(seg[x << 1].c, seg[x << 1 | 1].c); }
int lx, rx, c;
void upd(const int x, const int l, const int r) {
  if (rx < l || r < lx) return;
  if (lx <= l && r <= rx) {
    seg[x].flag += c, seg[x].c += c;
    return;
  }
  pushdown(x);
  int mid = l + r >> 1, lc = x << 1, rc = lc | 1;
  upd(lc, l, mid);
  upd(rc, mid + 1, r);
  pushup(x);
}
int query(const int x, const int l, const int r) {
  if (rx < l || r < lx) return 0;
  if (seg[x].c < 0) return 0;
  if (lx <= l && r <= rx) return seg[x].c;
  pushdown(x);
  int mid = l + r >> 1, lc = x << 1, rc = lc | 1;
  return max(query(lc, l, mid), query(rc, mid + 1, r));
}
int ans;
int main() {
  scanf("%d%d", &n, &m);
  ans = n - m;
  for (int i = 1; i <= n; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    V[l].push_back(r), V[r].push_back(l);
  }
  for (int i = 1; i <= m + 1; i++) {
    if (i - 2 >= 0) lx = 0, rx = i - 2, up(ans, query(1, 0, m) - (m - i + 1));
    lx = i - 1, rx = i - 1, c = n - (i - 1), upd(1, 0, m);
    for (int j = 0; j < V[i].size(); j++) {
      int k = V[i][j];
      if (k < i)
        lx = k, rx = i - 1, c = -1, upd(1, 0, m);
      else
        lx = 0, rx = i - 1, c = -1, upd(1, 0, m);
    }
  }
  up(ans, 0);
  printf("%d\n", ans);
  return 0;
}
