#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long ux[500 << 2], uy[500 << 2];
long long uxn, uyn;
long long s, t, ans;
long long d[500], l, r, q[500];
long long cnt = 1, hd[500];
pair<pair<long long, long long>, pair<long long, long long> > a[500];
struct Node {
  long long nx, to, dis;
} e[500 * 500 * 4];
inline long long read() {
  long long xx = 0, f = 0;
  char c = getchar();
  while (!isdigit(c)) f |= c == '-', c = getchar();
  while (isdigit(c)) xx = (xx << 1) + (xx << 3) + (c ^ 48), c = getchar();
  return f ? -xx : xx;
}
inline long long mn(long long u, long long v) { return u < v ? u : v; }
inline void add(long long u, long long v, long long w) {
  e[++cnt].to = v;
  e[cnt].nx = hd[u];
  e[cnt].dis = w;
  hd[u] = cnt;
}
inline long long bfs() {
  memset(d, 0, sizeof(d));
  d[s] = 1;
  q[l = r = 1] = s;
  while (l <= r) {
    long long u = q[l++];
    for (register int i = hd[u]; i; i = e[i].nx) {
      long long to = e[i].to;
      if (e[i].dis && !d[to]) q[++r] = to, d[to] = d[u] + 1;
    }
  }
  return d[t];
}
inline long long dfs(long long u, long long limit) {
  if (u == t) return limit;
  long long flow = 0;
  for (register int i = hd[u]; i && limit; i = e[i].nx) {
    long long to = e[i].to;
    if (d[to] == d[u] + 1 && e[i].dis) {
      long long fj = dfs(to, mn(limit, e[i].dis));
      e[i].dis -= fj;
      e[i ^ 1].dis += fj;
      flow += fj;
      limit -= fj;
    }
  }
  if (!flow) d[u] = 0;
  return flow;
}
int main() {
  n = read();
  m = read();
  for (register int i = 1; i <= m; ++i) {
    a[i].first.first = read();
    a[i].first.second = read();
    a[i].second.first = read();
    a[i].second.second = read();
    ++a[i].second.first;
    ++a[i].second.second;
    ux[++uxn] = a[i].first.first;
    uy[++uyn] = a[i].first.second;
    ux[++uxn] = a[i].second.first;
    uy[++uyn] = a[i].second.second;
  }
  ux[++uxn] = n + 1, uy[++uyn] = n + 1;
  sort(ux + 1, ux + uxn + 1);
  sort(uy + 1, uy + uyn + 1);
  uxn = unique(ux + 1, ux + uxn + 1) - ux - 1;
  uyn = unique(uy + 1, uy + uyn + 1) - uy - 1;
  s = 0, t = uxn + uyn + 1;
  for (register int i = 1; i <= m; ++i) {
    a[i].first.first = lower_bound(ux + 1, ux + uxn + 1, a[i].first.first) - ux;
    a[i].first.second =
        lower_bound(uy + 1, uy + uyn + 1, a[i].first.second) - uy;
    a[i].second.first =
        lower_bound(ux + 1, ux + uxn + 1, a[i].second.first) - ux;
    a[i].second.second =
        lower_bound(uy + 1, uy + uyn + 1, a[i].second.second) - uy;
    for (register int j = a[i].first.first; j < a[i].second.first; ++j)
      for (register int k = a[i].first.second; k < a[i].second.second; ++k)
        add(j, k + uxn, 1000000000000), add(k + uxn, j, 0);
  }
  for (register int i = 1; i < uxn; ++i)
    add(s, i, ux[i + 1] - ux[i]), add(i, s, 0);
  for (register int i = 1; i < uyn; ++i)
    add(i + uxn, t, uy[i + 1] - uy[i]), add(t, i + uxn, 0);
  while (bfs()) ans += dfs(s, 1000000000000);
  printf("%lld\n", ans);
  return 0;
}
