#include <bits/stdc++.h>
using namespace std;
const int N = 250;
const int maxn = (int)5e5 + 9;
const int inf = (int)1e9 + 100;
int X[N], Y[N], cnt, invx[N], invy[N], R, C, p, s, t, head[N], n, m, vis[N],
    mp[N][N];
struct edge {
  int u, c, nxt;
} e[maxn];
void addedge(int u, int v, int c) {
  e[p] = (edge){v, c, head[u]}, head[u] = p++;
  e[p] = (edge){u, 0, head[v]}, head[v] = p++;
}
set<int> S;
map<int, int> id;
bool bfs() {
  for (int i = (1); i < (t + 2); i++) vis[i] = -1;
  queue<int> q;
  q.push(s);
  vis[s] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; ~i; i = e[i].nxt) {
      int w = e[i].u;
      if (vis[w] == -1 && e[i].c > 0) {
        vis[w] = vis[u] + 1;
        q.push(w);
      }
    }
  }
  if (vis[t] == -1) return 0;
  return 1;
}
int findf(int x, int flow) {
  if (x == t) return flow;
  int f;
  for (int i = head[x]; ~i; i = e[i].nxt) {
    int w = e[i].u;
    if (vis[w] == vis[x] + 1 && e[i].c > 0 &&
        (f = findf(w, min(flow, e[i].c)))) {
      e[i].c -= f;
      e[i ^ 1].c += f;
      return f;
    }
  }
  return 0;
}
int dinic() {
  int ans = 0;
  while (bfs()) {
    int val = findf(s, inf);
    ans += val;
  }
  return ans;
}
int main() {
  memset(head, -1, sizeof(head));
  scanf("%d%d", &m, &n);
  if (n == 0) {
    cout << 0;
    return 0;
  }
  for (int i = (0); i < (2 * n); i++) scanf("%d%d", &X[i], &Y[i]);
  for (int i = (0); i < (n); i++) X[i << 1]--, Y[i << 1]--;
  cnt = 0;
  S.clear();
  id.clear();
  for (int i = (0); i < (2 * n); i++)
    if (S.find(X[i]) == S.end()) S.insert(X[i]);
  for (auto x : S) id[x] = ++cnt, invx[cnt] = x;
  for (int i = (0); i < (2 * n); i++) X[i] = id[X[i]];
  R = cnt - 1;
  cnt = 0;
  S.clear();
  id.clear();
  for (int i = (0); i < (2 * n); i++)
    if (S.find(Y[i]) == S.end()) S.insert(Y[i]);
  for (auto x : S) id[x] = ++cnt, invy[cnt] = x;
  for (int i = (0); i < (2 * n); i++) Y[i] = id[Y[i]];
  C = cnt - 1;
  s = R + C + 1, t = R + C + 2;
  cnt = 0;
  for (int i = (1); i < (R + 1); i++) addedge(s, i, invx[i + 1] - invx[i]);
  for (int i = (1); i < (C + 1); i++) addedge(i + R, t, invy[i + 1] - invy[i]);
  for (int i = (0); i < (n); i++)
    for (int x = (X[i << 1]); x < (X[i << 1 | 1]); x++)
      for (int y = (Y[i << 1]); y < (Y[i << 1 | 1]); y++) mp[x][y] = 1;
  for (int x = (1); x < (R + 1); x++)
    for (int y = (1); y < (C + 1); y++)
      if (mp[x][y]) addedge(x, y + R, inf);
  printf("%d", dinic());
}
