#include <bits/stdc++.h>
using namespace std;
const int N = 1e6, M = 1e7;
int gi() {
  int w = 0;
  bool q = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-') c = getchar();
  if (c == '-') q = 0, c = getchar();
  while (c >= '0' && c <= '9') w = w * 10 + c - '0', c = getchar();
  return q ? w : -w;
}
const int inf = 1 << 30;
int head[N], nxt[M], to[M], w[M], tot = 1;
int dep[N], q[N], cur[N], S, T;
inline void link(int a, int b, int _w) {
  to[++tot] = b, nxt[tot] = head[a], head[a] = tot, w[tot] = _w;
  to[++tot] = a, nxt[tot] = head[b], head[b] = tot, w[tot] = 0;
}
inline bool bfs() {
  int i, k, l, r;
  for (i = S; i <= T; i++) dep[i] = 0;
  for (l = 0, dep[q[r = 1] = T] = 1; l != r;)
    for (i = head[k = q[++l]]; i; i = nxt[i])
      if (w[i ^ 1] && !dep[to[i]]) dep[q[++r] = to[i]] = dep[k] + 1;
  return dep[S];
}
inline int dfs(int k, int low) {
  if (k == T) return low;
  int s = 0, x;
  for (int &i = cur[k]; i; i = nxt[i])
    if (w[i] && dep[to[i]] == dep[k] - 1)
      if (x = dfs(to[i], min(low - s, w[i]))) {
        s += x;
        w[i] -= x;
        w[i ^ 1] += x;
        if (s == low) return s;
      }
  return s;
}
int main() {
  int n = gi(), m = gi(), i, k;
  S = 0, T = n + m * 3 + 1;
  for (i = 1; i <= m; i++) {
    link(i, T, 1);
    link(i + m, i, 1);
    link(i + m + m, i, 1);
    if (i < m) link(i + m, i + m + 1, inf);
    if (i > 1) link(i + m + m, i + m + m - 1, inf);
  }
  for (i = 1; i <= n; i++) {
    link(S, i + m * 3, 1);
    if (k = gi()) link(i + m * 3, m + m + k, 1);
    if ((k = gi()) != m + 1) link(i + m * 3, m + k, 1);
  }
  k = n;
  while (bfs()) {
    for (i = S; i <= T; i++) cur[i] = head[i];
    k -= dfs(S, inf);
  }
  printf("%d\n", k);
  return 0;
}
