#include <bits/stdc++.h>
using namespace std;
const int MAX_V = 5010;
long long INF = 100000000000;
struct edge {
  long long to, cap, rev;
};
vector<edge> G[MAX_V];
bool used[MAX_V];
long long b[MAX_V], f[MAX_V], can[MAX_V][MAX_V];
vector<int> v, w;
void add_edge(long long from, long long to, long long cap) {
  G[from].push_back({to, cap, (long long)G[to].size()});
  G[to].push_back({from, 0, (long long)G[from].size() - 1});
}
long long dfs(int s, int t, long long val) {
  for (edge e : G[s]) {
    if (used[e.to]) continue;
    b[e.to] += e.cap;
    if (b[e.to] >= val) {
      used[e.to] = true;
      v.push_back(e.to);
      w.push_back(e.to);
    }
  }
  if (v.size() == 0) return 0;
  int vv = v.back();
  if (vv == t) return 1;
  v.pop_back();
  return dfs(vv, t, val);
}
long long rev_dfs(int s, int t, long long val) {
  for (int i = 0; i < MAX_V; i++) f[i] = 0;
  f[t] = val;
  while (w.size()) {
    int now = w.back();
    w.pop_back();
    used[now] = false;
    for (edge ee : G[now]) {
      int from = ee.to;
      if (!used[from]) continue;
      int j = can[from][now];
      if (j == -1) continue;
      edge &e = G[from][j];
      long long f_ = min(f[now], e.cap);
      e.cap -= f_;
      G[e.to][e.rev].cap += f_;
      f[now] -= f_;
      f[from] += f_;
    }
  }
}
long long max_flow(int s, int t) {
  long long flow = 0, val = 0;
  int i, j;
  for (i = 0; i < MAX_V; i++) {
    b[i] = 0;
    for (j = 0; j < MAX_V; j++) {
      can[i][j] = -1;
    }
    for (j = 0; j < G[i].size(); j++) {
      can[i][G[i][j].to] = j;
      val = max(val, G[i][j].cap);
    }
  }
  while (val) {
    v.clear();
    w.clear();
    w.push_back(s);
    for (i = 0; i < MAX_V; i++) {
      b[i] = 0;
      used[i] = false;
    }
    used[s] = true;
    if (!dfs(s, t, val)) {
      val /= 2;
      continue;
    }
    rev_dfs(s, t, val);
    flow += val;
  }
  return flow;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int s = n + 3 * m, t = s + 1;
  for (int i = 0; i < n; ++i) {
    int l, r;
    cin >> l >> r;
    --r;
    add_edge(s, i, 1);
    if (l) {
      add_edge(i, n + (l - 1), 1);
    }
    if (r < m) {
      add_edge(i, n + m + r, 1);
    }
  }
  for (int j = 0; j < m; ++j) {
    add_edge(n + j, n + 2 * m + j, 1);
    add_edge(n + m + j, n + 2 * m + j, 1);
    add_edge(n + 2 * m + j, t, 1);
    if (j) {
      add_edge(n + j, n + (j - 1), INF);
    }
    if (j < m - 1) {
      add_edge(n + m + j, n + m + (j + 1), INF);
    }
  }
  cout << n - max_flow(s, t) << endl;
}
