#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <typename T1, typename T2>
inline void chkmin(T1 &x, const T2 &y) {
  if (x > y) x = y;
}
template <typename T1, typename T2>
inline void chkmax(T1 &x, const T2 &y) {
  if (x < y) x = y;
}
struct edge {
  ll to, cap, flow;
  edge() {}
  edge(ll _to, ll _cap) { to = _to, cap = _cap, flow = 0; }
  ll get_cap() { return cap - flow; }
};
struct max_flow {
  vector<edge> Edge;
  vector<vector<ll>> g;
  ll n;
  ll s, f;
  max_flow() {}
  void add_edge(ll from, ll to, ll cap) {
    g[from].push_back(Edge.size());
    Edge.push_back({to, cap});
    g[to].push_back(Edge.size());
    Edge.push_back({from, 0});
  }
  max_flow(ll _n, ll _s, ll _f) {
    n = _n, s = _s, f = _f;
    g.assign(n, {});
    Edge.clear();
  }
  vector<ll> dist;
  queue<ll> q;
  const ll INF = 1e9 + 10;
  bool bfs() {
    dist.assign(n, INF);
    dist[s] = 0;
    q.push(s);
    while (!q.empty()) {
      ll v = q.front();
      q.pop();
      for (auto i : g[v]) {
        if (Edge[i].get_cap() < 1) continue;
        ll to = Edge[i].to;
        if (dist[to] != INF) continue;
        dist[to] = dist[v] + 1;
        q.push(to);
      }
    }
    return dist[f] != INF;
  }
  vector<ll> pos;
  ll dfs(ll v, ll flow) {
    if (v == f) return flow;
    for (; pos[v] < g[v].size(); pos[v]++) {
      ll ind = g[v][pos[v]];
      if (Edge[ind].get_cap() < 1) continue;
      ll to = Edge[ind].to;
      if (dist[to] != dist[v] + 1) continue;
      ll add = dfs(to, min(flow, Edge[ind].get_cap()));
      if (add) {
        Edge[ind].flow += add;
        Edge[ind ^ 1].flow -= add;
        return add;
      }
    }
    return 0;
  }
  ll get_flow() {
    ll ans = 0;
    while (bfs()) {
      pos.assign(n, 0);
      while (true) {
        ll add = dfs(s, INF);
        ans += add;
        if (!add) break;
      }
    }
    return ans;
  }
};
const ll MAXM = 51;
ll n, m;
ll x1228[MAXM], y1228[MAXM], x2[MAXM], y2[MAXM];
vector<ll> x, y;
void read() {
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    cin >> x1228[i] >> y1228[i] >> x2[i] >> y2[i];
    x2[i]++;
    y2[i]++;
    x.push_back(x1228[i]);
    x.push_back(x2[i]);
    y.push_back(y1228[i]);
    y.push_back(y2[i]);
  }
}
ll ans;
void run() {
  sort(x.begin(), x.end());
  x.resize(unique(x.begin(), x.end()) - x.begin());
  sort(y.begin(), y.end());
  y.resize(unique(y.begin(), y.end()) - y.begin());
  max_flow g(x.size() + y.size() + 2, x.size() + y.size(),
             x.size() + y.size() + 1);
  for (ll i = 0; i < (ll)x.size() - 1; i++) {
    g.add_edge(g.s, i, x[i + 1] - x[i]);
  }
  for (ll i = 0; i < (ll)y.size() - 1; i++) {
    g.add_edge(i + x.size(), g.f, y[i + 1] - y[i]);
  }
  for (ll el = 0; el < m; el++) {
    for (ll i = 0; i < (ll)x.size() - 1; i++) {
      for (ll j = 0; j < (ll)y.size() - 1; j++) {
        if (x1228[el] <= x[i] && x[i + 1] <= x2[el] && y1228[el] <= y[j] &&
            y[j + 1] <= y2[el])
          g.add_edge(i, j + x.size(), (x[i + 1] - x[i]) + (y[j + 1] - y[j]));
      }
    }
  }
  ans = g.get_flow();
}
void write() { cout << ans << endl; }
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  read();
  run();
  write();
  return 0;
}
