#include <bits/stdc++.h>
using namespace std;
namespace std {
template <class S, class T>
struct hash<pair<S, T>> {
  size_t operator()(const pair<S, T> &p) const {
    return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
  }
};
template <class T>
struct hash<vector<T>> {
  size_t operator()(const vector<T> &v) const {
    size_t h = 0;
    for (auto i : v) h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
    return h;
  }
};
}  // namespace std
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  for (int i = 0; i < (int)v.size(); i++)
    os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{ ";
  for (const auto &i : v) os << i << ", ";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
  os << "{";
  for (const auto &i : v) os << " " << i.first << ": " << i.second << ",";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
  cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
  dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
struct FlowGraph {
  struct edge {
    int to, cap, rev;
  };
  int n, *level, *iter;
  vector<vector<edge>> G;
  FlowGraph(int sz) : n(sz) {
    G.resize(n);
    iter = new int[n];
    level = new int[n];
  }
  ~FlowGraph() {
    delete[] iter;
    delete[] level;
  }
  void add(int s, int t, int cap) {
    G[s].push_back((edge){t, cap, (int)G[t].size()});
    G[t].push_back((edge){s, 0, (int)G[s].size() - 1});
  }
  void bfs(int s) {
    for (int i = 0; i < (int)n; i++) level[i] = -1;
    queue<int> q;
    level[s] = 0;
    q.push(s);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int i = 0; i < (int)G[v].size(); i++) {
        edge &e = G[v][i];
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          q.push(e.to);
        }
      }
    }
  }
  int dfs(int v, int t, int f) {
    if (v == t) return f;
    for (int &i = iter[v]; i < (int)G[v].size(); i++) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[v] < level[e.to]) {
        int d = dfs(e.to, t, min(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          G[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }
  int max_flow(int s, int t) {
    int flow = 0;
    while (1) {
      bfs(s);
      if (level[t] < 0) return flow;
      for (int i = 0; i < (int)n; i++) iter[i] = 0;
      int f;
      while ((f = dfs(s, t, inf)) > 0) flow += f;
    }
  }
};
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  int s = 3 * m + n, t = s + 1;
  FlowGraph g(t + 1);
  for (int i = 0; i < (int)m - 1; i++) g.add(i, i + 1, m - i - 1);
  for (int i = 0; i < (int)m - 1; i++) g.add(i + 1 + m, i + m, m - i - 1);
  for (int i = 0; i < (int)m; i++) {
    g.add(i, i + 2 * m, 1);
    g.add(i + m, i + 2 * m, 1);
    g.add(i + 2 * m, t, 1);
  }
  for (int i = 0; i < (int)n; i++) {
    g.add(s, i + 3 * m, 1);
    int l, r;
    cin >> l >> r;
    if (l > 0) g.add(i + 3 * m, l - 1 + m, 1);
    if (r <= m) g.add(i + 3 * m, r - 1, 1);
  }
  cout << n - g.max_flow(s, t) << endl;
  return 0;
}
