#include <bits/stdc++.h>
using namespace std;
int MOD = 1e9 + 7;
int INF = 1e9;
struct edge {
  int from, to, cap;
  edge *rev;
  edge(int u, int v, int c) : from(u), to(v), cap(c), rev(NULL) {}
};
int n, s, t;
int dist[1111111], vis[1111111];
vector<edge *> g[1111111];
void addedge(int u, int v, int c) {
  edge *in = new edge(u, v, c);
  edge *out = new edge(v, u, 0);
  in->rev = out;
  out->rev = in;
  g[u].push_back(in);
  g[v].push_back(out);
}
int dfs(int u, int fo) {
  if (u == t) return fo;
  for (; vis[u] < g[u].size(); vis[u]++) {
    edge *e = g[u][vis[u]];
    int v = e->to;
    if (dist[v] != dist[u] + 1 || e->cap <= 0) continue;
    int foo = dfs(v, min(fo, e->cap));
    if (foo > 0) {
      e->cap -= foo;
      e->rev->cap += foo;
      return foo;
    }
  }
  return 0;
}
int maxflow() {
  int mxf = 0;
  for (;;) {
    for (int i = 0; i < n; i++) dist[i] = INF, vis[i] = 0;
    queue<int> q;
    q.push(s);
    dist[s] = 0;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      if (u == t) break;
      for (edge *e : g[u]) {
        int v = e->to;
        if (e->cap > 0 && dist[v] == INF) {
          dist[v] = dist[u] + 1;
          q.push(v);
        }
      }
    }
    if (dist[t] == INF) break;
    while (int fo = dfs(s, INF)) mxf += fo;
  }
  return mxf;
}
int N, M, l[200005], r[200005];
int solve() {
  cin >> N >> M;
  n = N + M + M * 2 + 2;
  s = N + M + M * 2, t = N + M + M * 2 + 1;
  for (int i = 0; i + 1 < M; i++) addedge(N + M + i + 1, N + M + i, INF);
  for (int i = 0; i + 1 < M; i++)
    addedge(N + M * 2 + i, N + M * 2 + i + 1, INF);
  for (int i = 0; i < N; i++) addedge(s, i, 1);
  for (int i = 0; i < N; i++) {
    cin >> l[i] >> r[i];
    if (l[i] > 0) l[i]--, addedge(i, N + M + l[i], 1);
    if (r[i] < M + 1) r[i]--, addedge(i, N + M * 2 + r[i], 1);
  }
  for (int i = 0; i < M; i++) addedge(N + M + i, N + i, 1);
  for (int i = 0; i < M; i++) addedge(N + M * 2 + i, N + i, 1);
  for (int i = 0; i < M; i++) addedge(N + i, t, 1);
  cout << N - maxflow() << endl;
  return 0;
}
int main() {
  ios::sync_with_stdio(0);
  solve();
  return 0;
}
