#include <bits/stdc++.h>
using namespace std;
template <class T>
void uniquify(vector<T> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}
const int M = 207, INF = 1e9;
int n, m, rect[M][4], sum[M][M], cap[M][M], src, snk, dist[M], nbr[M];
vector<int> xs, ys;
bool vis[M];
bool explore(int s, int t) {
  queue<int> q;
  fill_n(vis, n, 0);
  vis[s] = true;
  dist[s] = 0;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v = 0; v < n; v++)
      if (cap[u][v] && !vis[v]) {
        vis[v] = true;
        dist[v] = dist[u] + 1;
        q.push(v);
      }
  }
  return vis[t];
}
int augment(int u, int t, int f = INF) {
  if (u == t) {
    return f;
  }
  for (; nbr[u] < n; ++nbr[u]) {
    int v = nbr[u];
    if (cap[u][v] && dist[u] + 1 == dist[v]) {
      int go = augment(v, t, min(f, cap[u][v]));
      if (go > 0) {
        cap[u][v] -= go;
        cap[v][u] += go;
        return go;
      }
    }
  }
  return 0;
}
int dinic(int s, int t) {
  int flow, sum = 0;
  while (explore(s, t)) {
    fill_n(nbr, n, 0);
    while (flow = augment(s, t)) {
      sum += flow;
    }
  }
  return sum;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < 4; j++) {
      cin >> rect[i][j];
      if (j < 2) {
        rect[i][j]--;
      }
      xs.push_back(rect[i][j]);
      xs.swap(ys);
    }
  }
  uniquify(xs);
  uniquify(ys);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < 4; j++) {
      rect[i][j] = lower_bound(xs.begin(), xs.end(), rect[i][j]) - xs.begin();
      xs.swap(ys);
    }
    auto r = rect[i];
    int a = r[0], b = r[1], c = r[2], d = r[3];
    sum[a][b]++;
    sum[c][d]++;
    sum[a][d]--;
    sum[c][b]--;
  }
  for (int i = 0; i < xs.size(); i++) {
    for (int j = 0; j < ys.size(); j++) {
      if (i) sum[i][j] += sum[i - 1][j];
      if (j) sum[i][j] += sum[i][j - 1];
      if (i && j) sum[i][j] -= sum[i - 1][j - 1];
    }
  }
  for (int i = 0; i < xs.size(); i++) {
    for (int j = 0; j < ys.size(); j++) {
      if (sum[i][j]) {
        cap[i][xs.size() - 1 + j] = INF;
      }
    }
  }
  src = xs.size() + ys.size() - 2;
  snk = src + 1;
  n = snk + 1;
  for (int i = 0; i + 1 < xs.size(); i++) {
    cap[src][i] = xs[i + 1] - xs[i];
  }
  for (int j = 0; j + 1 < ys.size(); j++) {
    cap[xs.size() - 1 + j][snk] = ys[j + 1] - ys[j];
  }
  cout << dinic(src, snk) << endl;
}
