#include <iostream>
#include <string>
#include <cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#define _USE_MATH_DEFINES
#include <math.h>
#include <functional>
#include<complex>
using namespace std;

#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 1000000007;
const ll MAX = 4000001;
const long double eps = 1E-14;

ll max(ll a, ll b) {
	if (a > b) { return a; }
	return b;
}

ll min(ll a, ll b) {
	if (a > b) { return b; }
	return a;
}

ll gcd(ll a, ll b) {
	if (b == 0) { return a; }
	if (a < b) { return gcd(b, a); }
	return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}

struct edge {
	ll ind;
	ll fr;
	ll to;
	ll 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(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	// for prime MOD
	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, ll b) {
	if (b == 0) { return 1; }
	mint ret = pw(a, b >> 1);
	ret *= ret;
	if (b & 1) { ret *= a; }
	return ret;
}

typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;

typedef vector<mint> vmint;
typedef vector<vector<mint>> vvmint;
typedef vector<vector<vector<mint>>> vvvmint;

//////////////////////////////////////
struct Ford_Fulkerson {

	struct f_edge {
		ll to;
		ll cap;
		ll fl;
		ll rev;//反対方向の辺の添え字
	};


	ll N;
	ll s, t;
	vector<vector<edge>> g;//グラフ片方向にのみ張る、flowで双方向に構築
	vector<vector<f_edge>> flow;
	ll maxflow;

	vector<bool> see;
	ll dfs(ll v, ll x) {
		see[v] = 1;

		if (v == t) { return x; }

		rep(i, flow[v].size()) {
			const auto e = flow[v][i];

			if (see[e.to] == 1) { continue; }
			if (e.cap == e.fl) { continue; }

			ll y = min(x, e.cap - e.fl);

			y = dfs(e.to, y);
			if (y == 0) { continue; }

			flow[v][i].fl += y;
			flow[e.to][e.rev].fl -= y;
			return y;
		}

		return 0;
	}

	Ford_Fulkerson(ll N0, vector<vector<edge>> g0, ll s0, ll t0) {
		N = N0;
		g = g0;
		s = s0;
		t = t0;

		flow.resize(N + 1);
		repn(v, N) {
			for (edge e : g[v]) {
				ll w = e.to;
				flow[v].push_back({ w,e.d,0,flow[w].size() });
				flow[w].push_back({ v,0,0,flow[v].size() - 1 });
			}
		}

		maxflow = 0;

		if (s == t) { maxflow = INF; return; }

		while (1) {
			see.assign(N + 1, 0);
			ll f = dfs(s, INF);
			if (f == 0) { break; }
			maxflow += f;
		}

		maxflow = min(INF, maxflow);

		return;

	}
};

int main() {
	ll N, M;
	cin >> N >> M;

	vll L(N + 1), R(N + 1);
	repn(i, N) cin >> L[i] >> R[i];

	ll V = N + 3 * M + 2;
	vector<vector<edge>> g(V + 1);

	repn(i, N) {
		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 }); }
	}

	repn(i, M - 1) {
		g[N + i + 1].push_back({ 0,0,N + i,INF });
		g[N + M + i].push_back({ 0,0,N + M + i + 1,INF });
	}

	repn(i, M) {
		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 });
	}

	//repn(v, V) {
	//	for (edge e : g[v]) { cout << v << " " << e.to << endl; }
	//}

	Ford_Fulkerson ff(V, g, V - 1, V);

	ll X = ff.maxflow;
	cout << N - X << endl;

}