#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1005;
const long long maxm = 30005;
struct Dinic {
  struct edge {
    long long to, next, cap, flow;
  } es[maxm];
  long long d[maxn], cur[maxn], s, t, n;
  long long head[maxn], tol;
  queue<long long> q;
  void init() {
    memset(head, -1, sizeof(head));
    tol = 0;
  }
  inline void adde(long long u, long long v, long long w) {
    es[tol].to = v;
    es[tol].cap = w;
    es[tol].flow = 0;
    es[tol].next = head[u];
    head[u] = tol++;
    es[tol].to = u;
    es[tol].cap = es[tol].flow = 0;
    es[tol].next = head[v];
    head[v] = tol++;
  }
  bool bfs() {
    for (long long i = 0; i <= n; i++) {
      d[i] = -1;
    }
    d[s] = 0;
    q.push(s);
    while (!q.empty()) {
      long long u = q.front();
      q.pop();
      for (long long i = head[u]; i != -1; i = es[i].next) {
        long long v = es[i].to;
        if (d[v] == -1 && es[i].cap > es[i].flow) {
          d[v] = d[u] + 1;
          q.push(v);
        }
      }
    }
    return d[t] != -1;
  }
  long long dfs(long long u, long long low) {
    if (u == t || !low) return low;
    long long f = 0;
    for (long long &i = cur[u]; i != -1; i = es[i].next) {
      long long v = es[i].to, tmp = 0;
      if (d[u] + 1 == d[v] &&
          (tmp = dfs(v, min(low, es[i].cap - es[i].flow)))) {
        es[i].flow += tmp;
        es[i ^ 1].flow -= tmp;
        f += tmp;
        low -= tmp;
        if (!low) break;
      }
    }
    return f;
  }
  long long Maxflow(long long s, long long t, long long n) {
    this->s = s, this->t = t, this->n = n;
    long long ans = 0;
    while (bfs()) {
      for (long long i = 0; i < n; i++) cur[i] = head[i];
      ans += dfs(s, 1e9);
    }
    return ans;
  }
};
long long g[105][105];
vector<long long> vx, vy;
long long x1[55], yy1[55], x2[55], yy2[55];
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  for (long long i = 1; i <= m; ++i) {
    cin >> x1[i] >> yy1[i] >> x2[i] >> yy2[i];
    vx.push_back(x1[i] - 1), vx.push_back(x2[i]);
    vy.push_back(yy1[i] - 1), vy.push_back(yy2[i]);
  }
  sort(vx.begin(), vx.end()), sort(vy.begin(), vy.end());
  long long nx = unique(vx.begin(), vx.end()) - vx.begin(),
            ny = unique(vy.begin(), vy.end()) - vy.begin();
  for (long long i = 1; i <= m; ++i) {
    x1[i] = lower_bound(vx.begin(), vx.begin() + nx, x1[i]) - vx.begin() + 1;
    x2[i] = lower_bound(vx.begin(), vx.begin() + nx, x2[i]) - vx.begin() + 1;
    yy1[i] = lower_bound(vy.begin(), vy.begin() + ny, yy1[i]) - vy.begin() + 1;
    yy2[i] = lower_bound(vy.begin(), vy.begin() + ny, yy2[i]) - vy.begin() + 1;
    for (long long x = x1[i]; x <= x2[i]; ++x)
      for (long long y = yy1[i]; y <= yy2[i]; ++y) g[x - 1][y - 1] = 1;
  }
  Dinic d;
  d.init();
  long long s = nx + ny + 1, t = nx + ny + 2;
  for (long long i = 1; i < nx; ++i) d.adde(s, i, vx[i] - vx[i - 1]);
  for (long long i = 1; i < ny; ++i) d.adde(i + nx, t, vy[i] - vy[i - 1]);
  for (long long i = 1; i < nx; ++i)
    for (long long j = 1; j < ny; ++j) {
      if (g[i][j]) d.adde(i, nx + j, 1e9);
    }
  cout << d.Maxflow(s, t, t) << '\n';
}
