#include <bits/stdc++.h>
struct Edge {
  Edge(int a, int b) {
    to = a;
    cap = b;
  }
  int to, cap;
};
class Dinic {
 public:
  Dinic(int n) {
    edges.resize(n);
    this->n = n;
  }
  int maxFlow(int src, int sink) {
    int ans = 0;
    while (bfs(src, sink)) {
      int flow;
      pt = std::vector<int>(n, 0);
      while (flow = dfs(src, sink)) ans += flow;
    }
    return ans;
  }
  void addEdge(int from, int to, int cap) {
    edges[from].push_back(list.size());
    list.push_back(Edge(to, cap));
    edges[to].push_back(list.size());
    list.push_back(Edge(from, 0));
  }

 private:
  int n;
  std::vector<std::vector<int> > edges;
  std::vector<Edge> list;
  std::vector<int> h, pt;
  int dfs(int on, int sink, int flow = 1e9) {
    if (flow == 0) return 0;
    if (on == sink) return flow;
    for (; pt[on] < edges[on].size(); pt[on]++) {
      int cur = edges[on][pt[on]];
      if (h[on] + 1 != h[list[cur].to]) continue;
      int got = dfs(list[cur].to, sink, std::min(flow, list[cur].cap));
      if (got) {
        list[cur].cap -= got;
        list[cur ^ 1].cap += got;
        return got;
      }
    }
    return 0;
  }
  bool bfs(int src, int sink) {
    h = std::vector<int>(n, n);
    h[src] = 0;
    std::queue<int> q;
    q.push(src);
    while (!q.empty()) {
      int on = q.front();
      q.pop();
      for (auto a : edges[on]) {
        if (list[a].cap == 0) continue;
        int to = list[a].to;
        if (h[to] > h[on] + 1) {
          h[to] = h[on] + 1;
          q.push(to);
        }
      }
    }
    return h[sink] < n;
  }
};
const int ms = 200200;
const int INF = 1e6;
bool got[ms];
int main() {
  int n, m;
  while (std::cin >> n >> m) {
    Dinic graph(m + m + m + n + 2);
    int source = m + m + m + n;
    int sink = source + 1;
    for (int i = 0; i - 1 < m; i++) {
      graph.addEdge(i + 1, i, INF);
      graph.addEdge(m + i, m + i + 1, INF);
    }
    for (int i = 0; i < m; i++) {
      graph.addEdge(i, m + m + i, INF);
      graph.addEdge(m + i, m + m + i, INF);
    }
    std::set<int> can;
    for (int i = 0; i < m; i++) can.insert(i);
    int ans = n;
    std::vector<std::pair<std::pair<int, int>, int> > num;
    for (int i = 0; i < n; i++) {
      got[i] = false;
      int l, r;
      std::cin >> l >> r;
      l--;
      r--;
      if (l >= 0) graph.addEdge(m + m + m + i, l, 1);
      if (r < m) graph.addEdge(m + m + m + i, m + r, 1);
      if (l >= 0)
        num.push_back(
            std::pair<std::pair<int, int>, int>(std::pair<int, int>(l, i), 0));
      if (r < m)
        num.push_back(std::pair<std::pair<int, int>, int>(
            std::pair<int, int>(m - r, i), 1));
    }
    std::sort(num.begin(), num.end());
    for (auto a : num) {
      if (got[a.first.second]) continue;
      if (a.second == 0) {
        auto it = can.begin();
        if (it != can.end() && *it <= a.first.first) {
          can.erase(it);
          ans--;
          got[a.first.second] = true;
          graph.addEdge(m + m + *it, m + m + m + a.first.second, 1);
        }
      } else {
        auto it = can.end();
        if (it != can.begin()) {
          it--;
          if (*it >= m - a.first.first) {
            can.erase(it);
            ans--;
            got[a.first.second] = true;
            graph.addEdge(m + m + *it, m + m + m + a.first.second, 1);
          }
        }
      }
    }
    for (int i = 0; i < n; i++) {
      if (!got[i]) {
        graph.addEdge(source, m + m + m + i, 1);
      }
    }
    for (auto a : can) graph.addEdge(m + m + a, sink, 1);
    std::cout << ans - graph.maxFlow(source, sink) << '\n';
  }
}
