#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int INF = 1e9;
int n, m;
int a[N][2];
inline int max(int x, int y) { return x > y ? x : y; }
inline int min(int x, int y) { return x < y ? x : y; }
namespace DINIC {
const int S = N * 4;
const int M = N * 10;
int flow_s, flow_t;
int dis[S], cur[S];
int h[S], tot;
struct Edge {
  int v, f, next;
} e[M * 2];
void init(int _flow_s, int _flow_t) {
  flow_s = _flow_s;
  flow_t = _flow_t;
  tot = 1;
}
void addEdge(int u, int v, int f) {
  e[++tot] = (Edge){v, f, h[u]};
  h[u] = tot;
  e[++tot] = (Edge){u, 0, h[v]};
  h[v] = tot;
}
queue<int> q;
bool bfs() {
  for (int i = 1; i <= flow_t; i++) dis[i] = -1, cur[i] = h[i];
  while (!q.empty()) q.pop();
  q.push(flow_s);
  dis[flow_s] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = h[u], v; i; i = e[i].next)
      if (e[i].f && dis[v = e[i].v] == -1) {
        dis[v] = dis[u] + 1;
        q.push(v);
      }
  }
  return dis[flow_t] != -1;
}
int dfs(int u, int flow) {
  if (u == flow_t) return flow;
  int get, res = 0;
  for (int i = cur[u], v; i && flow; i = e[i].next)
    if (e[i].f && dis[v = e[i].v] == dis[u] + 1) {
      get = dfs(v, min(flow, e[i].f));
      e[i].f -= get;
      e[i ^ 1].f += get;
      if (e[i].f) cur[u] = i;
      flow -= get;
      res += get;
    }
  if (!res) dis[u] = -1;
  return res;
}
int solve() {
  int res = 0;
  while (bfs()) res += dfs(flow_s, INF);
  return res;
}
}  // namespace DINIC
void readData() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i][0], &a[i][1]);
    a[i][0] = max(a[i][0], 1);
    a[i][1] = min(a[i][1], m);
  }
}
void buildGraph() {
  for (int i = 1; i <= m; i++) {
    if (i < m) {
      DINIC::addEdge(i, i + 1, m - i);
      DINIC::addEdge(m + i + 1, m + i, i);
    }
    DINIC::addEdge(i, 2 * m + i, 1);
    DINIC::addEdge(m + i, 2 * m + i, 1);
    DINIC::addEdge(2 * m + i, DINIC::flow_t, 1);
  }
  for (int i = 1; i <= n; i++) {
    DINIC::addEdge(DINIC::flow_s, 3 * m + i, 1);
    DINIC::addEdge(3 * m + i, m + a[i][0], 1);
    DINIC::addEdge(3 * m + i, a[i][1], 1);
  }
}
int main() {
  readData();
  DINIC::init(3 * m + n + 1, 3 * m + n + 2);
  buildGraph();
  int match = DINIC::solve();
  printf("%d\n", n - match);
  return 0;
}
