#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using PII = pair<int, int>;
template <typename T>
struct MaxFlow {
  struct Edge {
    int u, v;
    T c, f;
  };
  const T eps = (T)1e-9;
  vector<Edge> E;
  int n, src, sink;
  vector<vector<int>> G;
  vector<int> P, D;
  T flow = T();
  MaxFlow(int n, int src, int sink)
      : n(n), src(src), sink(sink), G(n), P(n), D(n) {}
  void clear_flow() {
    for (auto& e : E) e.f = T();
    flow = T();
  }
  void reduce() {
    for (auto& e : E) e.c -= e.f;
  }
  void add_edge(int u, int v, T cap) {
    assert(0 <= u && u < n && 0 <= v && v < n);
    G[u].push_back(E.size()), E.push_back({u, v, cap, T()});
    G[v].push_back(E.size()), E.push_back({v, u, T(), T()});
  }
  bool expath() {
    fill(D.begin(), D.end(), -1);
    queue<int> Q;
    Q.push(sink), D[sink] = 0;
    while (!Q.empty()) {
      int u = Q.front();
      Q.pop();
      for (int id : G[u]) {
        const Edge &e = E[id], &b = E[id ^ 1];
        if (b.c - b.f > eps && D[e.v] == -1) {
          D[e.v] = D[e.u] + 1, Q.push(e.v);
          if (e.v == src) return true;
        }
      }
    }
    return false;
  }
  T dfs(int u, T w) {
    if (u == sink) return w;
    for (int& i = P[u]; i < int(G[u].size()); i++) {
      int id = G[u][i];
      const Edge& e = E[id];
      if (e.c - e.f > eps && D[e.v] == D[e.u] - 1) {
        T t = dfs(e.v, min(e.c - e.f, w));
        if (t > eps) return E[id].f += t, E[id ^ 1].f -= t, t;
      }
    }
    return T();
  }
  const T max_flow() {
    while (expath()) {
      fill(P.begin(), P.end(), 0);
      T tot = T();
      for (;;) {
        T add = dfs(src, numeric_limits<T>::max());
        if (add <= eps) break;
        tot += add;
      }
      if (tot <= eps) break;
      flow += tot;
    }
    return flow;
  }
  vector<int> min_cut() {
    max_flow();
    vector<int> R(n);
    for (int i = 0; i < n; i++) R[i] = (D[i] != -1);
    return R;
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  if (m == 0) return puts("0"), 0;
  vector<int> xs, ys, x1(m), x2(m), y1(m), y2(m);
  for (int i = 0; i < m; i++) {
    cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
    x2[i]++;
    y2[i]++;
    xs.push_back(x1[i]);
    xs.push_back(x2[i]);
    ys.push_back(y1[i]);
    ys.push_back(y2[i]);
  }
  sort(xs.begin(), xs.end());
  sort(ys.begin(), ys.end());
  xs.resize(unique(xs.begin(), xs.end()) - xs.begin());
  ys.resize(unique(ys.begin(), ys.end()) - ys.begin());
  int nx = xs.size() - 1, ny = ys.size() - 1;
  int src = nx + ny, sink = nx + ny + 1;
  MaxFlow<LL> mf(sink + 1, src, sink);
  for (int i = 0; i < nx; i++) mf.add_edge(src, i, xs[i + 1] - xs[i]);
  for (int i = 0; i < ny; i++) mf.add_edge(i + nx, sink, ys[i + 1] - ys[i]);
  for (int i = 0; i < nx; i++) {
    for (int j = 0; j < ny; j++) {
      for (int k = 0; k < m; k++) {
        if (xs[i] >= x1[k] && xs[i + 1] <= x2[k] && ys[j] >= y1[k] &&
            ys[j + 1] <= y2[k]) {
          mf.add_edge(i, j + nx, 1LL << 50);
          break;
        }
      }
    }
  }
  cout << mf.max_flow() << endl;
}
