#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 1000000001;
const long long LINF = 1000000000000000001LL;
struct PushRelabel {
  const long long FINF = LINF;
  struct edge {
    int v, revInd;
    long long cap, flow;
  };
  int n, src, dest;
  int maxLevelActive = 0;
  vector<queue<int>> active;
  vector<vector<edge>> G;
  vector<long long> exc;
  vector<int> height;
  PushRelabel(int n) : n(n), active(2 * n), G(n), exc(n, 0), height(n, 0) {}
  void addEdge(int u, int v, long long cap) {
    int sizeU = (int)(G[u]).size(), sizeV = (int)(G[v]).size();
    G[u].push_back({v, sizeV, cap, 0});
    G[v].push_back({u, sizeU, 0, 0});
  }
  void setActive(int v) {
    maxLevelActive = max(maxLevelActive, height[v]);
    active[height[v]].push(v);
  }
  void push(int u, int ind) {
    int v = G[u][ind].v, rev = G[u][ind].revInd;
    long long amount = min(exc[u], G[u][ind].cap - G[u][ind].flow);
    if (amount) {
      G[u][ind].flow += amount;
      G[v][rev].flow -= amount;
      exc[u] -= amount;
      exc[v] += amount;
      if (v != src && v != dest && exc[v] == amount) {
        setActive(v);
      }
    }
  }
  void relabel(int u) {
    int minH = INF;
    for (const edge &e : G[u]) {
      if (e.flow < e.cap) minH = min(minH, height[e.v]);
    }
    height[u] = 1 + minH;
  }
  vector<int> revBfs(int s) {
    queue<int> Q;
    Q.push(s);
    vector<int> dist(n, -1);
    dist[s] = 0;
    while (!Q.empty()) {
      int q = Q.front();
      Q.pop();
      for (edge &e : G[q])
        if (dist[e.v] == -1) {
          edge &revEdge = G[e.v][e.revInd];
          if (revEdge.flow < revEdge.cap) {
            dist[e.v] = dist[q] + 1;
            Q.push(e.v);
          }
        }
    }
    return dist;
  }
  void globalRelabeling() {
    vector<int> srcDist = revBfs(src), destDist = revBfs(dest);
    for (int i = (0); i <= (n - 1); ++i) {
      if (i == src)
        height[i] = n;
      else if (destDist[i] != -1)
        height[i] = destDist[i];
      else if (srcDist[i] != -1)
        height[i] = n + srcDist[i];
    }
  }
  long long getFlow(int src, int dest) {
    this->src = src;
    this->dest = dest;
    height[src] = n;
    exc[src] = FINF;
    for (int i = (0); i <= ((int)(G[src]).size() - 1); ++i) {
      push(src, i);
    }
    int itCount = 0;
    while (true) {
      while (maxLevelActive > -1 && active[maxLevelActive].empty()) {
        maxLevelActive--;
      }
      if (maxLevelActive == -1) break;
      int v = active[maxLevelActive].front();
      active[maxLevelActive].pop();
      if (itCount % n == 0) {
      }
      for (int i = (0); i <= ((int)(G[v]).size() - 1); ++i)
        if (height[G[v][i].v] == height[v] - 1) {
          push(v, i);
        }
      itCount++;
      if (exc[v]) {
        relabel(v);
        setActive(v);
      }
    }
    return exc[dest];
  }
  pair<long long, vector<bool>> getCut(int src, int dest) {
    long long value = getFlow(src, dest);
    vector<bool> seen(n, 0);
    queue<int> Q;
    Q.push(src);
    seen[src] = 1;
    while (!Q.empty()) {
      int q = Q.front();
      Q.pop();
      for (auto &e : G[q])
        if (!seen[e.v] && e.flow < e.cap) {
          seen[e.v] = 1;
          Q.push(e.v);
        }
    }
    return {value, seen};
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  int n, m;
  cin >> m >> n;
  const int v = 3 * n + m + 2;
  const int src = v - 2, sink = v - 1;
  const int inf = m;
  PushRelabel net(v);
  for (int i = (0); i <= (m - 1); ++i) {
    net.addEdge(src, i, 1);
  }
  for (int i = (0); i <= (n - 1); ++i) {
    if (i < n - 1) {
      net.addEdge(m + i, m + i + 1, inf);
      net.addEdge(n + m + i + 1, n + m + i, inf);
    }
    net.addEdge(m + i, 2 * n + m + i, 1);
    net.addEdge(n + m + i, 2 * n + m + i, 1);
    net.addEdge(2 * n + m + i, sink, 1);
  }
  for (int i = (0); i <= (m - 1); ++i) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    if (l >= 0) {
      net.addEdge(i, n + m + l, 1);
    }
    if (r < n) {
      net.addEdge(i, m + r, 1);
    }
  }
  cout << m - net.getFlow(src, sink);
  return 0;
}
