#include <bits/stdc++.h>
static const int MAXN = 2e5 + 4;
static const int MAXM = 2e5 + 4;
static const int INF = 0x3fffffff;
int n, m;
namespace nwf {
static const int MAXN = ::MAXN + ::MAXM * 2 + 2;
static const int MAXM = 8e6 + 10;
int n, m, source, sink;
int head[MAXN], dest[MAXM], cap[MAXM], next[MAXM];
inline void init(int _n) {
  n = _n, m = 0;
  for (int i = 0; i < n; ++i) head[i] = -1;
}
inline void add_edge(int u, int v, int c) {
  dest[m] = v;
  cap[m] = c;
  next[m] = head[u];
  head[u] = m++;
  dest[m] = u;
  cap[m] = 0;
  next[m] = head[v];
  head[v] = m++;
}
int d[MAXN], cur[MAXN];
inline bool level_graph() {
  static int q[MAXN], qhead, qtail;
  memset(d, -1, sizeof d);
  d[source] = 0, q[0] = source;
  qhead = 0, qtail = 1;
  while (qhead < qtail) {
    int u = q[qhead++];
    for (int w = head[u]; w != -1; w = next[w])
      if (cap[w] > 0 && d[dest[w]] == -1) {
        d[dest[w]] = d[u] + 1;
        q[qtail++] = dest[w];
      }
  }
  return (d[sink] > 0);
}
int augment(int u, int cur_cap = INF) {
  if (u == sink) return cur_cap;
  int new_cap;
  for (int &w = cur[u]; w != -1; w = next[w])
    if (cap[w] > 0 && d[dest[w]] == d[u] + 1 &&
        (new_cap = augment(dest[w], std::min(cur_cap, cap[w]))) > 0) {
      cap[w] -= new_cap;
      cap[w ^ 1] += new_cap;
      return new_cap;
    }
  return 0;
}
inline int deluge() {
  int ans = 0, aug;
  while (level_graph()) {
    for (int i = 0; i < n; ++i) cur[i] = head[i];
    while ((aug = augment(source)) > 0) ans += aug;
  }
  return ans;
}
}  // namespace nwf
int main() {
  scanf("%d%d", &n, &m);
  nwf::init(n + m * 2 + 2);
  nwf::source = n + m * 2;
  nwf::sink = n + m * 2 + 1;
  for (int i = 0; i < m; ++i) {
    if (i < m - 1) nwf::add_edge(i, i + 1, INF);
    if (i > 0) nwf::add_edge(m + i, m + i - 1, INF);
    nwf::add_edge(i, m + i, 1);
    nwf::add_edge(m + i, nwf::sink, 1);
  }
  for (int i = 0, l, r; i < n; ++i) {
    scanf("%d%d", &l, &r);
    --l, --r;
    nwf::add_edge(nwf::source, m * 2 + i, 1);
    if (l != -1) nwf::add_edge(m * 2 + i, m + l, 1);
    if (r != m) nwf::add_edge(m * 2 + i, r, 1);
  }
  printf("%d\n", n - nwf::deluge());
  return 0;
}
