#include <bits/stdc++.h>
using namespace std;
struct max_flow {
  struct edge {
    int to, cap, rev;
  };
  int V;
  vector<vector<edge>> G;
  vector<int> itr, level;
  max_flow(int V) : V(V) { G.assign(V, vector<edge>()); }
  void add_edge(int from, int to, int cap) {
    G[from].push_back((edge){to, cap, (int)G[to].size()});
    G[to].push_back((edge){from, 0, (int)G[from].size() - 1});
  }
  void bfs(int s) {
    level.assign(V, -1);
    queue<int> q;
    level[s] = 0;
    q.push(s);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (auto& e : G[v]) {
        if (e.cap > 0 and level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          q.push(e.to);
        }
      }
    }
  }
  int dfs(int v, int t, int f) {
    if (v == t) return f;
    for (int& i = itr[v]; i < (int)G[v].size(); ++i) {
      edge& e = G[v][i];
      if (e.cap > 0 and level[v] < level[e.to]) {
        int d = dfs(e.to, t, min(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          G[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }
  int run(int s, int t) {
    int ret = 0, f;
    while (bfs(s), level[t] >= 0) {
      itr.assign(V, 0);
      while ((f = dfs(s, t, (1e9))) > 0) ret += f;
    }
    return ret;
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  const int full = 200005;
  max_flow graph(full * 4 + 100);
  for (int i = 1; i <= n; i++) {
    graph.add_edge(0, i, 1);
  }
  for (int i = 1; i < m; i++) {
    graph.add_edge(full + i, full + i + 1, (1e9));
  }
  for (int i = m; i >= 2; i--) {
    graph.add_edge(2 * full + i, 2 * full + i - 1, (1e9));
  }
  for (int i = 1; i <= m; i++) {
    graph.add_edge(full + i, 3 * full + i, 1);
    graph.add_edge(2 * full + i, 3 * full + i, 1);
    graph.add_edge(3 * full + i, 4 * full + 1, 1);
  }
  for (int i = 1; i <= n; i++) {
    int l, r;
    cin >> l >> r;
    if (l > 0) {
      graph.add_edge(i, 2 * full + l, 1);
    }
    if (r <= m) {
      graph.add_edge(i, full + r, 1);
    }
  }
  cout << n - graph.run(0, 4 * full + 1) << endl;
  return 0;
}
