#include <bits/stdc++.h>
#define REP(i, a, b) for (int i = a; i <= b; ++i)
#define PER(i, a, b) for (int i = a; i >= b; --i)
#define RVC(i, S) for (int i = 0; i < S.size(); ++i)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> VI;

inline int read(){
	int x = 0, ch = getchar(), f = 1;
	while (!isdigit(ch)){if (ch == '-') f = -1; ch = getchar();}
	while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
	return x * f;
}

const int N = 800005, M = 10000005, INF = 0x3f3f3f3f;

struct Edge{int to, c, nxt; };

namespace Network{
	int m, s, t, fr[N], vis[N], cur[N], d[N];
	Edge E[M];
	void init(){
		m = 0;
		memset(fr, -1, sizeof fr);
	}
	inline void addedge(int x, int y, int c){
		E[m++] = (Edge){y, c, fr[x]}; fr[x] = m - 1;
		E[m++] = (Edge){x, 0, fr[y]}; fr[y] = m - 1;
	}
	int bfs(){
		queue<int> q;
		memset(vis, 0, sizeof vis);
		memcpy(cur, fr, sizeof cur);
		d[s] = 0; vis[s] = 1;
		q.push(s);
		while (!q.empty()){
			int h = q.front(); q.pop();
			for (int i = fr[h]; i != -1; i = E[i].nxt)
				if (E[i].c && !vis[E[i].to]){
					vis[E[i].to] = 1;
					d[E[i].to] = d[h] + 1;
					q.push(E[i].to);
				}
		}
		return vis[t];
	}
	int dfs(int x, int a){
		if (x == t || a == 0) return a;
		int fl = 0, f;
		for (int &i = cur[x]; i != -1; i = E[i].nxt)
			if (d[E[i].to] == d[x] + 1 && (f = dfs(E[i].to, min(a, E[i].c))) > 0){
				E[i].c -= f; E[i ^ 1].c += f;
				fl += f; a -= f;
				if (!a) break;
			}
		return fl;
	}
	int maxflow(int _s, int _t){
		s = _s; t = _t;
		int fl = 0;
		while (bfs()) fl += dfs(s, INF);
		return fl;
	}
}

int idx, id[N], n, m;

#define lc (x << 1)
#define rc (x << 1 | 1)

void build(int x, int l, int r){
	id[x] = ++idx;
	if (l == r){
		Network::addedge(id[x], n + l, 1);
		return;
	}
	int mid = l + r >> 1;
	build(lc, l, mid);
	build(rc, mid + 1, r);
	Network::addedge(id[x], id[lc], INF);
	Network::addedge(id[x], id[rc], INF);
}

void link(int x, int l, int r, int ql, int qr, int p){
	if (ql <= l && r <= qr){
		Network::addedge(p, id[x], 1);
		return;
	}
	int mid = l + r >> 1;
	if (ql <= mid) link(lc, l, mid, ql, qr, p);
	if (qr > mid) link(rc, mid + 1, r, ql, qr, p);
}

int main(){
	n = read(), m = read();
	Network::init();
	idx = n + m;
	build(1, 1, m);
	REP(i, 1, n){
		int l = read(), r = read();
		if (l > 0) link(1, 1, m, 1, l, i);
		if (r <= m) link(1, 1, m, r, m, i);
		Network::addedge(0, i, 1);
	}
	int T = ++idx;
	REP(i, 1, m) Network::addedge(n + i, T, 1);
	int f = Network::maxflow(0, T);
	printf("%d\n", n - f);
	return 0;
}