#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005 * 4, inf = 0x3f3f3f3f;
struct Edge {
  int next, to, cap, flow;
  Edge(int a = 0, int b = 0, int c = 0) : next(a), to(b), cap(c), flow(0) {}
};
vector<Edge> e(2);
int head[maxn];
void Add(int u, int v, int c) {
  e.push_back(Edge(head[u], v, c));
  head[u] = e.size() - 1;
  e.push_back(Edge(head[v], u, 0));
  head[v] = e.size() - 1;
}
int N, M;
int p_, src, sink;
int cur[maxn], d[maxn], vis[maxn], T;
queue<int> q;
bool bfs() {
  q.push(src);
  vis[src] = ++T;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = head[x]; i; i = e[i].next) {
      int to = e[i].to;
      if (vis[to] != T && e[i].cap > e[i].flow) {
        vis[to] = T;
        d[to] = d[x] + 1;
        q.push(to);
      }
    }
  }
  return vis[sink] == T;
}
int dfs(int x, int a) {
  if (x == sink || !a) return a;
  int flow = 0, f;
  for (int &i = cur[x]; i; i = e[i].next) {
    int to = e[i].to;
    if (d[to] == d[x] + 1 && (f = dfs(to, min(a, e[i].cap - e[i].flow))) > 0) {
      e[i].flow += f;
      e[i ^ 1].flow -= f;
      flow += f;
      a -= f;
      if (!a) break;
    }
  }
  return flow;
}
int dinic() {
  int flow = 0;
  while (bfs()) {
    memcpy(cur, head, sizeof cur);
    flow += dfs(src, inf);
  }
  return flow;
}
inline int code(int i, int j) { return M * (j - 1) + i; }
void read(int &x) {
  x = 0;
  char ch;
  while (ch = getchar(), !isdigit(ch))
    ;
  x = ch - '0';
  while (ch = getchar(), isdigit(ch)) x = x * 10 + ch - '0';
}
int main() {
  read(N), read(M);
  src = M * 3 + 1, sink = src + 1, p_ = sink;
  for (int i = 1; i <= M; ++i) {
    if (i > 1) Add(code(i, 1), code(i - 1, 1), inf);
    if (i < M) Add(code(i, 2), code(i + 1, 2), inf);
    Add(code(i, 1), code(i, 3), 1);
    Add(code(i, 2), code(i, 3), 1);
    Add(code(i, 3), sink, 1);
  }
  for (int i = 1, l, r; i <= N; ++i) {
    read(l), read(r);
    ++p_;
    Add(src, p_, 1);
    if (l >= 1) Add(p_, code(l, 1), 1);
    if (r <= M) Add(p_, code(r, 2), 1);
  }
  printf("%d\n", N - dinic());
  return 0;
}
