#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e6 + 10;
const int M = (int)2e7 + 10;
const int INF = (int)0x3f3f3f3f;
struct Edge {
  int u, v, c;
  Edge(){};
  Edge(int u, int v, int c) : u(u), v(v), c(c){};
};
struct Flow {
  int n, m, s, t;
  Edge E[M];
  int head[N], next[M];
  void Init(int __n, int __s, int __t) {
    n = __n;
    m = 0;
    s = __s;
    t = __t;
    for (int i = 1; i <= n; ++i) head[i] = -1;
  };
  void AddEdge(int u, int v, int c) {
    E[m] = Edge(u, v, c);
    next[m] = head[u];
    head[u] = m++;
    E[m] = Edge(v, u, 0);
    next[m] = head[v];
    head[v] = m++;
  }
  int d[N], prev[N];
  bool BFS() {
    queue<int> Q;
    Q.push(s);
    for (int i = 1; i <= n; ++i) d[i] = -1;
    d[s] = 0;
    while (Q.size()) {
      int u = Q.front();
      Q.pop();
      for (int i = head[u]; i != -1; i = next[i]) {
        int v = E[i].v, c = E[i].c;
        if (d[v] == -1 && c > 0) {
          d[v] = d[u] + 1;
          Q.push(v);
        }
      }
    }
    return d[t] != -1;
  }
  int DFS(int u, int fl) {
    if (u == t) return fl;
    for (int &i = prev[u]; i != -1; i = next[i]) {
      int v = E[i].v, c = E[i].c;
      if (d[v] == d[u] + 1 && c > 0) {
        int x = DFS(v, min(fl, c));
        if (x) {
          E[i ^ 0].c -= x;
          E[i ^ 1].c += x;
          return x;
        }
      }
    }
    return 0;
  }
  int MaxFlow() {
    int ans = 0;
    while (BFS()) {
      for (int i = 1; i <= n; ++i) prev[i] = head[i];
      while (1) {
        int x = DFS(s, INF);
        if (x)
          ans += x;
        else
          break;
      }
    }
    return ans;
  }
} Flow;
int n, m;
int _s;
int _t;
void Add(int x, int k, int l, int r, int i, int j) {
  assert(k <= 2 * m);
  if (l > j || r < i) return;
  if (i <= l && r <= j) {
    Flow.AddEdge(x, k + n, 1);
    return;
  }
  int mid = (l + r) >> 1;
  Add(x, k << 1 | 0, l, mid, i, j);
  Add(x, k << 1 | 1, mid + 1, r, i, j);
}
void Build(int k, int l, int r) {
  assert(k <= 2 * m);
  if (l == r) {
    Flow.AddEdge(k + n, _t, 1);
    return;
  }
  int mid = (l + r) >> 1;
  Flow.AddEdge(k + n, k * 2 + 0 + n, INF);
  Flow.AddEdge(k + n, k * 2 + 1 + n, INF);
  Build(k << 1 | 0, l, mid);
  Build(k << 1 | 1, mid + 1, r);
}
void Init() {
  for (int i = 1; i <= n; ++i) Flow.AddEdge(_s, i, 1);
  Build(1, 1, m);
}
void LUL(int &m) {
  int k = 0;
  while (1 << k < m) k++;
  m = 1 << k;
}
int main() {
  scanf("%d%d", &n, &m);
  int f = m;
  LUL(m);
  _s = n + 2 * m + 1;
  _t = n + 2 * m + 2;
  Flow.Init(n + 2 * m + 2, _s, _t);
  for (int i = 1; i <= n; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    if (1 <= l) Add(i, 1, 1, m, 1, l);
    if (r <= f) Add(i, 1, 1, m, r, f);
  }
  Init();
  printf("%d", n - Flow.MaxFlow());
  return 0;
}
