#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1.01e9;
struct Edge {
  int from, to, cap, flow;
};
int q[(int)1e7], qL, qR;
struct Graph {
  int n;
  vector<Edge> edges;
  vector<vector<int> > e;
  vector<int> d, c;
  Graph(int N) {
    n = N;
    e.resize(n);
  }
  void addEdge(int from, int to, int cap) {
    e[from].push_back(edges.size());
    edges.push_back(Edge({from, to, cap, 0}));
    e[to].push_back(edges.size());
    edges.push_back(Edge({to, from, 0, 0}));
  }
  bool bfs() {
    c.assign(n, 0);
    d.assign(n, INF);
    qL = qR = 0;
    q[qR++] = 0;
    d[0] = 0;
    while (qL < qR) {
      int v = q[qL++];
      for (int i = 0; i < (int)e[v].size(); i++) {
        Edge cur = edges[e[v][i]];
        if (d[cur.to] > d[v] + 1 && cur.flow < cur.cap) {
          d[cur.to] = d[v] + 1;
          q[qR++] = cur.to;
        }
      }
    }
    return d[n - 1] != INF;
  }
  int dfs(int v, int flow) {
    if (v == n - 1) return flow;
    if (flow == 0) return 0;
    for (int &i = c[v]; i < (int)e[v].size(); i++) {
      Edge cur = edges[e[v][i]];
      if (d[cur.to] != d[v] + 1) continue;
      int pushed = dfs(cur.to, min(flow, cur.cap - cur.flow));
      if (pushed) {
        edges[e[v][i]].flow += pushed;
        edges[e[v][i] ^ 1].flow -= pushed;
        return pushed;
      }
    }
    return 0;
  }
  int flow() {
    int res = 0;
    while (bfs()) {
      while (int pushed = dfs(0, INF)) {
        res += pushed;
      }
    }
    return res;
  }
};
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m) == 2) {
    vector<int> l(n), r(n);
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &l[i], &r[i]);
      l[i]--;
      r[i]--;
    }
    Graph gr(1 + n + m + m + m + 1);
    for (int i = 0; i < n; i++) {
      gr.addEdge(0, 1 + i, 1);
      if (l[i] >= 0) gr.addEdge(1 + i, 1 + n + m + l[i], 1);
      if (r[i] < m) gr.addEdge(1 + i, 1 + n + m + m + r[i], 1);
    }
    for (int i = 0; i < m; i++) {
      gr.addEdge(1 + n + m + i, 1 + n + i, 1);
      if (i) gr.addEdge(1 + n + m + i, 1 + n + m + i - 1, INF);
      gr.addEdge(1 + n + m + m + i, 1 + n + i, 1);
      if (i < m - 1) gr.addEdge(1 + n + m + m + i, 1 + n + m + m + i + 1, INF);
      gr.addEdge(1 + n + i, 1 + n + m + m + m, 1);
    }
    int fl = gr.flow();
    cout << n - fl << endl;
  }
  return 0;
}
