#include <bits/stdc++.h>
using namespace std;
const int N = 3e6 + 1;
const int inf = 0x3f3f3f3f;
int n, m;
int L[N], R[N];
struct edge {
  int tail[N], cnt, to[N], w[N], nxt[N];
  inline void init() {
    memset(tail, 0, sizeof tail);
    cnt = 1;
  }
  inline void add_edge(int x, int y, int z) {
    to[++cnt] = y;
    nxt[cnt] = tail[x];
    tail[x] = cnt;
    w[cnt] = z;
    to[++cnt] = x;
    nxt[cnt] = tail[y];
    tail[y] = cnt;
    w[cnt] = 0;
  }
} e;
int rt;
struct Segment_Tree {
  int cnt, lc[N], rc[N], tag[N];
  void connect(int x, int l, int r, int id, int ql, int qr) {
    if (qr < l && ql > r) return;
    if (ql <= l && r <= qr) {
      e.add_edge(id, x, inf);
      return;
    }
    if (ql <= (l + r) / 2) {
      if (!lc[x]) lc[x] = ++cnt;
      connect(lc[x], l, (l + r) / 2, id, ql, qr);
    }
    if (qr > (l + r) / 2) {
      if (!rc[x]) rc[x] = ++cnt;
      connect(rc[x], (l + r) / 2 + 1, r, id, ql, qr);
    }
  }
  void c2(int x, int l, int r) {
    if (!lc[x])
      for (int w = l; w <= (l + r) / 2; ++w) e.add_edge(x, n + w, inf);
    if (!rc[x])
      for (int w = (l + r) / 2 + 1; w <= r; ++w) e.add_edge(x, n + w, inf);
    if (lc[x]) {
      e.add_edge(x, lc[x], inf);
      c2(lc[x], l, (l + r) / 2);
    }
    if (rc[x]) {
      e.add_edge(x, rc[x], inf);
      c2(rc[x], (l + r) / 2 + 1, r);
    }
  }
} S;
int dis[N];
int ss, tt;
queue<int> q1;
bool bfs() {
  memset(dis, 0x3f, sizeof dis);
  dis[ss] = 0;
  q1.push(ss);
  while (!q1.empty()) {
    int x = q1.front();
    q1.pop();
    for (int i = e.tail[x]; i; i = e.nxt[i]) {
      int y = e.to[i];
      if (e.w[i] && dis[y] == inf) {
        dis[y] = dis[x] + 1;
        q1.push(y);
      }
    }
  }
  if (dis[tt] == inf) return false;
  return true;
}
int dfs(int x, int flow) {
  if (x == tt) return flow;
  int val = flow;
  for (int i = e.tail[x]; i; i = e.nxt[i]) {
    int res = 0;
    int y = e.to[i];
    if (e.w[i] && dis[y] == dis[x] + 1 && (res = dfs(y, min(e.w[i], val)))) {
      e.w[i] -= res;
      e.w[i ^ 1] += res;
      val -= res;
      if (val == 0) return flow;
    }
  }
  return flow - val;
}
int dinic() {
  int res = 0;
  while (bfs()) {
    res += dfs(ss, inf);
  }
  return res;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", L + i, R + i);
  }
  e.init();
  S.cnt = n + m + 1;
  rt = n + m + 1;
  ss = 0;
  for (int i = 1; i <= n; ++i) {
    e.add_edge(ss, i, 1);
    if (L[i]) S.connect(rt, 1, m, i, 1, L[i]);
    if (R[i] <= m) S.connect(rt, 1, m, i, R[i], m);
  }
  S.c2(rt, 1, m);
  tt = S.cnt + 10;
  for (int i = 1; i <= m; ++i) e.add_edge(i + n, tt, 1);
  printf("%d\n", n - dinic());
  return 0;
}
