#include <bits/stdc++.h>
using namespace std;
template <class T>
void dbs(string str, T t) {
  cout << str << " : " << t << endl;
}
template <class T, class... second>
void dbs(string str, T t, second... s) {
  int idx = str.find(',');
  cout << str.substr(0, idx) << " : " << t << ",";
  dbs(str.substr(idx + 1), s...);
}
template <class second, class T>
ostream& operator<<(ostream& os, const pair<second, T>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& p) {
  os << "[ ";
  for (auto& it : p) os << it << " ";
  return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, const set<T>& p) {
  os << "[ ";
  for (auto& it : p) os << it << " ";
  return os << "]";
}
template <class second, class T>
ostream& operator<<(ostream& os, const map<second, T>& p) {
  os << "[ ";
  for (auto& it : p) os << it << " ";
  return os << "]";
}
template <class T>
void prc(T a, T b) {
  cout << "[";
  for (T i = a; i != b; ++i) {
    if (i != a) cout << ", ";
    cout << *i;
  }
  cout << "]";
  cout << endl;
}
const int inf = 0x3f3f3f3f;
struct edge {
  int x, y, cap, flow;
};
struct DinicFlow {
  vector<edge> e;
  vector<int> cur, d;
  vector<vector<int> > adj;
  int n, source, sink;
  DinicFlow() {}
  DinicFlow(int v) {
    n = v;
    cur = vector<int>(n + 1);
    d = vector<int>(n + 1);
    adj = vector<vector<int> >(n + 1);
  }
  void AddEdge(int from, int to, int cap) {
    edge e1 = {from, to, cap, 0};
    edge e2 = {to, from, 0, 0};
    adj[from].push_back(e.size());
    e.push_back(e1);
    adj[to].push_back(e.size());
    e.push_back(e2);
  }
  int bfs() {
    queue<int> q;
    for (int i = 0; i <= n; ++i) d[i] = -1;
    q.push(source);
    d[source] = 0;
    while (!q.empty() and d[sink] < 0) {
      int x = q.front();
      q.pop();
      for (int i = 0; i < (int)adj[x].size(); ++i) {
        int id = adj[x][i], y = e[id].y;
        if (d[y] < 0 and e[id].flow < e[id].cap) {
          q.push(y);
          d[y] = d[x] + 1;
        }
      }
    }
    return d[sink] >= 0;
  }
  int dfs(int x, int flow) {
    if (!flow) return 0;
    if (x == sink) return flow;
    for (; cur[x] < (int)adj[x].size(); ++cur[x]) {
      int id = adj[x][cur[x]], y = e[id].y;
      if (d[y] != d[x] + 1) continue;
      int pushed = dfs(y, min(flow, e[id].cap - e[id].flow));
      if (pushed) {
        e[id].flow += pushed;
        e[id ^ 1].flow -= pushed;
        return pushed;
      }
    }
    return 0;
  }
  int GetMaxFlow(int src, int snk) {
    this->source = src;
    this->sink = snk;
    int flow = 0;
    while (bfs()) {
      for (int i = 0; i <= n; ++i) cur[i] = 0;
      while (int pushed = dfs(source, inf)) {
        flow += pushed;
      }
    }
    return flow;
  }
};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  DinicFlow G(n + 3 * m + 2);
  int s = n + 3 * m, t = n + 3 * m + 1;
  for (int i = (0); i <= (m - 2); ++i) G.AddEdge(n + i, n + i + 1, n);
  for (int i = (1); i <= (m - 1); ++i) G.AddEdge(n + m + i, n + m + i - 1, n);
  for (int i = (0); i <= (m - 1); ++i) G.AddEdge(n + 2 * m + i, t, 1);
  for (int i = (0); i <= (m - 1); ++i) G.AddEdge(n + m + i, n + 2 * m + i, 1);
  for (int i = (0); i <= (m - 1); ++i) G.AddEdge(n + i, n + 2 * m + i, 1);
  for (int it = (0); it <= (n - 1); ++it) {
    int l, r;
    cin >> l >> r;
    --l, --r;
    G.AddEdge(s, it, 1);
    if (r >= 0 and r <= m - 1) G.AddEdge(it, n + r, 1);
    if (l >= 0 and l <= m - 1) G.AddEdge(it, n + m + l, 1);
  }
  cout << n - G.GetMaxFlow(s, t) << "\n";
  return 0;
}
