#include <bits/stdc++.h>
using namespace std;
template <class T>
struct dinic {
  struct edge {
    int to;
    T cap;
  };
  static constexpr T inf = numeric_limits<T>::max();
  vector<edge> es;
  vector<vector<int>> g;
  vector<int> dist, i;
  dinic(int n) : g(n), dist(n), i(n) {}
  void add_edge(int from, int to, T cap) {
    assert(from != to);
    assert(cap >= 0);
    if (cap == 0) return;
    g[from].push_back(es.size()), es.push_back({to, cap});
    g[to].push_back(es.size()), es.push_back({from, 0});
  }
  void bfs(int s) {
    fill(begin(dist), end(dist), -1);
    queue<int> que;
    dist[s] = 0;
    que.push(s);
    while (not que.empty()) {
      int v = que.front();
      que.pop();
      for (int id : g[v]) {
        auto e = es[id];
        if (e.cap == 0 or dist[e.to] != -1) continue;
        dist[e.to] = dist[v] + 1;
        que.push(e.to);
      }
    }
  }
  T dfs(int v, int s, T f) {
    if (v == s) return f;
    for (; i[v] < (int)g[v].size(); ++i[v]) {
      edge &e = es[g[v][i[v]]], &re = es[g[v][i[v]] ^ 1];
      if (dist[e.to] >= dist[v] or re.cap == 0) continue;
      T d = dfs(e.to, s, min(f, re.cap));
      if (d > 0) {
        re.cap -= d;
        e.cap += d;
        return d;
      }
    }
    return 0;
  }
  T max_flow(int s, int t) {
    assert(s != t);
    T res = 0;
    while (true) {
      bfs(s);
      if (dist[t] == -1) return res;
      fill(begin(i), end(i), 0);
      while (true) {
        T f = dfs(t, s, inf);
        if (f == 0) break;
        res += f;
      }
    }
  }
};
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int s = n + 3 * m, t = s + 1;
  dinic<int> d(t + 1);
  for (int i = 0; i < n; ++i) {
    int l, r;
    cin >> l >> r;
    --r;
    d.add_edge(s, i, 1);
    if (l) {
      d.add_edge(i, n + (l - 1), 1);
    }
    if (r < m) {
      d.add_edge(i, n + m + r, 1);
    }
  }
  for (int j = 0; j < m; ++j) {
    d.add_edge(n + j, n + 2 * m + j, 1);
    d.add_edge(n + m + j, n + 2 * m + j, 1);
    d.add_edge(n + 2 * m + j, t, 1);
    if (j) {
      d.add_edge(n + j, n + (j - 1), d.inf);
    }
    if (j < m - 1) {
      d.add_edge(n + m + j, n + m + (j + 1), d.inf);
    }
  }
  int res = n - d.max_flow(s, t);
  cout << res << '\n';
}
