#include <bits/stdc++.h>
using namespace std;
const long long maxn = 405;
const long long inf = 1e18;
long long n, m, s, t, dep[maxn];
struct edge {
  long long to, next, w;
} e[maxn * maxn * 2];
long long head[maxn], cur[maxn], ecnt = 1;
struct node {
  long long x1, y1, x2, y2;
} a[maxn];
long long x[maxn * 2], xx, y[maxn * 2], yy;
void adde(long long u, long long v, long long w) {
  e[++ecnt].to = v;
  e[ecnt].w = w;
  e[ecnt].next = head[u];
  head[u] = ecnt;
}
void add(long long u, long long v, long long w) {
  adde(u, v, w);
  adde(v, u, 0);
}
long long read() {
  long long res = 0, f = 1;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') f = -1;
  } while (!isdigit(ch));
  do {
    res = res * 10 + ch - '0';
    ch = getchar();
  } while (isdigit(ch));
  return res * f;
}
long long bfs() {
  queue<long long> q;
  q.push(s);
  memset(dep, 0, sizeof(dep));
  dep[s] = 1;
  while (!q.empty()) {
    long long u = q.front();
    q.pop();
    for (long long i = head[u]; i; i = e[i].next) {
      long long v = e[i].to;
      if (dep[v] || !e[i].w) continue;
      dep[v] = dep[u] + 1;
      q.push(v);
    }
  }
  return dep[t];
}
long long dfs(long long u, long long flow) {
  if (u == t) return flow;
  long long cost = 0, use = 0;
  for (long long &i = cur[u]; i; i = e[i].next) {
    if (!e[i].w) continue;
    long long v = e[i].to;
    if (dep[v] != dep[u] + 1) continue;
    cost = dfs(v, min(flow - use, e[i].w));
    e[i].w -= cost;
    e[i ^ 1].w += cost;
    use += cost;
    if (use == flow) break;
  }
  return use;
}
long long dinic() {
  long long res = 0;
  while (bfs()) {
    memcpy(cur, head, sizeof(cur));
    res += dfs(s, inf);
  }
  return res;
}
signed main() {
  n = read();
  m = read();
  for (long long i = (1); i <= (m); ++i) {
    a[i].x1 = read();
    a[i].y1 = read();
    a[i].x2 = read() + 1;
    a[i].y2 = read() + 1;
    x[++xx] = a[i].x1;
    y[++yy] = a[i].y1;
    x[++xx] = a[i].x2;
    y[++yy] = a[i].y2;
  }
  x[++xx] = n + 1;
  y[++yy] = n + 1;
  sort(x + 1, x + xx + 1);
  sort(y + 1, y + yy + 1);
  xx = unique(x + 1, x + xx + 1) - x - 1;
  yy = unique(y + 1, y + yy + 1) - y - 1;
  s = 0;
  t = xx + yy + 1;
  for (long long i = (1); i <= (m); ++i) {
    a[i].x1 = lower_bound(x + 1, x + xx + 1, a[i].x1) - x;
    a[i].y1 = lower_bound(y + 1, y + yy + 1, a[i].y1) - y;
    a[i].x2 = lower_bound(x + 1, x + xx + 1, a[i].x2) - x;
    a[i].y2 = lower_bound(y + 1, y + yy + 1, a[i].y2) - y;
    for (long long p = (a[i].x1); p <= (a[i].x2 - 1); ++p)
      for (long long q = (a[i].y1); q <= (a[i].y2 - 1); ++q)
        add(p, q + xx, inf);
  }
  for (long long i = (1); i <= (xx - 1); ++i) add(s, i, x[i + 1] - x[i]);
  for (long long i = (1); i <= (yy - 1); ++i) add(i + xx, t, y[i + 1] - y[i]);
  printf("%lld\n", dinic());
}
