#include <bits/stdc++.h>
using namespace std;
struct node {
  int v, w, to;
} edge[50005 * 20];
int pre[50005], cnt, dep[50005];
int S, T, z, head[50005], id;
int qq[50005], cur[50005];
void link(int u, int v, int w) {
  edge[cnt] = {v, w, head[u]};
  head[u] = cnt++;
  edge[cnt] = {u, 0, head[v]};
  head[v] = cnt++;
}
bool bfs() {
  for (int i = 0; i <= T; i++) dep[i] = 0;
  dep[S] = 1;
  int l = 0, r = 1;
  qq[r] = S;
  while (l < r) {
    int u = qq[++l];
    for (int i = head[u]; i != -1; i = edge[i].to) {
      int v = edge[i].v;
      if (!dep[v] && edge[i].w) dep[v] = dep[u] + 1, qq[++r] = v;
    }
  }
  return dep[T];
}
long long dfs(int u, long long mi) {
  long long res = 0;
  if (mi == 0 || u == T) return mi;
  for (int &i = cur[u]; i != -1; i = edge[i].to) {
    int v = edge[i].v;
    if (dep[u] + 1 == dep[v] && edge[i].w) {
      int minn = dfs(v, min(mi - res, 1ll * edge[i].w));
      edge[i].w -= minn;
      edge[i ^ 1].w += minn;
      res += minn;
      if (res == mi) return res;
    }
  }
  if (res == 0) dep[u] = 0;
  return res;
}
long long dinic() {
  long long res = 0;
  while (bfs()) {
    memcpy(cur, head, sizeof(head));
    res += dfs(S, 1e17);
  }
  return res;
}
int lowb(int x, int a[], int len) {
  return lower_bound(a + 1, a + len + 1, x) - a;
}
int n, m;
int xl[200], xr[200], cx[200], yl[200], yr[200], cy[200];
int main() {
  cin >> n >> m;
  for (int i = (1); i <= (m); i++)
    scanf("%d%d%d%d", &xl[i], &yl[i], &xr[i], &yr[i]),
        cx[i] = xl[i], cx[m + i] = xr[i] + 1, cy[i] = yl[i],
        cy[m + i] = yr[i] + 1;
  sort(cx + 1, cx + m * 2 + 1);
  sort(cy + 1, cy + m * 2 + 1);
  int xlen = unique(cx + 1, cx + m * 2 + 1) - (cx + 1);
  int ylen = unique(cy + 1, cy + m * 2 + 1) - (cy + 1);
  S = 0, T = xlen + ylen + 1;
  for (int i = (0); i <= (T); i++) head[i] = -1;
  for (int i = (1); i <= (xlen - 1); i++) link(S, i, cx[i + 1] - cx[i]);
  for (int i = (1); i <= (ylen - 1); i++) link(i + xlen, T, cy[i + 1] - cy[i]);
  for (int i = (1); i <= (m); i++) {
    int a = lowb(xl[i], cx, xlen), b = lowb(xr[i] + 1, cx, xlen),
        c = lowb(yl[i], cy, ylen), d = lowb(yr[i] + 1, cy, ylen);
    for (int x = (a); x <= (b - 1); x++)
      for (int y = (c); y <= (d - 1); y++) link(x, y + xlen, 0x3f3f3f3f);
  }
  printf("%lld\n", dinic());
  return 0;
}
