#include <bits/stdc++.h>
using namespace std;
class FordFulkerson {
 public:
  struct Edge {
    int from, to, capacity, flow;
    int calcRest(bool isRev) { return isRev ? flow : capacity - flow; }
    int getNext(bool isRev) { return isRev ? from : to; }
  };
  bool visited[((2 * 100000) * 3 + 100)];
  Edge edgeInstances[((2 * 100000) * (60))];
  std::vector<Edge*> edges[((2 * 100000) * 3 + 100)][2];
  int E = 0;
  void addEdge(Edge edge) {
    edgeInstances[E] = edge;
    edges[edge.from][0].push_back(&edgeInstances[E]);
    edges[edge.to][1].push_back(&edgeInstances[E]);
    E++;
  }
  int calcMaxFlow(int s, int t) {
    int max_flow = 0;
    while (true) {
      std::memset(visited, 0, sizeof(visited));
      int flow = findFlow(s, t, ((2 * 100000) * 3 + 100));
      max_flow += flow;
      if (flow == 0) {
        break;
      }
    }
    return max_flow;
  }
  int findFlow(int current, int destination, int flow) {
    visited[current] = true;
    if (current == destination) return flow;
    for (int j = 0; j < 2; ++j) {
      bool revEdge = j == 1;
      std::vector<Edge*>& edgeList = edges[current][j];
      for (int i = 0; i < edgeList.size(); ++i) {
        Edge& edge = *edgeList[i];
        int rest = edge.calcRest(revEdge), next = edge.getNext(revEdge);
        if (visited[next]) continue;
        if (rest > 0) {
          int max_flow = findFlow(next, destination, std::min(rest, flow));
          edge.flow += revEdge ? -max_flow : max_flow;
          if (max_flow > 0) {
            return max_flow;
          }
        }
      }
    }
    return 0;
  }
};
FordFulkerson fordFulkerson;
int N, M;
int m = 1;
void constructSegment(int i, int pm) {
  if (i >= m - 1) return;
  fordFulkerson.addEdge({N + 1 + i, N + 1 + (i * 2 + 1), pm / 2});
  fordFulkerson.addEdge({N + 1 + i, N + 1 + (i * 2 + 2), pm / 2});
  constructSegment(i * 2 + 1, pm / 2);
  constructSegment(i * 2 + 2, pm / 2);
}
void constructEdges(int from, int a, int b, int k, int l, int r) {
  if (b <= l || a >= r) return;
  if (a <= l && b >= r) {
    fordFulkerson.addEdge({from, N + 1 + k, 1});
  } else {
    constructEdges(from, a, b, k * 2 + 1, l, (l + r) / 2);
    constructEdges(from, a, b, k * 2 + 2, (l + r) / 2, r);
  }
}
int main() {
  scanf("%d%d", &N, &M);
  while (m < M) m *= 2;
  for (int i = 0; i < N; ++i) {
    fordFulkerson.addEdge({0, i + 1, 1});
  }
  for (int i = 0; i < N; ++i) {
    int L, R;
    scanf("%d%d", &L, &R);
    constructEdges(i + 1, 0, L, 0, 0, m);
    constructEdges(i + 1, R - 1, M, 0, 0, m);
  }
  constructSegment(0, m);
  for (int i = 0; i < m; ++i) {
    fordFulkerson.addEdge({N + m + i + 1, N + 2 * m + 1, 1});
  }
  printf("%d", N - fordFulkerson.calcMaxFlow(0, 2 * m + N + 1) - 1);
  return 0;
}
