#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chmax(T& a, const T& b) {
  return a < b ? (a = b, true) : false;
}
template <typename T>
bool chmin(T& a, const T& b) {
  return b < a ? (a = b, true) : false;
}
int N, M;
const int MAX_N = 200000, MAX_V = MAX_N * 4 + 17;
int L[MAX_N], R[MAX_N];
const int INF = 1e9;
struct edge {
  int to, rev, cap;
};
vector<edge> G[MAX_V];
int level[MAX_V], iter[MAX_V];
inline void add_edge(int from, int to, int cap) {
  G[from].push_back(edge{to, int(G[to].size()), cap});
  G[to].push_back(edge{from, int(G[from].size()) - 1, 0});
}
inline void bfs(int s) {
  memset(level, -1, sizeof(level));
  queue<int> q;
  level[s] = 0;
  q.push(s);
  while (q.empty() == false) {
    int curr = q.front();
    q.pop();
    for (int i = 0; i < int(G[curr].size()); i++) {
      edge& e = G[curr][i];
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[curr] + 1;
        q.push(e.to);
      }
    }
  }
}
inline bool dfs(int curr, int t) {
  if (curr == t) return true;
  for (int& i = iter[curr]; i < int(G[curr].size()); i++) {
    edge& e = G[curr][i];
    if (e.cap > 0 && level[e.to] > level[curr]) {
      if (dfs(e.to, t) == false) continue;
      e.cap--;
      G[e.to][e.rev].cap++;
      return true;
    }
  }
  return false;
}
inline int max_flow(int s, int t) {
  int flow = 0, d;
  while (true) {
    bfs(s);
    if (level[t] < 0) break;
    memset(iter, 0, sizeof(iter));
    while (dfs(s, t)) flow++;
  }
  return flow;
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = int(0); i < int(N); i++) {
    scanf("%d%d", &L[i], &R[i]);
    L[i]--;
    R[i]--;
  }
  int s = N + M * 3, t = N + M * 3 + 1;
  for (int i = int(0); i < int(N); i++) {
    add_edge(s, i, 1);
    if (L[i] >= 0 && L[i] < M) add_edge(i, L[i] + N, 1);
    if (R[i] >= 0 && R[i] < M) add_edge(i, R[i] + N + M, 1);
  }
  for (int i = int(0); i < int(M); i++) {
    if (i > 0) {
      add_edge(i + N, (i - 1) + N, INF);
    }
    if (i + 1 < M) {
      add_edge(i + N + M, (i + 1) + N + M, INF);
    }
    add_edge(i + N, i + N + M * 2, 1);
    add_edge(i + N + M, i + N + M * 2, 1);
    add_edge(i + N + M * 2, t, 1);
  }
  int ans = N - max_flow(s, t);
  printf("%d\n", ans);
  return 0;
}
