#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int t, f, next;
  Edge() {}
  Edge(int a, int b, int c) : t(a), f(b), next(c) {}
};
Edge e[10000000];
int head[400005], vs, vt, tot = -1;
void addEdge(int x, int y, int z) {
  e[++tot] = Edge(y, z, head[x]);
  head[x] = tot;
  e[++tot] = Edge(x, 0, head[y]);
  head[y] = tot;
}
namespace Flow {
int d[400005], cur[400005];
queue<int> q;
bool bfs() {
  while (!q.empty()) q.pop();
  memset(d, 255, sizeof(d));
  d[vs] = 0;
  cur[vs] = head[vs];
  q.push(vs);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = head[x]; i != -1; i = e[i].next)
      if (e[i].f && d[e[i].t] == -1) {
        int u = e[i].t;
        d[u] = d[x] + 1;
        cur[u] = head[u];
        if (u == vt) return 1;
        q.push(u);
      }
  }
  return 0;
}
int dfs(int x, int a) {
  if (x == vt || !a) return a;
  int ans = 0;
  for (int &i = cur[x]; i != -1; i = e[i].next)
    if (e[i].f && d[e[i].t] == d[x] + 1) {
      int u = e[i].t;
      int f = dfs(u, min(a, e[i].f));
      if (f) {
        e[i].f -= f;
        e[i ^ 1].f += f;
        ans += f;
        a -= f;
        if (!a) break;
      }
    }
  return ans;
}
int maxflow() {
  int ans = 0;
  while (bfs()) ans += dfs(vs, 0x3f3f3f3f);
  return ans;
}
}  // namespace Flow
int main() {
  memset(head, 255, sizeof(head));
  int n, m;
  scanf("%d%d", &n, &m);
  vs = 0;
  vt = n + 3 * m + 1;
  for (int i = 1; i <= n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    addEdge(vs, i, 1);
    if (x) addEdge(i, n + x, 1);
    if (y <= m) addEdge(i, n + m + y, 1);
  }
  for (int i = 1; i <= m; i++) {
    if (i > 1) addEdge(n + i, n + i - 1, 0x3f3f3f3f);
    if (i < m) addEdge(n + m + i, n + m + i + 1, 0x3f3f3f3f);
    addEdge(n + i, n + 2 * m + i, 1);
    addEdge(n + m + i, n + 2 * m + i, 1);
    addEdge(n + 2 * m + i, vt, 1);
  }
  printf("%d\n", n - Flow::maxflow());
  return 0;
}
