#include <bits/stdc++.h>
using namespace std;
template <class T>
void debug(T itr1, T itr2) {
  auto now = itr1;
  while (now < itr2) {
    cout << *now << " ";
    now++;
  }
  cout << endl;
}
const long long MOD = pow(10, 9) + 7;
const long long LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;
vector<long long> fac;
void c_fac(int x = pow(10, 6) + 10) {
  fac.resize(x, true);
  for (long long i = (long long)0; i < (long long)x; i++)
    fac[i] = i ? (fac[i - 1] * i) % MOD : 1;
}
long long inv(long long a, long long m = MOD) {
  long long b = m, x = 1, y = 0;
  while (b != 0) {
    int d = a / b;
    a -= b * d;
    swap(a, b);
    x -= y * d;
    swap(x, y);
  }
  return (x + m) % m;
}
long long nck(long long n, long long k) {
  return fac[n] * inv(fac[k] * fac[n - k] % MOD) % MOD;
}
long long gcd(long long a, long long b) {
  if (a < b) swap(a, b);
  return b == 0 ? a : gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
struct edge {
  int to;
  long long cap;
  long long rev;
};
vector<vector<edge>> adj;
vector<bool> used;
void add_edge(int from, int to, int cap) {
  adj[from].push_back((edge){to, cap, (int)adj[to].size()});
  adj[to].push_back((edge){from, 0, (int)adj[from].size() - 1});
}
long long dfs(int n, int to, long long f) {
  if (n == to) return f;
  used[n] = true;
  for (auto &x : adj[n]) {
    if (!used[x.to] && x.cap) {
      long long d = dfs(x.to, to, min(f, x.cap));
      if (d) {
        x.cap -= d;
        adj[x.to][x.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}
long long FordFulkerson(int from, int to) {
  int N = adj.size();
  used.resize(N, false);
  long long flow = 0;
  while (true) {
    fill(used.begin(), used.end(), false);
    if (long long f = dfs(from, to, LLINF))
      flow += f;
    else
      return flow;
  }
}
int main() {
  long long N, M;
  cin >> N >> M;
  long long L[N], R[N];
  for (long long i = (long long)0; i < (long long)N; i++) cin >> L[i] >> R[i];
  adj.resize(N + M * 2 + 6);
  for (long long i = (long long)0; i < (long long)N; i++) add_edge(0, i + 1, 1);
  for (long long i = (long long)0; i < (long long)N; i++)
    add_edge(i + 1, N + 1 + L[i], 1);
  for (long long i = (long long)0; i < (long long)M + 1; i++)
    add_edge(N + 1 + (i + 1), N + 1 + i, N);
  for (long long i = (long long)0; i < (long long)N; i++)
    add_edge(i + 1, N + M + 3 + R[i], 1);
  for (long long i = (long long)0; i < (long long)M + 1; i++)
    add_edge(N + M + 3 + i, N + M + 3 + (i + 1), N);
  for (long long i = (long long)0; i < (long long)M + 1; i++)
    add_edge(N + 1 + (i + 1), N + M * 2 + 5, 1);
  for (long long i = (long long)0; i < (long long)M + 1; i++)
    add_edge(N + M + 3 + i, N + M * 2 + 5, 1);
  long long result = N - FordFulkerson(0, N + M * 2 + 5);
  cout << result << endl;
  return 0;
}
