#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long _INF = 0xc0c0c0c0c0c0c0c0;
const long long mod = (int)1e9 + 7;
const int N = 20010;
const int M = N * 8;
int x[N], y[N];
int x1[N], _y1[N], x2[N], y2[N];
int head[N], deep[N], cur[N];
int w[M], to[M], nx[M];
int tot;
void add(int u, int v, int val) {
  w[tot] = val;
  to[tot] = v;
  nx[tot] = head[u];
  head[u] = tot++;
  w[tot] = 0;
  to[tot] = u;
  nx[tot] = head[v];
  head[v] = tot++;
}
int bfs(int s, int t) {
  queue<int> q;
  memset(deep, 0, sizeof(deep));
  q.push(s);
  deep[s] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; ~i; i = nx[i]) {
      if (w[i] > 0 && deep[to[i]] == 0) {
        deep[to[i]] = deep[u] + 1;
        q.push(to[i]);
      }
    }
  }
  return deep[t] > 0;
}
int Dfs(int u, int t, int flow) {
  if (u == t) return flow;
  for (int &i = cur[u]; ~i; i = nx[i]) {
    if (deep[u] + 1 == deep[to[i]] && w[i] > 0) {
      int di = Dfs(to[i], t, min(w[i], flow));
      if (di > 0) {
        w[i] -= di, w[i ^ 1] += di;
        return di;
      }
    }
  }
  return 0;
}
int Dinic(int s, int t) {
  int ans = 0, tmp;
  while (bfs(s, t)) {
    for (int i = 0; i <= t; i++) cur[i] = head[i];
    while (tmp = Dfs(s, t, inf)) ans += tmp;
  }
  return ans;
}
void init() {
  memset(head, -1, sizeof(head));
  tot = 0;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  if (!m) return 0 * puts("0");
  init();
  int tx = 0, ty = 0;
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d%d", &x1[i], &_y1[i], &x2[i], &y2[i]);
    x[tx++] = x1[i];
    x[tx++] = x2[i] + 1;
    y[ty++] = _y1[i];
    y[ty++] = y2[i] + 1;
  }
  sort(x, x + tx);
  sort(y, y + ty);
  tx = unique(x, x + tx) - x - 1;
  ty = unique(y, y + ty) - y - 1;
  for (int i = 1; i <= m; ++i)
    for (int j = 1; j <= tx; ++j)
      for (int k = 1; k <= ty; ++k)
        if (x1[i] <= x[j - 1] && x[j] <= x2[i] + 1)
          if (_y1[i] <= y[k - 1] && y[k] <= y2[i] + 1) add(j, k + tx, inf);
  int s = 0, t = tx + ty + 1;
  for (int i = 1; i <= tx; ++i) add(s, i, x[i] - x[i - 1]);
  for (int i = 1; i <= ty; ++i) add(i + tx, t, y[i] - y[i - 1]);
  cout << Dinic(s, t) << endl;
  return 0;
}
