#include <bits/stdc++.h>
using namespace std;
const int N = 1e4, M = 55, E = 1e6, INF = 1e9;
int n, m;
int xl[M], yl[M], xr[M], yr[M];
int bx[M << 1], by[M << 1], tx, ty;
int h[N], to[E], nxt[E], flow[E], ecnt = 1, hh[N];
inline void adde(int u, int v, int w) {
  nxt[++ecnt] = h[u];
  h[u] = ecnt;
  to[ecnt] = v;
  flow[ecnt] = w;
}
inline void add(int u, int v, int w) {
  adde(u, v, w);
  adde(v, u, 0);
}
int vis[N], dep[N], S, T, gap[N];
inline void bfs() {
  static queue<int> q;
  q.push(T);
  dep[T] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = h[u], v; i; i = nxt[i])
      if (!dep[v = to[i]]) gap[dep[v] = dep[u] + 1]++, q.push(v);
  }
}
inline int dfs(int u = S, int lim = INF) {
  if (u == T) return lim;
  int ret = 0;
  for (int &i = hh[u], v; i; i = nxt[i])
    if (flow[i] && dep[v = to[i]] == dep[u] - 1) {
      int del = dfs(v, min(lim - ret, flow[i]));
      if (del) {
        flow[i] -= del;
        flow[i ^ 1] += del;
        ret += del;
        if (ret == del) return ret;
      }
    }
  if (!--gap[dep[u]]) dep[S] = T + 6;
  gap[++dep[u]]++;
  return ret;
}
inline int mf() {
  bfs();
  int ret = 0;
  while (dep[S] <= T + 5) memcpy(hh, h, sizeof h), ret += dfs();
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++)
    scanf("%d%d%d%d", &xl[i], &yl[i], &xr[i], &yr[i]),
        bx[++tx] = xl[i] - 1, bx[++tx] = xr[i], by[++ty] = yl[i] - 1,
        by[++ty] = yr[i];
  bx[++tx] = 0;
  bx[++tx] = n;
  by[++ty] = 0;
  by[++ty] = n;
  sort(bx + 1, bx + tx + 1);
  tx = unique(bx + 1, bx + tx + 1) - bx - 1;
  sort(by + 1, by + ty + 1);
  ty = unique(by + 1, by + ty + 1) - by - 1;
  T = tx + ty + 1;
  for (int i = 1; i <= m; i++) {
    xl[i] = lower_bound(bx + 1, bx + tx + 1, xl[i] - 1) - bx;
    xr[i] = lower_bound(bx + 1, bx + tx + 1, xr[i]) - bx;
    yl[i] = lower_bound(by + 1, by + ty + 1, yl[i] - 1) - by;
    yr[i] = lower_bound(by + 1, by + ty + 1, yr[i]) - by;
    for (int j = xl[i]; j < xr[i]; j++)
      for (int k = yl[i]; k < yr[i]; k++) add(j, k + tx, INF);
  }
  for (int i = 1; i < tx; i++) add(S, i, bx[i + 1] - bx[i]);
  for (int i = 1; i < ty; i++) add(i + tx, T, by[i + 1] - by[i]);
  printf("%d\n", mf());
  return 0;
}
