#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e17;
const long long MOD = 1000000007;
const long long MAX = 4000001;
const long double eps = 1E-14;
long long max(long long a, long long b) {
  if (a > b) {
    return a;
  }
  return b;
}
long long min(long long a, long long b) {
  if (a > b) {
    return b;
  }
  return a;
}
long long gcd(long long a, long long b) {
  if (b == 0) {
    return a;
  }
  if (a < b) {
    return gcd(b, a);
  }
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
struct edge {
  long long ind;
  long long fr;
  long long to;
  long long d;
};
class mint {
  long long x;

 public:
  mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint& operator+=(const mint& a) {
    if ((x += a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator-=(const mint& a) {
    if ((x += MOD - a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator*=(const mint& a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint& a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint& a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint& a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(long long t) const {
    if (!t) return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1) a *= *this;
    return a;
  }
  mint inv() const { return pow(MOD - 2); }
  mint& operator/=(const mint& a) { return (*this) *= a.inv(); }
  mint operator/(const mint& a) const {
    mint res(*this);
    return res /= a;
  }
  friend ostream& operator<<(ostream& os, const mint& m) {
    os << m.x;
    return os;
  }
};
mint pw(mint a, long long b) {
  if (b == 0) {
    return 1;
  }
  mint ret = pw(a, b >> 1);
  ret *= ret;
  if (b & 1) {
    ret *= a;
  }
  return ret;
}
struct max_flow {
  struct f_edge {
    int to, cap, rev;
  };
  int V;
  vector<vector<f_edge>> G;
  vector<int> itr, level;
  max_flow(int V) : V(V) { G.assign(V, vector<f_edge>()); }
  void add_edge(int from, int to, int cap) {
    G[from].push_back({to, cap, (int)G[to].size()});
    G[to].push_back({from, 0, (int)G[from].size() - 1});
  }
  void bfs(int s) {
    level.assign(V, -1);
    queue<int> q;
    level[s] = 0;
    q.push(s);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (auto& e : G[v]) {
        if (e.cap > 0 and 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 = itr[v]; i < (int)G[v].size(); ++i) {
      f_edge& e = G[v][i];
      if (e.cap > 0 and 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 run(int s, int t) {
    int ret = 0, f;
    while (bfs(s), level[t] >= 0) {
      itr.assign(V, 0);
      while ((f = dfs(s, t, 1e9)) > 0) ret += f;
    }
    return ret;
  }
};
int main() {
  long long N, M;
  cin >> N >> M;
  vector<long long> L(N + 1), R(N + 1);
  for (long long i = 1; i <= N; i++) cin >> L[i] >> R[i];
  long long V = N + 3 * M + 2;
  vector<vector<edge>> g(V + 1);
  for (long long i = 1; i <= N; i++) {
    g[N + 3 * M + 1].push_back({0, 0, i, 1});
    if (L[i] > 0) {
      g[i].push_back({0, 0, N + L[i], 1});
    }
    if (R[i] <= M) {
      g[i].push_back({0, 0, N + M + R[i], 1});
    }
  }
  for (long long i = 1; i <= M - 1; i++) {
    g[N + i + 1].push_back({0, 0, N + i, INF});
    g[N + M + i].push_back({0, 0, N + M + i + 1, INF});
  }
  for (long long i = 1; i <= M; i++) {
    g[N + i].push_back({0, 0, N + 2 * M + i, 1});
    g[N + M + i].push_back({0, 0, N + 2 * M + i, 1});
    g[N + 2 * M + i].push_back({0, 0, N + 3 * M + 2, 1});
  }
  max_flow mf(V + 1);
  for (long long v = 1; v <= V; v++)
    for (edge e : g[v]) {
      mf.add_edge(v, e.to, e.d);
    }
  long long X = mf.run(V - 1, V);
  cout << N - X << endl;
}
