#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
int n, m;
int l[MAXN];
int r[MAXN];
bool read() {
  if (scanf("%d%d", &n, &m) < 2) {
    return 0;
  }
  for (int i = 0; i < int(n); ++i) {
    scanf("%d%d", &l[i], &r[i]);
  }
  return 1;
}
const int INF = 1e9;
struct Edge {
  int v, to;
  int cap;
  int flow;
};
struct Graph {
  vector<vector<int> > g;
  vector<Edge> es;
  int n;
  vector<int> ptr;
  vector<int> dist;
  Graph(int n = 0) : n(n) { g.resize(n); }
  void addE(int v, int to, int cap) {
    g[v].push_back(((int)(es).size()));
    es.push_back(Edge{v, to, cap, 0});
    g[to].push_back(((int)(es).size()));
    es.push_back(Edge{to, v, 0, 0});
  }
  bool bfs() {
    dist.assign(n, INF);
    dist[0] = 0;
    vector<int> q;
    q.push_back(0);
    for (int it = 0; it < int(((int)(q).size())); ++it) {
      int v = q[it];
      for (int id : g[v]) {
        if (es[id].cap > es[id].flow && dist[es[id].to] > dist[v] + 1) {
          q.push_back(es[id].to);
          dist[es[id].to] = dist[v] + 1;
        }
      }
    }
    if (dist[n - 1] == INF) {
      return 0;
    }
    return 1;
  }
  int dfs(int v, int f) {
    if (!f) {
      return 0;
    }
    if (v == n - 1) {
      return f;
    }
    int ans = 0;
    for (int& i = ptr[v]; i < ((int)(g[v]).size()); ++i) {
      int id = g[v][i];
      if (dist[es[id].to] != dist[v] + 1) {
        continue;
      }
      int cur = dfs(es[id].to, min(es[id].cap - es[id].flow, f));
      ans += cur;
      f -= cur;
      es[id].flow += cur;
      es[id ^ 1].flow -= cur;
    }
    return ans;
  }
  int max_flow() {
    int f = 0;
    while (1) {
      if (!bfs()) {
        break;
      }
      ptr.assign(n, 0);
      while (int cur = dfs(0, INF)) {
        f += cur;
      }
    }
    return f;
  }
};
int solve() {
  Graph g(1 + n + m + m + m + 1);
  for (int i = 0; i < int(n); ++i) {
    g.addE(0, 1 + i, 1);
    if (l[i] > 0) {
      g.addE(1 + i, 1 + n + (l[i] - 1), 1);
    }
    if (r[i] <= m) {
      g.addE(1 + i, 1 + n + m + (r[i] - 1), 1);
    }
  }
  for (int i = 0; i < int(m); ++i) {
    if (i > 0) {
      g.addE(1 + n + i, 1 + n + i - 1, INF);
    }
    if (i + 1 < m) {
      g.addE(1 + n + m + i, 1 + n + m + i + 1, INF);
    }
    g.addE(1 + n + i, 1 + n + m + m + i, INF);
    g.addE(1 + n + m + i, 1 + n + m + m + i, INF);
    g.addE(1 + n + m + m + i, 1 + n + m + m + m, 1);
  }
  return n - g.max_flow();
}
int main() {
  while (read()) {
    cout << solve() << endl;
  }
  return 0;
}
