#include <bits/stdc++.h>
using namespace std;
const int S = (1 << 20) + 5;
char buf[S], *H, *T;
inline char Get() {
  if (H == T) T = (H = buf) + fread(buf, 1, S, stdin);
  if (H == T) return -1;
  return *H++;
}
inline int read() {
  int x = 0;
  char c = Get();
  while (!isdigit(c)) c = Get();
  while (isdigit(c)) x = x * 10 + c - '0', c = Get();
  return x;
}
const int N = 200010;
struct Edge {
  int to, capa, flow, next;
} e[N << 4];
int h[N << 2], sum = -1, s, t, tot;
bool vis[N << 2];
int cur[N << 2];
int d[N << 2];
void AddEdge(int u, int v, int w) {
  e[++sum].to = v;
  e[sum].capa = w;
  e[sum].flow = 0;
  e[sum].next = h[u];
  h[u] = sum;
}
void add_edge(int u, int v, int w) {
  AddEdge(u, v, w);
  AddEdge(v, u, 0);
}
bool BFS() {
  memset(vis, 0, sizeof(vis));
  queue<int> q;
  q.push(s);
  vis[s] = 1;
  d[s] = 0;
  while (!q.empty()) {
    int u = q.front();
    for (int tmp = h[u]; ~tmp; tmp = e[tmp].next)
      if (!vis[e[tmp].to] && e[tmp].capa > e[tmp].flow) {
        vis[e[tmp].to] = 1;
        d[e[tmp].to] = d[u] + 1;
        q.push(e[tmp].to);
      }
    q.pop();
  }
  return vis[t];
}
int DFS(int u, int a) {
  if (u == t || a == 0) return a;
  int flow = 0, f;
  for (int& tmp = cur[u]; ~tmp; tmp = e[tmp].next)
    if (d[e[tmp].to] == d[u] + 1) {
      f = DFS(e[tmp].to, min(a, e[tmp].capa - e[tmp].flow));
      if (f > 0) {
        flow += f;
        a -= f;
        e[tmp].flow += f;
        e[tmp ^ 1].flow -= f;
        if (a == 0) break;
      }
    }
  return flow;
}
int Dinic() {
  int flow = 0;
  while (BFS()) {
    for (int i = 0; i <= tot; i++) cur[i] = h[i];
    flow += DFS(s, 0x7fffffff);
  }
  return flow;
}
int main() {
  memset(h, -1, sizeof(h));
  int n = read(), m = read(), l, r;
  s = 0;
  t = tot = 3 * n + m + 10;
  for (int i = 1; i <= n; i++) add_edge(s, i, 1);
  for (int i = 1; i <= m; i++) {
    add_edge(n + i, t, 1);
    add_edge(n + m + i, n + i, 1);
    add_edge(n + 2 * m + i, n + i, 1);
  }
  for (int i = 2; i <= m; i++) add_edge(n + m + i, n + m + i - 1, 0x7fffffff);
  for (int i = 1; i < m; i++)
    add_edge(n + 2 * m + i, n + 2 * m + i + 1, 0x7fffffff);
  for (int i = 1; i <= n; i++) {
    l = read();
    r = read();
    if (l >= 1) add_edge(i, n + m + l, 1);
    if (r <= m) add_edge(i, n + 2 * m + r, 1);
  }
  printf("%d\n", n - Dinic());
  return 0;
}
