#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxm = 600 + 10;
const int maxn = 610 * 610 * 4;
const int inf = 0x3f3f3f3f;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
                                       qu[55];
int f, qr;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char x) {
  *oS++ = x;
  if (oS == oT) flush();
}
template <typename A>
inline bool read(A &x) {
  for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-')
      f = -1;
    else if (c == EOF)
      return 0;
  for (x = 0; c <= '9' && c >= '0';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    x = x * 10 + (c & 15);
  x *= f;
  return 1;
}
inline bool read(char &x) {
  while ((x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                           (iS == iT ? EOF : *iS++))
                        : *iS++)) == ' ' ||
         x == '\n' || x == '\r')
    ;
  return x != EOF;
}
inline bool read(char *x) {
  while ((*x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                            (iS == iT ? EOF : *iS++))
                         : *iS++)) == '\n' ||
         *x == ' ' || *x == '\r')
    ;
  if (*x == EOF) return 0;
  while (!(*x == '\n' || *x == ' ' || *x == '\r' || *x == EOF))
    *(++x) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                          (iS == iT ? EOF : *iS++))
                       : *iS++);
  *x = 0;
  return 1;
}
template <typename A, typename... B>
inline bool read(A &x, B &...y) {
  return read(x) && read(y...);
}
template <typename A>
inline bool write(A x) {
  if (!x) putc('0');
  if (x < 0) putc('-'), x = -x;
  while (x) qu[++qr] = x % 10 + '0', x /= 10;
  while (qr) putc(qu[qr--]);
  return 0;
}
inline bool write(char x) {
  putc(x);
  return 0;
}
inline bool write(const char *x) {
  while (*x) {
    putc(*x);
    ++x;
  }
  return 0;
}
inline bool write(char *x) {
  while (*x) {
    putc(*x);
    ++x;
  }
  return 0;
}
template <typename A, typename... B>
inline bool write(A x, B... y) {
  return write(x) || write(y...);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher_;
}  // namespace io
using io ::putc;
using io ::read;
using io ::write;
int row[maxm], col[maxm];
struct rec {
  int x1, y1, x2, y2;
} a[maxm];
int s, t;
int now[maxm * 2], dep[maxm * 2], head[maxm * 2];
struct edge {
  int v, w, nxt;
} e[maxn];
int ecnt;
void addedge(int u, int v, int w) {
  e[ecnt] = {v, w, head[u]};
  head[u] = ecnt++;
  e[ecnt] = {u, 0, head[v]};
  head[v] = ecnt++;
}
int bfs() {
  queue<int> q;
  while (!q.empty()) q.pop();
  for (int i = s; i <= t; i++) {
    dep[i] = -1;
    now[i] = -1;
  }
  dep[s] = 0;
  q.push(s);
  now[s] = head[s];
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i != -1; i = e[i].nxt) {
      int v = e[i].v;
      if (e[i].w > 0 && dep[v] == -1) {
        dep[v] = dep[u] + 1;
        now[v] = head[v];
        q.push(v);
      }
    }
  }
  return dep[t] != -1;
}
int dfs(int u, int mi) {
  if (mi == 0 || u == t) return mi;
  int tmp, sum = 0;
  for (int i = now[u]; i != -1 && mi; i = e[i].nxt) {
    now[u] = i;
    int v = e[i].v;
    if (e[i].w > 0 && dep[v] == dep[u] + 1) {
      tmp = dfs(v, min(mi, e[i].w));
      e[i].w -= tmp;
      e[i ^ 1].w += tmp;
      sum += tmp;
      mi -= tmp;
      if (!mi) break;
    }
  }
  return sum;
}
int dinic() {
  int ans = 0;
  while (bfs()) {
    ans += dfs(s, inf);
  }
  return ans;
}
int main() {
  memset(head, -1, sizeof(head));
  int n, m;
  read(n, m);
  int sz1 = 0, sz2 = 0;
  for (int i = 1; i <= m; i++) {
    int x1, y1, x2, y2;
    read(x1, y1, x2, y2);
    a[i] = {x1, y1, x2, y2};
    row[++sz1] = x1;
    row[++sz1] = x2;
    col[++sz2] = y1;
    col[++sz2] = y2;
  }
  if (m == 0) {
    printf("0\n");
    return 0;
  }
  sort(row + 1, row + sz1 + 1);
  sort(col + 1, col + sz2 + 1);
  sz1 = unique(row + 1, row + sz1 + 1) - row - 1;
  sz2 = unique(col + 1, col + sz2 + 1) - col - 1;
  s = 0, t = sz1 + sz1 - 1 + sz2 + sz2 - 1 + 1;
  int N = sz1 + sz1 - 1;
  addedge(s, 1, 1);
  for (int i = 2; i <= sz1; i++) {
    addedge(s, i, 1);
    addedge(s, sz1 + i - 1, row[i] - row[i - 1] - 1);
  }
  addedge(N + 1, t, 1);
  for (int i = 2; i <= sz2; i++) {
    addedge(N + i, t, 1);
    addedge(N + sz2 + i - 1, t, col[i] - col[i - 1] - 1);
  }
  for (int i = 1; i <= m; i++) {
    int x1 = lower_bound(row + 1, row + sz1 + 1, a[i].x1) - row;
    int y1 = lower_bound(col + 1, col + sz2 + 1, a[i].y1) - col;
    int x2 = lower_bound(row + 1, row + sz1 + 1, a[i].x2) - row;
    int y2 = lower_bound(col + 1, col + sz2 + 1, a[i].y2) - col;
    for (int j = x1; j <= x2; j++) {
      for (int k = y1; k <= y2; k++) {
        addedge(j, k + N, inf);
        if (j > x1) addedge(sz1 + j - 1, k + N, inf);
        if (k > y1) addedge(j, N + sz2 + k - 1, inf);
        if (j > x1 && k > y1) addedge(sz1 + j - 1, N + sz2 + k - 1, inf);
      }
    }
  }
  int ans = dinic();
  printf("%d\n", ans);
  return 0;
}
