#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 = 2e5 + 100;
  static const int M = 3e6 + 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 main() {
  int n, m;
  cin >> n >> m;
  swap(n, m);
  flow.init(n + m * 3 + 10);
  for (int i = 1; i <= m; ++i) {
    int x, y;
    cin >> x >> y;
    for (int j = 1; j <= x; ++j) {
      flow.add(i + m, j + m * 2, 1);
    }
    for (int j = y; j <= m; ++j) {
      flow.add(i + m, j + m * 2, 1);
    }
  }
  for (int i = 1; i <= m; ++i) {
    flow.add(0, i, 1);
    flow.add(i, i + m, 1);
  }
  for (int i = 1; i <= n; ++i) {
    flow.add(i + m * 3, n + m * 3 + 1, 1);
    flow.add(i + m * 2, i + m * 3, 1);
  }
  int ans = flow.max_flow(0, n + m * 3 + 1);
  cout << m - ans << '\n';
  return 0;
}
