#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int INF = 2e9;
struct edge {
  int to, w, nxt;
} e[N << 1];
int n, tot, totx, toty, S, T;
int head[N], dep[N], xl[N], xr[N], yl[N], yr[N], tmpx[N], tmpy[N];
template <typename T>
inline void chkmax(T &x, T y) {
  x = max(x, y);
}
template <typename T>
inline void chkmin(T &x, T y) {
  x = min(x, y);
}
template <typename T>
inline void read(T &x) {
  T f = 1;
  x = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -f;
  for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
  x *= f;
}
inline void addedge(int u, int v, int w) {
  ++tot;
  e[tot] = (edge){v, w, head[u]};
  head[u] = tot;
  ++tot;
  e[tot] = (edge){u, 0, head[v]};
  head[v] = tot;
}
inline bool BFS() {
  queue<int> Q;
  while (!Q.empty()) Q.pop();
  for (int i = 1; i <= T; ++i) dep[i] = -1;
  dep[S] = 0;
  Q.push(S);
  while (!Q.empty()) {
    int cur = Q.front();
    Q.pop();
    for (int i = head[cur]; i; i = e[i].nxt) {
      int v = e[i].to, w = e[i].w;
      if (w > 0 && dep[v] == -1) {
        dep[v] = dep[cur] + 1;
        Q.push(v);
        if (v == T) return true;
      }
    }
  }
  return false;
}
inline int dinic(int u, int flow) {
  int rest = flow, k;
  if (u == T) return flow;
  for (int i = head[u]; i && rest; i = e[i].nxt) {
    int v = e[i].to, w = e[i].w;
    if (w > 0 && dep[v] == dep[u] + 1) {
      k = dinic(v, min(rest, w));
      e[i].w -= k;
      e[i ^ 1].w += k;
      rest -= k;
      if (!k) dep[v] = -1;
    }
  }
  return flow - rest;
}
int main() {
  read(n);
  read(n);
  for (int i = 1; i <= n; ++i) {
    read(xl[i]);
    read(yl[i]);
    read(xr[i]);
    read(yr[i]);
    tmpx[++totx] = xl[i];
    tmpx[++totx] = xr[i] + 1;
    tmpy[++toty] = yl[i];
    tmpy[++toty] = yr[i] + 1;
  }
  sort(tmpx + 1, tmpx + totx + 1);
  sort(tmpy + 1, tmpy + toty + 1);
  totx = unique(tmpx + 1, tmpx + totx + 1) - tmpx - 1;
  toty = unique(tmpy + 1, tmpy + toty + 1) - tmpy - 1;
  for (int i = 1; i <= n; ++i) {
    xl[i] = lower_bound(tmpx + 1, tmpx + totx + 1, xl[i]) - tmpx;
    xr[i] = lower_bound(tmpx + 1, tmpx + totx + 1, xr[i] + 1) - tmpx - 1;
    yl[i] = lower_bound(tmpy + 1, tmpy + toty + 1, yl[i]) - tmpy;
    yr[i] = lower_bound(tmpy + 1, tmpy + toty + 1, yr[i] + 1) - tmpy - 1;
  }
  --totx;
  --toty;
  S = 1, T = totx + toty + 2;
  tot = 1;
  for (int i = 1; i <= n; ++i)
    for (int j = xl[i]; j <= xr[i]; ++j)
      for (int k = yl[i]; k <= yr[i]; ++k) addedge(1 + j, 1 + totx + k, INF);
  for (int i = 1; i <= totx; ++i) addedge(S, 1 + i, tmpx[i + 1] - tmpx[i]);
  for (int i = 1; i <= toty; ++i)
    addedge(1 + totx + i, T, tmpy[i + 1] - tmpy[i]);
  int ans = 0;
  while (BFS()) {
    while (int flw = dinic(S, INF)) ans += flw;
  }
  printf("%d\n", ans);
  return 0;
}
