#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
double PI = 4 * atan(1);
map<int, int> grid[800002];
int N, M, par[800002];
bool visit[800002];
vector<int> todo;
void dfs(int node) {
  visit[node] = true;
  todo.push_back(node);
  for (auto v : grid[node]) {
    if (visit[N + 3 * M + 1]) return;
    if (!visit[v.first]) {
      par[v.first] = node;
      dfs(v.first);
    }
  }
}
int fordFulkerson() {
  int max_flow = 0;
  while (1) {
    dfs(0);
    if (!visit[N + 3 * M + 1]) break;
    for (int v = N + 3 * M + 1; v != 0; v = par[v]) {
      int u = par[v];
      grid[u][v]--;
      if (grid[u][v] == 0) grid[u].erase(v);
      grid[v][u]++;
    }
    max_flow++;
    for (int a : todo) visit[a] = 0;
    todo.clear();
  }
  return max_flow;
}
void init() {
  for (int i = 1; i < N + 1; i++) grid[0][i] = 1;
  for (int i = N + 1; i < N + M + 1; i++) {
    if (i != N + 1) grid[i][i - 1] = MOD;
    grid[i][i + 2 * M] = 1;
  }
  for (int i = N + M + 1; i < N + 2 * M + 1; i++) {
    if (i != 2 * M) grid[i][i + 1] = MOD;
    grid[i][i + M] = 1;
  }
  for (int i = N + 2 * M + 1; i < N + 3 * M + 1; i++)
    grid[i][N + 3 * M + 1] = 1;
}
int main() {
  cin >> N >> M;
  init();
  for (int i = 1; i < N + 1; i++) {
    int L, R;
    cin >> L >> R;
    if (L > 0) grid[0][L + N]++;
    if (R <= M) grid[0][N + M + R]++;
  }
  cout << N - fordFulkerson();
}
