#include <bits/stdc++.h>
using namespace std;
const int Size = 1 << 16;
char buffer[Size], *head, *tail;
inline char Getchar() {
  if (head == tail) {
    int l = fread(buffer, 1, Size, stdin);
    tail = (head = buffer) + l;
  }
  if (head == tail) return -1;
  return *head++;
}
inline int read() {
  int x = 0, f = 1;
  char c = Getchar();
  for (; !isdigit(c); c = Getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = Getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int maxn = 800010;
const int inf = 2147483647;
const int maxm = 4000010;
struct Dinic {
  int n, m, s, t;
  int first[maxn], next[maxm];
  struct Edge {
    int u, v, f;
  } edges[maxm];
  void init(int n) {
    this->n = n;
    m = 0;
    memset(first, -1, sizeof(first));
  }
  void AddEdge(int u, int v, int f) {
    edges[m] = (Edge){u, v, f};
    next[m] = first[u];
    first[u] = m++;
    edges[m] = (Edge){v, u, 0};
    next[m] = first[v];
    first[v] = m++;
  }
  int vis[maxn], d[maxn], clo;
  int Q[maxn], cur[maxn];
  int BFS() {
    int l = 1, r = 0;
    vis[s] = ++clo;
    Q[++r] = s;
    while (l <= r) {
      int x = Q[l++];
      for (int i = first[x]; i != -1; i = next[i]) {
        Edge& e = edges[i];
        if (e.f && vis[e.v] != clo) {
          vis[e.v] = clo;
          d[e.v] = d[x] + 1;
          Q[++r] = e.v;
        }
      }
    }
    return vis[t] == clo;
  }
  int DFS(int x, int a) {
    if (x == t || !a) return a;
    int flow = 0, f;
    for (int& i = cur[x]; i != -1; i = next[i]) {
      Edge& e = edges[i];
      if (e.f && d[e.v] == d[x] + 1 && (f = DFS(e.v, min(a, e.f)))) {
        e.f -= f;
        edges[i ^ 1].f += f;
        flow += f;
        a -= f;
        if (!a) break;
      }
    }
    return flow;
  }
  int solve(int s, int t) {
    this->s = s;
    this->t = t;
    int ans = 0;
    int f;
    while (BFS()) {
      for (int i = (1), i_end_ = (n); i <= i_end_; i++) cur[i] = first[i];
      while (f = DFS(s, inf)) ans += f;
    }
    return ans;
  }
} sol;
int n, m;
int main() {
  n = read();
  m = read();
  sol.init(3 * m + n + 2);
  int S = 3 * m + n + 1, T = S + 1;
  for (int i = (1), i_end_ = (m); i <= i_end_; i++) sol.AddEdge(i, T, 1);
  for (int i = (1), i_end_ = (m); i <= i_end_; i++) {
    sol.AddEdge(i + m, i, inf);
    if (i != 1) sol.AddEdge(i + m, i + m - 1, inf);
  }
  for (int i = (1), i_end_ = (m); i <= i_end_; i++) {
    sol.AddEdge(i + m * 2, i, inf);
    if (i != m) sol.AddEdge(i + m * 2, i + m * 2 + 1, inf);
  }
  for (int i = (1), i_end_ = (n); i <= i_end_; i++) {
    int l = read(), r = read(), c = 3 * m + i;
    sol.AddEdge(S, c, 1);
    if (l) sol.AddEdge(c, l + m, inf);
    if (r != m + 1) sol.AddEdge(c, r + m * 2, inf);
  }
  printf("%d\n", n - sol.solve(S, T));
  return 0;
}
