#include <bits/stdc++.h>
using namespace std;
struct edge {
  int v, to, cap;
  edge(int a, int b, int c) { v = a, to = b, cap = c; }
  edge() {}
};
const int N = 1e6 + 7;
vector<edge> e;
vector<int> g[N];
int us[N];
int usbs = 1;
int d[N];
int q[N];
int ded[N];
int ptr[N];
int after_bfs = 1;
int visb = 1;
long long ans = 0;
int qh, qt;
int s, t;
int l;
void add_edge(int u, int v, int cap) {
  g[u].push_back(e.size());
  e.push_back({u, v, cap});
  g[v].push_back(e.size());
  e.push_back({v, u, 0});
}
bool bfs() {
  qh = qt = 0;
  us[s] = usbs;
  q[qt++] = s;
  d[s] = 0;
  while (qh < qt) {
    int v = q[qh++];
    for (auto c : g[v]) {
      if (us[e[c].to] != usbs && e[c].cap > 0) {
        d[e[c].to] = d[v] + 1;
        us[e[c].to] = usbs;
        q[qt++] = e[c].to;
      }
    }
  }
  return (us[t] == usbs++);
}
int dfs(int v = s, int c = 1e9) {
  if (!c) {
    return 0;
  }
  if (v == t) {
    ans += c;
    return c;
  }
  if (ded[v] != after_bfs) {
    ded[v] = after_bfs;
    ptr[v] = 0;
  }
  for (; ptr[v] < (int)g[v].size(); ptr[v]++) {
    int ind = g[v][ptr[v]];
    if (e[ind].cap > 0 && d[e[ind].to] == d[v] + 1) {
      int x = dfs(e[ind].to, min(c, e[ind].cap));
      if (x) {
        e[ind].cap -= x;
        e[ind ^ 1].cap += x;
        return x;
      }
    }
  }
  return 0;
}
int n, m;
class Main {
 public:
  void solve(std::istream &in, std::ostream &out) {
    ans = 0;
    e.clear();
    for (int i = 0; i < N; i++) {
      g[i].clear();
    }
    in >> n >> m;
    for (int i = 0; i < n; i++) {
      int l, r;
      in >> l >> r;
      l--, r--;
      if (l != -1) {
        add_edge(i, n + l, 1);
      }
      if (r != m) {
        add_edge(i, n + m + r, 1);
      }
    }
    s = n + m + m + m, t = n + m + m + m + 1;
    for (int i = 0; i < n; i++) {
      add_edge(s, i, 1);
    }
    for (int i = 1; i < m; i++) {
      add_edge(n + i, n + i - 1, 1e9);
    }
    for (int i = 0; i < m - 1; i++) {
      add_edge(n + m + i, n + m + i + 1, 1e9);
    }
    for (int i = 0; i < m; i++) {
      add_edge(n + i, n + m + m + i, 1);
      add_edge(n + m + i, n + m + m + i, 1);
    }
    for (int i = 0; i < m; i++) {
      add_edge(n + m + m + i, t, 1);
    }
    while (bfs()) {
      after_bfs++;
      while (dfs()) {
        continue;
      }
    }
    out << n - ans << '\n';
  }
};
int main() {
  ios::sync_with_stdio(0);
  Main solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}
