#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, ch = getchar(), f = 1;
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
const int N = 800005, M = 5000005, INF = 0x3f3f3f3f;
struct Edge {
  int to, c, nxt;
};
namespace Network {
int m, s, t, fr[N], vis[N], cur[N], d[N];
Edge E[M];
void init() {
  m = 0;
  memset(fr, -1, sizeof fr);
}
inline void addedge(int x, int y, int c) {
  E[m++] = (Edge){y, c, fr[x]};
  fr[x] = m - 1;
  E[m++] = (Edge){x, 0, fr[y]};
  fr[y] = m - 1;
}
int bfs() {
  queue<int> q;
  memset(vis, 0, sizeof vis);
  memcpy(cur, fr, sizeof cur);
  d[s] = 0;
  vis[s] = 1;
  q.push(s);
  while (!q.empty()) {
    int h = q.front();
    q.pop();
    for (int i = fr[h]; i != -1; i = E[i].nxt)
      if (E[i].c && !vis[E[i].to]) {
        vis[E[i].to] = 1;
        d[E[i].to] = d[h] + 1;
        q.push(E[i].to);
      }
  }
  return vis[t];
}
int dfs(int x, int a) {
  if (x == t || a == 0) return a;
  int fl = 0, f;
  for (int &i = cur[x]; i != -1; i = E[i].nxt)
    if (d[E[i].to] == d[x] + 1 && (f = dfs(E[i].to, min(a, E[i].c))) > 0) {
      E[i].c -= f;
      E[i ^ 1].c += f;
      fl += f;
      a -= f;
      if (!a) break;
    }
  return fl;
}
int maxflow(int _s, int _t) {
  s = _s;
  t = _t;
  int fl = 0;
  while (bfs()) fl += dfs(s, INF);
  return fl;
}
}  // namespace Network
int idx, id[N], n, m;
void build(int x, int l, int r) {
  id[x] = ++idx;
  if (l == r) {
    Network::addedge(id[x], n + l, 1);
    return;
  }
  int mid = l + r >> 1;
  build((x << 1), l, mid);
  build((x << 1 | 1), mid + 1, r);
  Network::addedge(id[x], id[(x << 1)], INF);
  Network::addedge(id[x], id[(x << 1 | 1)], INF);
}
void link(int x, int l, int r, int ql, int qr, int p) {
  if (ql <= l && r <= qr) {
    Network::addedge(p, id[x], 1);
    return;
  }
  int mid = l + r >> 1;
  if (ql <= mid) link((x << 1), l, mid, ql, qr, p);
  if (qr > mid) link((x << 1 | 1), mid + 1, r, ql, qr, p);
}
int main() {
  n = read(), m = read();
  Network::init();
  idx = n + m;
  build(1, 1, m);
  for (int i = 1; i <= n; ++i) {
    int l = read(), r = read();
    if (l > 0) link(1, 1, m, 1, l, i);
    if (r <= m) link(1, 1, m, r, m, i);
    Network::addedge(0, i, 1);
  }
  int T = ++idx;
  for (int i = 1; i <= m; ++i) Network::addedge(n + i, T, 1);
  int f = Network::maxflow(0, T);
  printf("%d\n", n - f);
  return 0;
}
