#include <bits/stdc++.h>
using namespace std;
const int N = 10005, E = 200005;
int Head[N], Next[E], Adj[E], Flow[E], tot = 1;
inline void addedge(int u, int v, int w) {
  Next[++tot] = Head[u], Head[u] = tot, Adj[tot] = v, Flow[tot] = w;
  Next[++tot] = Head[v], Head[v] = tot, Adj[tot] = u, Flow[tot] = 0;
}
int S, T, level[N];
int Q[N];
inline bool bfs() {
  memset(level, -1, sizeof(level));
  int l = 1, r = 0;
  Q[++r] = S, level[S] = 0;
  while (l <= r) {
    int x = Q[l++];
    for (int e = Head[x]; e; e = Next[e])
      if (level[Adj[e]] == -1 && Flow[e])
        level[Adj[e]] = level[x] + 1, Q[++r] = Adj[e];
  }
  return level[T] != -1;
}
inline int dfs(int x, int flow) {
  if (x == T || !flow) return flow;
  int ret = 0, c;
  for (int e = Head[x]; e; e = Next[e])
    if (level[Adj[e]] == level[x] + 1 && Flow[e] &&
        (c = dfs(Adj[e], min(flow - ret, Flow[e])))) {
      ret += c;
      Flow[e] -= c, Flow[e ^ 1] += c;
      if (ret == flow) break;
    }
  if (!ret) level[x] = -1;
  return ret;
}
struct Node {
  int x1, y1, x2, y2;
} p[N];
int p1[N], p2[N];
int n, m, ans = 0;
inline bool judge(int a, int b, int c, int d) {
  for (int i = 1; i <= m; i++)
    if (p[i].x1 <= a && p[i].y1 <= b && c <= p[i].x2 && d <= p[i].y2)
      return true;
  return false;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d%d", &p[i].x1, &p[i].y1, &p[i].x2, &p[i].y2);
    p1[i * 2 - 1] = p[i].x1, p1[i * 2] = p[i].x2 + 1;
    p2[i * 2 - 1] = p[i].y1, p2[i * 2] = p[i].y2 + 1;
  }
  sort(p1 + 1, p1 + m * 2 + 1);
  sort(p2 + 1, p2 + m * 2 + 1);
  int v1 = unique(p1 + 1, p1 + m * 2 + 1) - p1 - 1;
  int v2 = unique(p2 + 1, p2 + m * 2 + 1) - p2 - 1;
  S = v1 + v2 + 1, T = S + 1;
  for (int i = 1; i < v1; i++) addedge(S, i, p1[i + 1] - p1[i]);
  for (int i = 1; i < v2; i++) addedge(i + v1, T, p2[i + 1] - p2[i]);
  for (int i = 1; i < v1; i++)
    for (int j = 1; j < v2; j++)
      if (judge(p1[i], p2[j], p1[i + 1] - 1, p2[j + 1] - 1))
        addedge(i, j + v1, 1 << 30);
  while (bfs()) ans += dfs(S, 1 << 30);
  printf("%d", ans);
  return 0;
}
