#include <bits/stdc++.h>
using namespace std;
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;
}
long long powmod(long long x, long long n) {
  long long 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 = 6e6 + 100;
  static const int M = 8e6 + 100;
  struct Edge {
    int to, w, flow, next;
  } E[M << 1];
  int P[N], tot, n, deep[N];
  void init(int _n) {
    n = _n;
    tot = 0, memset(P, -1, (n + 2) << 2);
  }
  void add(int u, int v, int flow) {
    E[tot].to = v, E[tot].next = P[u], E[tot].flow = flow;
    P[u] = tot++;
    E[tot].to = u, E[tot].next = P[v], E[tot].flow = 0;
    P[v] = tot++;
  }
  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 (int i = P[now]; ~i; i = E[i].next) {
        Edge u = E[i];
        if (!deep[u.to] && u.flow) {
          deep[u.to] = deep[now] + 1;
          q[++r] = u.to;
        }
      }
    }
    return deep[end];
  }
  int dfs(int start, int flow, int &end) {
    if (start == end) return flow;
    int f = 0;
    for (int i = P[start]; ~i && flow; i = E[i].next) {
      Edge &u = E[i];
      if (deep[start] + 1 == deep[u.to] && u.flow) {
        int r = dfs(u.to, min(flow, u.flow), end);
        u.flow -= r;
        E[i ^ 1].flow += r;
        f += r, flow -= r;
      }
    }
    if (!f) deep[start] = -2;
    return f;
  }
  int max_flow(int S, int T) {
    int ret = 0;
    while (bfs(S, T)) {
      ret += dfs(S, inf, T);
    }
    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], inf);
      if (rson[rt]) flow.add(rt, rson[rt], inf);
    }
  }
  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, inf);
    } 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;
pair<int, int> seg[maxn];
int main() {
  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] = make_pair(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);
    S = A.tot + 1;
    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].first, id);
      A.update(1, n, x, seg[i].second, 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);
    }
    int ans = flow.max_flow(y, S * 2);
    cout << m - ans << '\n';
  }
  return 0;
}
