#include <bits/stdc++.h>
using namespace std;
int S, T;
namespace yjq {
int cnt = 1, head[600010], nxt[10000010], to[10000010], cap[10000010],
    d[600010], cur[600010], q[600010], l, r, i, he;
inline void add(int a, int b, int c) {
  to[++cnt] = b, cap[cnt] = c, nxt[cnt] = head[a], head[a] = cnt;
  to[++cnt] = a, cap[cnt] = 0, nxt[cnt] = head[b], head[b] = cnt;
}
inline bool bfs(int s, int t) {
  memset(d, -1, sizeof d), l = 0, r = -1;
  q[++r] = s, d[s] = 0;
  while (l <= r) {
    he = q[l++];
    for (i = head[he]; i; i = nxt[i])
      if (cap[i] && d[to[i]] < 0) d[to[i]] = d[he] + 1, q[++r] = to[i];
  }
  return d[t] >= 0;
}
inline int dfs(int t, int x, int a) {
  if (x == t || a == 0) return a;
  int ret = 0, f;
  for (int i = cur[x]; i; i = nxt[i])
    if (cap[i] > 0 && d[to[i]] == d[x] + 1) {
      f = dfs(t, to[i], min(a, cap[i]));
      if (!f)
        d[to[i]] = -1;
      else {
        cap[i] -= f, cap[i ^ 1] += f, ret += f, a -= f;
        if (!a) return d[x] = -1, ret;
      }
    }
  return ret;
}
inline int dinic(int s, int t) {
  int ret = 0;
  while (bfs(s, t)) {
    for (i = 0; i <= T; i++) cur[i] = head[i];
    ret += dfs(t, s, 0x3f3f3f3f);
  }
  return ret;
}
}  // namespace yjq
using yjq::add;
int n, m;
int main() {
  scanf("%d%d", &n, &m), T = n + 2 * m + 1;
  for (int i = 1; i <= n; i++) {
    add(S, i, 1);
    int l, r;
    scanf("%d%d", &l, &r);
    if (l) add(i, l + n, 1);
    if (r <= m) add(i, r + n + m, 1);
  }
  add(1 + n, T, 1);
  for (int i = 2; i <= m; i++) add(i + n, i - 1 + n, i - 1), add(i + n, T, 1);
  add(m + n + m, T, 1);
  for (int i = m - 1; i; i--)
    add(i + n + m, i + 1 + n + m, m - i), add(i + n + m, T, 1);
  printf("%d", n - yjq::dinic(S, T));
  return 0;
}
