#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 edge {
  int to, cap, rev;
};
struct Dinic {
  int n, m;
  edge w[maxm];
  int fr[maxm];
  int num[maxn], cur[maxn], first[maxn];
  edge e[maxm];
  void init(int n) {
    this->n = n;
    m = 0;
  }
  void AddEdge(int from, int to, int cap) {
    w[++m] = (edge){to, cap};
    num[from]++, fr[m] = from;
    w[++m] = (edge){from, 0};
    num[to]++, fr[m] = to;
  }
  void prepare() {
    first[1] = 1;
    for (int i = 2; i <= n; i++) first[i] = first[i - 1] + num[i - 1];
    for (int i = 1; i < n; i++) num[i] = first[i + 1] - 1;
    num[n] = m;
    for (int i = 1; i <= m; i++) {
      e[first[fr[i]] + (cur[fr[i]]++)] = w[i];
      if (!(i % 2)) {
        e[first[fr[i]] + cur[fr[i]] - 1].rev =
            first[w[i].to] + cur[w[i].to] - 1;
        e[first[w[i].to] + cur[w[i].to] - 1].rev =
            first[fr[i]] + cur[fr[i]] - 1;
      }
    }
  }
  int q[maxn];
  int dist[maxn];
  int t;
  bool bfs(int s) {
    int l = 1, r = 1;
    q[1] = s;
    memset(dist, -1, (n + 1) * 4);
    dist[s] = 0;
    while (l <= r) {
      int u = q[l++];
      for (int i = first[u]; i <= num[u]; i++) {
        int v = e[i].to;
        if ((dist[v] != -1) || (!e[i].cap)) continue;
        dist[v] = dist[u] + 1;
        if (v == t) return true;
        q[++r] = v;
      }
    }
    return dist[t] != -1;
  }
  int dfs(int u, int flow) {
    if (u == t) return flow;
    for (int& i = cur[u]; i <= num[u]; i++) {
      int v = e[i].to;
      if (!e[i].cap || dist[v] != dist[u] + 1) continue;
      int t = dfs(v, min(flow, e[i].cap));
      if (t) {
        e[i].cap -= t;
        e[e[i].rev].cap += t;
        return t;
      }
    }
    return 0;
  }
  int solve(int s, int t) {
    int ans = 0;
    this->t = t;
    while (bfs(s)) {
      int flow;
      for (int i = 1; i <= n; i++) cur[i] = first[i];
      while (flow = dfs(s, inf)) ans += flow;
    }
    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);
  }
  sol.prepare();
  printf("%d\n", n - sol.solve(S, T));
  return 0;
}
