#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  return x * f;
}
const int N = 5e5 + 5, M = 1e6 + 5;
struct node {
  int to, nxt, flow;
} edge[M];
int head[N], cur[N], dep[N], cnt, S, T;
void pre() {
  memset(head, -1, sizeof(head));
  cnt = -1;
}
void add(int u, int v, int w) {
  edge[++cnt] = (node){v, head[u], w};
  head[u] = cnt;
  edge[++cnt] = (node){u, head[v], 0};
  head[v] = cnt;
}
bool bfs() {
  memset(dep, 0, sizeof(dep));
  dep[S] = 1;
  queue<int> q;
  q.push(S);
  memcpy(cur, head, sizeof(head));
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; ~i; i = edge[i].nxt) {
      int v = edge[i].to, f = edge[i].flow;
      if (f && !dep[v]) dep[v] = dep[u] + 1, q.push(v);
    }
  }
  return dep[T];
}
int dfs(int u, int lim) {
  if (!lim || u == T) return lim;
  int addflow = 0, flow;
  for (int i = cur[u]; ~i; i = edge[i].nxt) {
    cur[u] = i;
    int v = edge[i].to, f = edge[i].flow;
    if (dep[v] == dep[u] + 1 && (flow = dfs(v, min(lim, f)))) {
      addflow += flow;
      lim -= flow;
      edge[i].flow -= flow;
      edge[i ^ 1].flow += flow;
      if (!lim) break;
    }
  }
  return addflow;
}
int Dinic() {
  int maxflow = 0;
  while (bfs()) maxflow += dfs(S, 0x3f3f3f3f);
  return maxflow;
}
int lx[N], rx[N], dy[N], uy[N];
int x[N], y[N], cntx, cnty;
int main() {
  pre();
  read();
  int m = read();
  for (int i = 1; i <= m; ++i) {
    lx[i] = read();
    dy[i] = read();
    rx[i] = read() + 1;
    uy[i] = read() + 1;
    x[++cntx] = lx[i];
    x[++cntx] = rx[i];
    y[++cnty] = dy[i];
    y[++cnty] = uy[i];
  }
  sort(x + 1, x + cntx + 1);
  sort(y + 1, y + cnty + 1);
  cntx = unique(x + 1, x + cntx + 1) - x - 1;
  cnty = unique(y + 1, y + cnty + 1) - y - 1;
  S = cntx + cnty + 1;
  T = cntx + cnty + 2;
  for (int i = 1; i <= cntx; ++i) add(S, i, x[i] - x[i - 1]);
  for (int i = 1; i <= cnty; ++i) add(i + cntx, T, y[i] - y[i - 1]);
  for (int i = 1; i <= cntx; ++i)
    for (int j = 1; j <= cnty; ++j)
      for (int k = 1; k <= m; ++k)
        if (lx[k] <= x[i - 1] && rx[k] >= x[i] && dy[k] <= y[j - 1] &&
            uy[k] >= y[j])
          add(i, j + cntx, 0x3f3f3f3f);
  printf("%d\n", Dinic());
  return 0;
}
