#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3005;
const int MAXE = 5000005;
const long long INF = (long long)1e+17;
int n, m, S, T;
int dep[MAXN], head[MAXN], cur[MAXN], tot;
struct edge {
  int v;
  int nxt;
  long long val;
} e[MAXE];
void add(int u, int v, long long w) {
  e[tot].v = v;
  e[tot].nxt = head[u];
  e[tot].val = w;
  head[u] = tot++;
}
void addedge(int u, int v, long long w) {
  add(u, v, w);
  add(v, u, 0);
}
bool bfs(int s, int t) {
  memset(dep, 0, sizeof(dep));
  queue<int> q;
  q.push(s);
  dep[s] = 1;
  cur[s] = head[s];
  while (!q.empty()) {
    s = q.front();
    q.pop();
    for (int i = head[s]; i != -1; i = e[i].nxt) {
      int v = e[i].v;
      if (e[i].val > 0 && !dep[v]) {
        dep[v] = dep[s] + 1;
        cur[v] = head[v];
        if (v == t) return true;
        q.push(v);
      }
    }
  }
  return false;
}
long long dfs(int s, int t, long long flow) {
  if (s == t || flow <= 0) return flow;
  long long rest = flow;
  for (int &i = cur[s]; i != -1; i = e[i].nxt) {
    int v = e[i].v;
    if (e[i].val > 0 && dep[v] == dep[s] + 1) {
      long long tmp = dfs(v, t, min(rest, e[i].val));
      if (tmp <= 0) dep[v] = 0;
      rest -= tmp;
      e[i].val -= tmp;
      e[i ^ 1].val += tmp;
      if (rest <= 0) break;
    }
  }
  return flow - rest;
}
long long maxflow(int s, int t) {
  long long ans = 0;
  while (bfs(s, t)) ans += dfs(s, t, INF);
  return ans;
}
int x1[MAXN], y11[MAXN], x2[MAXN], y2[MAXN];
int tx[MAXN], ty[MAXN];
int main() {
  tot = 0;
  memset(head, -1, sizeof(head));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d%d", &x1[i], &y11[i], &x2[i], &y2[i]);
    x2[i] += 1;
    y2[i] += 1;
    tx[++tx[0]] = x1[i];
    tx[++tx[0]] = x2[i];
    ty[++ty[0]] = y11[i];
    ty[++ty[0]] = y2[i];
  }
  sort(tx + 1, tx + 1 + tx[0]);
  sort(ty + 1, ty + 1 + ty[0]);
  tx[0] = unique(tx + 1, tx + 1 + tx[0]) - tx - 1;
  ty[0] = unique(ty + 1, ty + 1 + ty[0]) - ty - 1;
  S = 0;
  T = tx[0] + ty[0] + 1;
  for (int i = 1; i < tx[0]; i++) addedge(S, i, tx[i + 1] - tx[i]);
  for (int i = 1; i < ty[0]; i++) addedge(i + tx[0], T, ty[i + 1] - ty[i]);
  for (int i = 1; i < tx[0]; i++)
    for (int j = 1; j < ty[0]; j++)
      for (int k = 1; k <= m; k++)
        if (x1[k] <= tx[i] && x2[k] >= tx[i + 1] && y11[k] <= ty[j] &&
            y2[k] >= ty[j + 1]) {
          addedge(i, tx[0] + j, INF);
          break;
        }
  long long res = maxflow(S, T);
  printf("%lld\n", res);
  return 0;
}
