#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#define rep(i, a, b) for (int i = (a), _ = (b); i <= _; ++ i)
#define per(i, a, b) for (int i = (a), _ = (b); i >= _; -- i)
#define For(i, a, b) for (int i = (a), _ = (b); i < _; ++ i)
#define fore(e, u, v) for (int p = e(u), v = e[p].y; p; v = e[p = e[p].nxt].y)
#define ri rd<int>
using namespace std;
const int maxN = 6e5 + 7;
const int maxM = maxN * 120;
const int INF = 1e9 + 7;

template<class T> inline T rd() {
	bool f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = 0;
	T x = 0; for (; isdigit(c); c = getchar()) x = x * 10 + c - 48; return f ? x : -x;
}

int n, m;
int S = 0, T = 600003;

struct vec {
	static const int maxE = maxM;
	int g[maxN], te;
	struct edge {int y, f, nxt;} e[maxE << 1];
	vec() {te = 1;}
	inline void push(int x, int y, int f) {e[++te] = (edge){y, f, g[x]}; g[x] = te;}
	inline void link(int x, int y, int f) {push(x, y, f), push(y, x, 0);}
	inline int& operator () (int x) {return g[x];}
	inline edge& operator [] (int x) {return e[x];}
}e;

namespace Seg {
	int tt, rt, lc[maxN], rc[maxN];

	int build(int l, int r) {
		int x = ++ tt;
		if (l == r) {
			e.link(x, T, 1);
			return x;
		}
		int mid = (l + r) >> 1;
		lc[x] = build(l, mid);
		rc[x] = build(mid+1, r);
		e.link(x, lc[x], INF);
		e.link(x, rc[x], INF);
		return x;
	}

	void add(int x, int l, int r, int tl, int tr, int t) {
		if (tl <= l && r <= tr) {
			e.link(t, x, INF);
			return;
		}
		int mid = (l + r) >> 1;
		if (tl <= mid) add(lc[x], l, mid, tl, tr, t);
		if (mid < tr) add(rc[x], mid+1, r, tl, tr, t);
	}
	
	inline void add(int t, int l, int r) {add(rt, 1, m, l, r, t);}

	inline void build() {
		tt = 0;
		rt = build(1, m);
	}
}

namespace G {

	int lev[maxN];
	
	bool bfs() {
		static int q[maxN]; int h = 0, t = 1;
		memset(lev, 0, sizeof lev);
		q[1] = S, lev[S] = 1;
		while (h != t) {
			int x = q[++h];
			fore (e, x, y) if (e[p].f && !lev[y]) {
				lev[y] = lev[x] + 1;
				if (y == T) return 1;
				q[++t] = y;
			}
		}
		return 0;
	}

	int dfs(int x, int fl) {
		if (x == T) return fl;
		int res = 0;
		fore (e, x, y) if (e[p].f && lev[x] + 1 == lev[y]) {
			int tp = dfs(y, min(fl, e[p].f));
			if (tp) {
				res += tp;
				fl -= tp;
				e[p].f -= tp;
				e[p^1].f += tp;
				if (fl == 0) return res;
			}
		}
		if (!res) lev[x] = 0;
		return res;
	}

	int dinic() {
		int res = 0;
		while (bfs())
		 	res += dfs(S, INF);
		return res;
	}
}

int main() {

	n = ri(), m = ri();
	Seg::build();
	rep (i, 1, n) {
		int l = ri(), r = ri();
		e.link(S, i + Seg::tt, 1);
		if (l >= 1) Seg::add(i + Seg::tt, 1, l);
		if (r <= m) Seg::add(i + Seg::tt, r, m);
	}

	printf("%d\n", n - G::dinic());

	return 0;
}