#include <bits/stdc++.h>
int min(int a, int b) { return a < b ? a : b; }
int *xy;
int compare(const void *a, const void *b) {
  int i = *(int *)a;
  int j = *(int *)b;
  return xy[i] - xy[j];
}
int compress(int *xx, int *hh, int *xx_, int n) {
  int h, i;
  xy = xx, qsort(hh, n, sizeof *hh, compare);
  i = 0;
  for (h = 0; h < n; h++)
    if (h + 1 == n || xx[hh[h + 1]] != xx[hh[h]]) {
      xx_[i] = xx[hh[h]];
      xx[hh[h]] = i++;
    } else
      xx[hh[h]] = i;
  return i;
}
int oo[1 + ((50 * 2 - 1) * (50 * 2 - 1) + (50 * 2 - 1) + (50 * 2 - 1)) * 2],
    oh[1 + ((50 * 2 - 1) * (50 * 2 - 1) + (50 * 2 - 1) + (50 * 2 - 1)) * 2];
int link(int o, int h) {
  static int _ = 1;
  oo[_] = o, oh[_] = h;
  return _++;
}
int ij[((50 * 2 - 1) * (50 * 2 - 1) + (50 * 2 - 1) + (50 * 2 - 1))],
    cc[((50 * 2 - 1) * (50 * 2 - 1) + (50 * 2 - 1) + (50 * 2 - 1)) * 2], m_;
int ae[((50 * 2 - 1) + (50 * 2 - 1) + 2)],
    dd[((50 * 2 - 1) + (50 * 2 - 1) + 2)], n_;
void link_(int i, int j, int c) {
  int h = m_++;
  ij[h] = i ^ j, cc[h << 1] = c;
  ae[i] = link(ae[i], h << 1);
  ae[j] = link(ae[j], h << 1 | 1);
}
int bfs(int s, int t) {
  static int qu[((50 * 2 - 1) + (50 * 2 - 1) + 2)];
  int i, head, cnt;
  for (i = 0; i < n_; i++) dd[i] = n_;
  head = cnt = 0;
  dd[s] = 0, qu[head + cnt++] = s;
  while (cnt) {
    int d, o;
    i = qu[cnt--, head++];
    d = dd[i] + 1;
    for (o = ae[i]; o; o = oo[o]) {
      int h_ = oh[o];
      if (cc[h_]) {
        int h = h_ >> 1, j = i ^ ij[h];
        if (dd[j] > d) {
          dd[j] = d;
          if (j == t) return 1;
          qu[head + cnt++] = j;
        }
      }
    }
  }
  return 0;
}
int dfs(int i, int t, int c) {
  int d, o;
  if (i == t) return c;
  d = dd[i] + 1;
  for (o = ae[i]; o; o = oo[o]) {
    int h_ = oh[o];
    if (cc[h_]) {
      int h = h_ >> 1, j = i ^ ij[h];
      if (dd[j] == d) {
        int f = dfs(j, t, min(c, cc[h_]));
        if (f) {
          cc[h_] -= f, cc[h_ ^ 1] += f;
          return f;
        }
      }
    }
  }
  dd[i] = n_;
  return 0;
}
int dinic(int s, int t) {
  int sum = 0, f;
  while (bfs(s, t))
    while ((f = dfs(s, t, 0x3f3f3f3f))) sum += f;
  return sum;
}
int main() {
  static int xx[50 * 2], yy[50 * 2], xx_[50 * 2], yy_[50 * 2], hh[50 * 2];
  static char used[(50 * 2 - 1)][(50 * 2 - 1)];
  int n, m, nx, ny, h, i, j, s, t;
  scanf("%d%d", &n, &m);
  if (m == 0) {
    printf("0\n");
    return 0;
  }
  for (h = 0; h < m; h++) {
    int l = h << 1, r = h << 1 | 1;
    scanf("%d%d%d%d", &xx[l], &yy[l], &xx[r], &yy[r]), xx[r]++, yy[r]++;
  }
  for (h = 0; h < m * 2; h++) hh[h] = h;
  nx = compress(xx, hh, xx_, m * 2) - 1;
  ny = compress(yy, hh, yy_, m * 2) - 1;
  n_ = nx + ny + 2;
  for (h = 0; h < m; h++) {
    int l = h << 1, r = h << 1 | 1;
    for (i = xx[l]; i < xx[r]; i++)
      for (j = yy[l]; j < yy[r]; j++) used[i][j] = 1;
  }
  s = nx + ny, t = s + 1;
  for (i = 0; i < nx; i++)
    for (j = 0; j < ny; j++)
      if (used[i][j]) link_(i, nx + j, 0x3f3f3f3f);
  for (i = 0; i < nx; i++) link_(s, i, xx_[i + 1] - xx_[i]);
  for (j = 0; j < ny; j++) link_(nx + j, t, yy_[j + 1] - yy_[j]);
  printf("%d\n", dinic(s, t));
  return 0;
}
