#include <bits/stdc++.h>
using namespace std;
const int maxn = 500500;
const int inf = 1e9;
struct edge {
  int obj, pre, c;
} e[maxn * 2];
int n, m, cntx, cnty, tot, s, t;
int head[maxn], cur[maxn], uu[maxn], cx[maxn], cy[maxn];
struct node {
  int a, b, c, d;
  node() {}
  void read() { scanf("%d%d%d%d", &a, &b, &c, &d); }
} a[maxn];
void insert(int x, int y, int c) {
  e[++tot].obj = y;
  e[tot].pre = head[x];
  head[x] = tot;
  e[tot].c = c;
  e[++tot].obj = x;
  e[tot].pre = head[y];
  head[y] = tot;
  e[tot].c = 0;
}
bool bfs() {
  for (int i = 0; i <= t; i++) uu[i] = -1;
  queue<int> q;
  q.push(0);
  uu[0] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int j = head[u]; j; j = e[j].pre) {
      int v = e[j].obj;
      if (uu[v] == -1 && e[j].c) uu[v] = uu[u] + 1, q.push(v);
    }
  }
  if (uu[t] == -1)
    return 0;
  else
    return 1;
}
int dfs(int x, int mx) {
  if (x == t) return mx;
  int used = 0;
  for (int j = cur[x]; j; j = e[j].pre) {
    int v = e[j].obj;
    if (uu[v] == uu[x] + 1 && e[j].c) {
      int w = dfs(v, min(e[j].c, mx - used));
      e[j].c -= w;
      e[j ^ 1].c += w;
      used += w;
      if (e[j].c) cur[x] = j;
      if (used == mx) return mx;
    }
  }
  if (!used) uu[x] = -1;
  return used;
}
int dinic() {
  int ans = 0;
  while (bfs()) {
    for (int i = 0; i <= t; i++) cur[i] = head[i];
    ans += dfs(0, inf);
  }
  return ans;
}
bool jud(int a, int b, int c, int d) {
  for (int i = 1; i <= m; i++) {
    node &p = ::a[i];
    if (p.a <= a && p.b <= b && c <= p.c && d <= p.d) {
      return 1;
    }
  }
  return 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    a[i].read();
    cx[++cntx] = a[i].a;
    cx[++cntx] = a[i].c + 1;
    cy[++cnty] = a[i].b;
    cy[++cnty] = a[i].d + 1;
  }
  sort(cx + 1, cx + 1 + cntx);
  cntx = unique(cx + 1, cx + 1 + cntx) - cx - 1;
  sort(cy + 1, cy + 1 + cnty);
  cnty = unique(cy + 1, cy + 1 + cnty) - cy - 1;
  s = 0;
  t = cntx + cnty + 1;
  tot = 1;
  for (int i = 1; i <= cntx - 1; i++) {
    insert(s, i, cx[i + 1] - cx[i]);
  }
  for (int i = 1; i <= cnty - 1; i++) {
    insert(cntx + i, t, cy[i + 1] - cy[i]);
  }
  for (int i = 1; i <= cntx - 1; i++) {
    int a = cx[i], c = cx[i + 1];
    for (int j = 1; j <= cnty - 1; j++) {
      int b = cy[j], d = cy[j + 1];
      if (jud(a, b, c - 1, d - 1)) insert(i, cntx + j, inf);
    }
  }
  printf("%d\n", dinic());
  return 0;
}
