#include <bits/stdc++.h>
using namespace std;
int read() {
  int ans = 0;
  char ch = getchar();
  for (; (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
    ;
  bool flag = 0;
  if (ch == '-') flag = 1, ch = getchar();
  for (; ch >= '0' && ch <= '9'; ch = getchar()) ans = ans * 10 + ch - '0';
  if (flag) ans = -ans;
  return ans;
}
void writ(int n) {
  char ch[25];
  int m = 0;
  if (n < 0) putchar('-'), n = -n;
  if (n == 0) {
    putchar('0');
    return;
  }
  for (; n; n /= 10) ch[m++] = n % 10ll + '0';
  for (; m;) putchar(ch[--m]);
}
const int N = 1 << 23, inf = 0x7fffffff;
int st[N], p1[N], pv[N], pf[N], tot, u2, shana, n, d[N], ts[N];
void side(int u, int v, int f) {
  pv[tot] = v;
  pf[tot] = f;
  p1[tot] = st[u];
  st[u] = tot++;
  swap(u, v);
  f = 0;
  pv[tot] = v;
  pf[tot] = f;
  p1[tot] = st[u];
  st[u] = tot++;
}
int sap(int u, int flow) {
  if (u == shana) return flow;
  int rest = flow;
  for (int &i = ts[u]; i; i = p1[i])
    if (d[u] == d[pv[i]] - 1 && pf[i]) {
      int e = sap(pv[i], min(rest, pf[i]));
      rest -= e;
      pf[i] -= e;
      pf[i ^ 1] += e;
      if (!rest) return flow;
    }
  return flow - rest;
}
int dinic() {
  int ans = 0;
  for (;;) {
    static int q[N];
    memset(d, 0, (shana + 1) * 4);
    for (int i = 0; i <= shana; i++) {
      ts[i] = st[i];
    }
    q[0] = u2;
    d[u2] = 1;
    int w = 1;
    for (int t = 0; t < w; t++) {
      int u = q[t];
      for (int i = st[u]; i; i = p1[i])
        if (pf[i] && !d[pv[i]]) {
          d[pv[i]] = d[u] + 1;
          q[w++] = pv[i];
          if (pv[i] == shana) {
            w = 0;
            break;
          }
        }
    }
    if (!d[shana]) return ans;
    ans += sap(u2, inf);
  }
}
int main() {
  int m;
  n = read();
  m = read();
  tot = 2;
  u2 = n + m * 3;
  shana = u2 + 1;
  memset(st, 0, sizeof(st));
  for (int i = 0; i < n; i++) {
    int x = read() - 1;
    int y = read() - 1;
    side(u2, i, 1);
    side(i, x + n, 1);
    side(i, y + n + m, 1);
  }
  for (int i = 0; i < m; i++) {
    if (i) side(i + n, i - 1 + n, inf);
    if (i < m - 1) side(i + n + m, i + 1 + n + m, inf);
    side(i + n, i + n + m + m, 1);
    side(i + n + m, i + n + m + m, 1);
    side(i + n + m + m, shana, 1);
  }
  writ(n - dinic());
  puts("");
  return 0;
}
