#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;
template <typename T, bool directed>
struct Dinic {
  struct edge {
    int to;
    T cap;
    int rev;
    edge() {}
    edge(int to_, T cap_, int rev_) : to(to_), cap(cap_), rev(rev_) {}
  };
  T inf = numeric_limits<T>::max();
  vector<vector<edge>> G;
  vector<int> level, iter;
  Dinic(int n) : G(n), level(n), iter(n) {}
  void addEdge(int from, int to, T cap) {
    G[from].emplace_back(to, cap, G[to].size());
    G[to].emplace_back(from, directed ? 0 : cap, G[from].size() - 1);
  }
  void bfs(int s) {
    fill(level.begin(), level.end(), -1);
    queue<int> que;
    level[s] = 0;
    que.emplace(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (edge &e : G[v]) {
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          que.emplace(e.to);
        }
      }
    }
  }
  T dfs(int v, int t, T f) {
    if (v == t) return f;
    for (int i = iter[v]; i < int(G[v].size()); ++i) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[v] < level[e.to]) {
        T d = dfs(e.to, t, min(f, e.cap));
        if (d == 0) continue;
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
    return 0;
  }
  T maxFlow(int s, int t, T lim) {
    T fl = 0;
    while (true) {
      bfs(s);
      if (level[t] < 0 || lim == 0) break;
      fill(iter.begin(), iter.end(), 0);
      while (true) {
        T f = dfs(s, t, lim);
        if (f == 0) break;
        fl += f;
        lim -= f;
      }
    }
    return fl;
  }
  T maxFlow(int s, int t) { return maxFlow(s, t, inf); }
};
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  Dinic<int, true> dinic(3 * m + n + 2);
  int s = 3 * m + n;
  int t = 3 * m + n + 1;
  for (int i = 0; i < m; ++i) {
    dinic.addEdge(i, t, 1);
    dinic.addEdge(m + i, i, 1);
    dinic.addEdge(2 * m + i, i, 1);
    if (i != 0) dinic.addEdge(m + i, m + i - 1, 1000000);
    if (i != m - 1) dinic.addEdge(2 * m + i, 2 * m + i + 1, 1000000);
  }
  for (int i = 0; i < n; ++i) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    dinic.addEdge(s, 3 * m + i, 1);
    if (-1 < l) dinic.addEdge(3 * m + i, m + l, 1);
    if (r < m) dinic.addEdge(3 * m + i, 2 * m + r, 1);
  }
  int sit = dinic.maxFlow(s, t);
  cout << n - sit << "\n";
  return 0;
}
