#include <bits/stdc++.h>
using namespace std;
inline long long mod(long long n, long long m) {
  long long ret = n % m;
  if (ret < 0) ret += m;
  return ret;
}
long long gcd(long long a, long long b) {
  return (b == 0LL ? a : gcd(b, a % b));
}
long long exp(long long a, long long b, long long m) {
  if (b == 0LL) return 1LL;
  if (b == 1LL) return mod(a, m);
  long long k = mod(exp(a, b / 2, m), m);
  if (b & 1LL) {
    return mod(a * mod(k * k, m), m);
  } else
    return mod(k * k, m);
}
struct Dinic {
  struct Edge {
    int to, rev;
    long long c, oc;
    long long flow() { return max(oc - c, 0LL); }
  };
  vector<int> lvl, ptr, q;
  vector<vector<Edge>> adj;
  Dinic(int n) : lvl(n), ptr(n), q(n), adj(n) {}
  void addEdge(int a, int b, long long c, int rcap = 0) {
    adj[a].push_back({b, (int)adj[b].size(), c, c});
    adj[b].push_back({a, (int)adj[a].size() - 1, rcap, rcap});
  }
  long long dfs(int v, int t, long long f) {
    if (v == t || !f) return f;
    for (int& i = ptr[v]; i < (int)adj[v].size(); i++) {
      Edge& e = adj[v][i];
      if (lvl[e.to] == lvl[v] + 1)
        if (long long p = dfs(e.to, t, min(f, e.c))) {
          e.c -= p, adj[e.to][e.rev].c += p;
          return p;
        }
    }
    return 0;
  }
  long long calc(int s, int t) {
    long long flow = 0;
    q[0] = s;
    for (int L = 0; L < (31); ++L) do {
        lvl = ptr = vector<int>((int)q.size());
        int qi = 0, qe = lvl[s] = 1;
        while (qi < qe && !lvl[t]) {
          int v = q[qi++];
          for (auto& e : adj[v])
            if (!lvl[e.to] && e.c >> (30 - L))
              q[qe++] = e.to, lvl[e.to] = lvl[v] + 1;
        }
        while (long long p = dfs(s, t, LLONG_MAX)) flow += p;
      } while (lvl[t]);
    return flow;
  }
  bool leftOfMinCut(int a) { return lvl[a] != 0; }
};
const int inf = 1e9;
const int N = 55;
int x1[N], x2[N], abacaba[N], y2[N];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int n, m;
  cin >> n >> m;
  vector<int> x, y;
  for (int i = 0; i < m; i++) {
    cin >> x1[i] >> abacaba[i] >> x2[i] >> y2[i];
    x1[i]--;
    abacaba[i]--;
    x.push_back(x1[i]);
    x.push_back(x2[i]);
    y.push_back(abacaba[i]);
    y.push_back(y2[i]);
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  y.erase(unique(y.begin(), y.end()), y.end());
  int nx = (int)x.size(), ny = (int)y.size();
  int s = nx + ny + 100, t = nx + ny + 101;
  Dinic D(nx + ny + 200);
  for (int i = 1; i < nx; i++) {
    D.addEdge(s, i, x[i] - x[i - 1]);
  }
  for (int i = 1; i < ny; i++) {
    D.addEdge(nx + i, t, y[i] - y[i - 1]);
  }
  auto inside = [](int a, int b, int i, int j) { return (a <= i and j <= b); };
  for (int i = 1; i < nx; i++) {
    for (int j = 1; j < ny; j++) {
      int ok = 0;
      for (int k = 0; k < m; k++) {
        if (inside(x1[k], x2[k], x[i - 1], x[i]) and
            inside(abacaba[k], y2[k], y[j - 1], y[j])) {
          ok = 1;
          break;
        }
      }
      if (ok) {
        D.addEdge(i, nx + j, inf);
      }
    }
  }
  cout << D.calc(s, t) << "\n";
}
