#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 105;
namespace Dinic {
const int MAXN = 50000 + 7, MAXM = 50000 + 7, INF = 0x7fffffff;
struct edge {
  int to, next, val;
} e[MAXM * 2];
int n, head[MAXN], cnt = 1, S, T, dis[MAXN], d[MAXN], cur[MAXN];
bool b[MAXN];
void init(int x) {
  n = x;
  memset(head, 0, (n + 3) * sizeof(int));
  cnt = 1;
}
void addedge(int x, int y, int z) {
  e[++cnt].to = y;
  e[cnt].val = z;
  e[cnt].next = head[x];
  head[x] = cnt;
  e[++cnt].to = x;
  e[cnt].val = 0;
  e[cnt].next = head[y];
  head[y] = cnt;
}
bool bfs() {
  memset(b, false, (n + 3) * sizeof(bool));
  int he = 0, en = 1;
  dis[S] = 1;
  b[S] = true;
  d[1] = S;
  while (he != en) {
    int x = d[++he];
    if (x == T) break;
    cur[x] = head[x];
    for (int i = head[x]; i; i = e[i].next)
      if (!b[e[i].to] && e[i].val) {
        d[++en] = e[i].to;
        b[e[i].to] = true;
        dis[e[i].to] = dis[x] + 1;
      }
  }
  return b[T];
}
int dfs(int x, int flow) {
  if (x == T) return flow;
  int l = flow;
  for (int &i = cur[x]; i; i = e[i].next)
    if (e[i].val && dis[x] + 1 == dis[e[i].to]) {
      int t = dfs(e[i].to, min(l, e[i].val));
      e[i].val -= t;
      e[i ^ 1].val += t;
      l -= t;
      if (l == 0) break;
    }
  return flow - l;
}
int dinic(int st, int en) {
  S = st;
  T = en;
  int ans = 0;
  while (bfs()) {
    ans += dfs(S, INF);
  }
  return ans;
}
}  // namespace Dinic
int n, m, A, B, setx[MAX_N], sety[MAX_N], col[MAX_N][MAX_N];
struct Rectangle {
  int x1, y1, x2, y2;
  Rectangle() {}
  Rectangle(int _x1, int _y1, int _x2, int _y2)
      : x1(_x1), y1(_y1), x2(_x2), y2(_y2) {}
} a[MAX_N];
map<int, int> mapx, mapy;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    a[i] = Rectangle(x1, y1, x2, y2);
    mapx[x1] = mapx[x2 + 1] = mapy[y1] = mapy[y2 + 1] = 0;
  }
  for (auto &p : mapx) {
    p.second = ++A;
    setx[A] = p.first;
  }
  for (auto &p : mapy) {
    p.second = ++B;
    sety[B] = p.first;
  }
  for (int p = 1; p <= m; ++p) {
    int x1, y1, x2, y2;
    x1 = mapx[a[p].x1];
    x2 = mapx[a[p].x2 + 1];
    y1 = mapy[a[p].y1];
    y2 = mapy[a[p].y2 + 1];
    for (int i = x1; i < x2; ++i) {
      for (int j = y1; j < y2; ++j) {
        col[i][j] = 1;
      }
    }
  }
  Dinic::init(A + B + 2);
  for (int i = 1; i < A; ++i) {
    Dinic::addedge(0, i + 1, setx[i + 1] - setx[i]);
  }
  for (int j = 1; j < B; ++j) {
    Dinic::addedge(j + A + 1, A + B + 2, sety[j + 1] - sety[j]);
  }
  for (int i = 1; i < A; ++i) {
    for (int j = 1; j < B; ++j) {
      if (col[i][j]) {
        Dinic::addedge(i + 1, j + A + 1, Dinic::INF);
      }
    }
  }
  printf("%d\n", Dinic::dinic(0, A + B + 2));
}
