#include <bits/stdc++.h>
#define rep(__i, l, r) for(int __i = (int)(l);__i <= (int)(r);++__i)
//#define M 26
#define fi first
#define se second
#define PB push_back
#define MP make_pair
#define ls (rt<<1)
#define rs (rt<<1|1)
using namespace std;
typedef long long ll;
//#define double long double
typedef unsigned long long ull;
//typedef __int128 LLL;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 1e9 + 10;
const int maxn = 1e6 + 200;
const int mod = 1e9 + 7;
const double eps = 1e-9;
int dcmp(double x) {
	if (fabs(x) < eps) return 0;
	return x < 0 ? -1 : 1;
}
ll powmod(ll x, ll n) {
	ll ret = 1;
	for (; n > 0; n >>= 1, x = x * x % mod)if (n & 1) ret = ret * x % mod;
	return ret;
}
//
struct net_flow {
		static const int N = 3e6 + 100;
		struct Edge {
				int to, flow, rev;
		};
		vector<Edge> v[N];
		int n, deep[N];
		void init(int _n) {
			n = _n;
			for (int i = 0; i <= n; ++i) v[i].clear();
		}
		void add(int x, int y, int flow) {
			v[x].PB(Edge{y, flow, v[y].size()});
			v[y].PB(Edge{x, 0, v[x].size() - 1});
		}
		bool bfs(int start, int end) {
			memset(deep, 0, (n + 5) << 2);
			static int q[N << 2];
			int l = 0, r = -1;
			q[++r] = start, deep[start] = 1;
			while (l <= r) {
				int now = q[l++];
				for (auto &u:v[now]) {
					if (!deep[u.to] && u.flow) {
						deep[u.to] = deep[now] + 1;
						q[++r] = u.to;
					}
				}
			}
			return deep[end];
		}
		int dfs(int S, int flow, int T) {
			if (S == T) return flow;
			int f = 0;
			for (auto &u:v[S]) {
				if (!flow) break;
				if (deep[S] + 1 == deep[u.to] && u.flow) {
					int r = dfs(u.to, min(flow, u.flow), T);
					u.flow -= r;
					v[u.to][u.rev].flow += r;
					f += r, flow -= r;
				}
			}
			if (!f) deep[S] = -2;
			return f;
		}
		int cnt = 0;
		int max_flow(int S, int T) {
			int ret = 0;
			double c = clock();
			while (bfs(S, T)) {
//				++cnt;c = clock();
				ret += dfs(S, inf, T);
//				cout << (clock() - c) / CLOCKS_PER_SEC << '\n';
			}
			return ret;
		}
} flow;
int S;
struct Laji {
		int tot = 0, lson[maxn], rson[maxn];
		void build(int l, int r, int &rt) {
			rt = ++tot;
			lson[rt] = rson[rt] = 0;
			if (l != r) {
				int mid = l + r >> 1;
				build(l, mid, lson[rt]);
				build(mid + 1, r, rson[rt]);
				if (lson[rt]) flow.add(rt, lson[rt], r - l + 1);
				if (rson[rt]) flow.add(rt, rson[rt], r - l + 1);
			}
		}
		int cnt = 0;
		void update(int l, int r, int rt, int ql, int qr, int x) {
			if (ql > qr) return;
			if (ql <= l && qr >= r) {
				flow.add(x, rt, r - l + 1);
				cnt++;
			} else {
				int mid = l + r >> 1;
				if (ql <= mid) update(l, mid, lson[rt], ql, qr, x);
				if (qr > mid) update(mid + 1, r, rson[rt], ql, qr, x);
			}
		}
		int getid(int l, int r, int rt, int p) {
			if (l == r) return rt;
			int mid = l + r >> 1;
			if (p <= mid) return getid(l, mid, lson[rt], p);
			else return getid(mid + 1, r, rson[rt], p);
		}
} A, B;
pii seg[maxn];
int main() {
//	freopen("1.in", "r", stdin);
//	freopen("1.out", "w", stdout);
//	ios::sync_with_stdio(0), cin.tie(0);
	double c = clock();
	int n, m;
	while (cin >> n >> m) {
		swap(n, m);
		for (int i = 1; i <= m; ++i) {
			int x, y;
			cin >> x >> y;
			seg[i] = MP(x, y);
		}
		S = (n + m + 10) * 2;
		flow.init(S * 2 + 10);
		int x, y;
		B.build(1, m, y);
		A.tot = B.tot;
		A.build(1, n, x);
		for (int i = 1; i <= m; ++i) {
			int id = B.getid(1, m, y, i) + S;
			flow.add(id - S, id, 1);
			A.update(1, n, x, 1, seg[i].fi, id);
			A.update(1, n, x, seg[i].se, n, id);
		}
		for (int i = 1; i <= n; ++i) {
			int id = A.getid(1, n, x, i) + S;
			flow.add(id - S, id, 1);
			flow.add(id, S * 2, 1);
		}
//		cout << A.tot << ' ' << B.tot << ' ' << S << '\n';
		int ans = flow.max_flow(y, S * 2);
//		cout << A.cnt << ' ' << flow.cnt << '\n';
		cout << m - ans << '\n';
	}
//	cout << (clock() - c) / CLOCKS_PER_SEC << '\n';
	return 0;
}