#include <bits/stdc++.h>
int N, u[50], d[50], l[50], r[50], Hx[102], Hy[102], Lx, Ly;
template <int xn, int xe>
struct flow {
  int head[xn], next[xe], to[xe], fl[xe], N, E, dep[xn], q[xn + 1];
  inline void add(int u, int v, int f) {
    next[E] = head[u], to[E] = v, fl[E] = f, head[u] = E++;
    next[E] = head[v], to[E] = u, fl[E] = 0, head[v] = E++;
  }
  void clear(int n) {
    N = n, E = 0;
    for (int i = 0; i < N; i++) head[i] = -1;
  }
  bool BFS() {
    for (int i = 0; i < N; i++) dep[i] = 0;
    dep[0] = 1;
    q[1] = 0;
    int H = 0, T = 1, u;
    while (H < T)
      for (int e = head[u = q[++H]]; ~e; e = next[e])
        if (fl[e] && !dep[to[e]]) {
          dep[q[++T] = to[e]] = dep[u] + 1;
          if (to[e] == N - 1) return true;
        }
    return false;
  }
  int DINIC(int p, int f) {
    if (p == N - 1) return f;
    int u = 0, y;
    for (int e = head[p]; ~e; e = next[e])
      if (fl[e] && dep[to[e]] == dep[p] + 1 &&
          (y = DINIC(to[e], std::min(f, fl[e])))) {
        u += y;
        f -= y;
        fl[e] -= y;
        fl[e ^ 1] += y;
        if (f == 0) return u;
      }
    if (u == 0) dep[p] = 0;
    return u;
  }
  int operator()() {
    int O = 0;
    while (BFS()) O += DINIC(0, 1000000000);
    return O;
  }
};
flow<666, 233333> G;
int main() {
  scanf("%*d%d", &N);
  if (N == 0) {
    puts("0");
    return 0;
  }
  for (int i = 0; i < N; i++) {
    scanf("%d%d%d%d", u + i, l + i, d + i, r + i);
    Hx[Lx++] = --u[i];
    Hx[Lx++] = d[i];
    Hy[Ly++] = --l[i];
    Hy[Ly++] = r[i];
  }
  std::sort(Hx, Hx + Lx);
  Lx = std::unique(Hx, Hx + Lx) - Hx;
  std::sort(Hy, Hy + Ly);
  Ly = std::unique(Hy, Hy + Ly) - Hy;
  G.clear(1 + (Lx - 1) + (Ly - 1) + N + 1);
  for (int i = 1; i < Lx; i++) G.add(0, i, Hx[i] - Hx[i - 1]);
  for (int i = 1; i < Ly; i++) G.add(Lx - 1 + i, G.N - 1, Hy[i] - Hy[i - 1]);
  for (int i = 0; i < N; i++) {
    u[i] = std::lower_bound(Hx, Hx + Lx, u[i]) - Hx;
    d[i] = std::lower_bound(Hx, Hx + Lx, d[i]) - Hx;
    l[i] = std::lower_bound(Hy, Hy + Ly, l[i]) - Hy;
    r[i] = std::lower_bound(Hy, Hy + Ly, r[i]) - Hy;
    for (int j = u[i] + 1; j <= d[i]; j++)
      G.add(j, Lx + Ly - 1 + i, 1000000000);
    for (int j = l[i] + 1; j <= r[i]; j++)
      G.add(Lx + Ly - 1 + i, Lx - 1 + j, 1000000000);
  }
  printf("%d\n", G());
  return 0;
}
