#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7f7f7f7f;
int n, m, s, t;
int cntx, ux[1005], cnty, uy[1005];
pair<pair<int, int>, pair<int, int> > a[1005];
int mxf;
int ee = 1, h[500005], nex[500005 << 1], to[500005 << 1], val[500005 << 1];
int dep[500005];
void addedge(int first, int second, int z) {
  nex[++ee] = h[first];
  to[ee] = second;
  val[ee] = z;
  h[first] = ee;
}
bool bfs() {
  memset(dep, 0, sizeof(dep));
  queue<int> q;
  dep[s] = 1;
  q.push(s);
  while (!q.empty()) {
    int first = q.front();
    q.pop();
    for (int i = h[first]; i; i = nex[i])
      if (val[i] && dep[to[i]] == 0) {
        dep[to[i]] = dep[first] + 1;
        q.push(to[i]);
      }
  }
  return dep[t] != 0;
}
int dfs(int first, int flow) {
  if (first == t) return flow;
  for (int i = h[first]; i; i = nex[i])
    if (val[i] && dep[to[i]] == dep[first] + 1) {
      int d = dfs(to[i], min(flow, val[i]));
      if (d > 0) {
        val[i] -= d;
        val[i ^ 1] += d;
        return d;
      }
    }
  return 0;
}
void dinic() {
  mxf = 0;
  while (bfs()) {
    int first = 1;
    while (first != 0) {
      first = dfs(s, INF);
      mxf += first;
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m && cin >> a[i].first.first >> a[i].first.second >>
                                a[i].second.first >> a[i].second.second;
       i++) {
    a[i].second.first++, a[i].second.second++;
    ux[++cntx] = a[i].first.first, ux[++cntx] = a[i].second.first;
    uy[++cnty] = a[i].first.second, uy[++cnty] = a[i].second.second;
  }
  ux[++cntx] = uy[++cnty] = n + 1;
  sort(ux + 1, ux + 1 + cntx), sort(uy + 1, uy + 1 + cnty);
  cntx = unique(ux + 1, ux + 1 + cntx) - ux - 1,
  cnty = unique(uy + 1, uy + 1 + cnty) - uy - 1;
  s = 0, t = cntx + cnty + 1;
  for (int i = 1; i < cntx; i++)
    addedge(s, i, ux[i + 1] - ux[i]), addedge(i, s, 0);
  for (int i = 1; i < cnty; i++)
    addedge(i + cntx, t, uy[i + 1] - uy[i]), addedge(t, i + cntx, 0);
  for (int i = 1; i <= m; i++) {
    a[i].first.first =
        lower_bound(ux + 1, ux + 1 + cntx, a[i].first.first) - ux,
    a[i].second.first =
        lower_bound(ux + 1, ux + 1 + cntx, a[i].second.first) - ux;
    a[i].first.second =
        lower_bound(uy + 1, uy + 1 + cnty, a[i].first.second) - uy,
    a[i].second.second =
        lower_bound(uy + 1, uy + 1 + cnty, a[i].second.second) - uy;
    for (int k = a[i].first.first; k < a[i].second.first; k++)
      for (int j = a[i].first.second; j < a[i].second.second; j++)
        addedge(k, j + cntx, INF), addedge(j + cntx, k, 0);
  }
  dinic();
  cout << mxf << endl;
  return 0;
}
