#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[53], b[53], c[53], d[53];
int mpx[103], cntx, mpy[103], cnty;
int g[102][102], w[102][102];
void init(void) {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d %d %d", a + i, b + i, c + i, d + i);
    mpx[i] = a[i], mpx[i + m] = ++c[i];
    mpy[i] = b[i], mpy[i + m] = ++d[i];
  }
  sort(mpx + 1, mpx + 1 + 2 * m);
  sort(mpy + 1, mpy + 1 + 2 * m);
  cntx = unique(mpx + 1, mpx + 1 + 2 * m) - mpx - 1;
  cnty = unique(mpy + 1, mpy + 1 + 2 * m) - mpy - 1;
  for (int i = 1; i <= m; i++) {
    a[i] = lower_bound(mpx + 1, mpx + 1 + cntx, a[i]) - mpx;
    b[i] = lower_bound(mpy + 1, mpy + 1 + cnty, b[i]) - mpy;
    c[i] = lower_bound(mpx + 1, mpx + 1 + cntx, c[i]) - mpx;
    d[i] = lower_bound(mpy + 1, mpy + 1 + cnty, d[i]) - mpy;
    g[a[i]][b[i]]++, g[c[i]][d[i]]++;
    g[a[i]][d[i]]--, g[c[i]][b[i]]--;
  }
  for (int i = 1; i <= cntx; i++)
    for (int j = 1; j <= cnty; j++)
      g[i][j] += g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1];
}
int head[205], to[20410], nxt[20410], flow[20410], cnt = 1;
void add(int u, int v, int f) {
  to[++cnt] = v, nxt[cnt] = head[u], flow[cnt] = f;
  head[u] = cnt;
}
void addedge(int u, int v, int f) { add(u, v, f), add(v, u, 0); }
int S, T;
void make_graph(void) {
  S = cntx + cnty + 1, T = cntx + cnty + 2;
  for (int i = 1; i < cntx; i++) addedge(S, i, mpx[i + 1] - mpx[i]);
  for (int i = 1; i < cnty; i++) addedge(i + cntx, T, mpy[i + 1] - mpy[i]);
  for (int i = 1; i < cntx; i++)
    for (int j = 1; j < cnty; j++)
      if (g[i][j]) addedge(i, j + cntx, 2147483647);
}
int deep[205];
queue<int> q;
bool bfs() {
  memset(deep + 1, 0, T * sizeof(int));
  q.push(S);
  deep[S] = 1;
  do {
    int x = q.front();
    q.pop();
    for (int i = head[x]; i; i = nxt[i]) {
      int &y = to[i];
      if (!deep[y] && flow[i]) {
        deep[y] = deep[x] + 1;
        q.push(y);
      }
    }
  } while (!q.empty());
  return deep[T] != 0;
}
int dfs(int x, int f) {
  if (x == T) return f;
  int ans = 0;
  for (int i = head[x]; i; i = nxt[i]) {
    int &y = to[i];
    if (flow[i] && deep[y] == deep[x] + 1) {
      int more = dfs(y, min(flow[i], f));
      if (!more) {
        deep[y] = -1;
        continue;
      }
      flow[i] -= more, flow[i ^ 1] += more;
      ans += more, f -= more;
      if (!f) break;
    }
  }
  return ans;
}
int dinic() {
  int ans = 0;
  while (bfs()) ans += dfs(S, 2147483647);
  return ans;
}
int main() {
  init();
  make_graph();
  cout << dinic() << endl;
}
