#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 239;
struct Edge {
  long long u, v;
  long long cap, flow;
  Edge(long long u_, long long v_, long long cap_, long long flow_)
      : u(u_), v(v_), cap(cap_), flow(flow_){};
};
namespace MaxFlow {
vector<Edge> ed;
vector<vector<long long>> g;
vector<long long> d;
vector<long long> pnt;
long long n;
long long start;
long long end;
bool bfs(long long lim) {
  fill(d.begin(), d.end(), INF);
  d[start] = 0;
  queue<long long> q;
  q.push(start);
  while (!q.empty()) {
    long long u = q.front();
    q.pop();
    for (auto ind : g[u]) {
      if (ed[ind].flow + lim <= ed[ind].cap && d[ed[ind].v] > d[u] + 1) {
        d[ed[ind].v] = d[u] + 1;
        q.push(ed[ind].v);
      }
    }
  }
  return d[end] != INF;
}
long long dfs(long long u, long long flow) {
  if (u == end) {
    return flow;
  }
  if (flow == 0) {
    return 0;
  }
  for (; pnt[u] < (long long)g[u].size(); pnt[u]++) {
    long long ind = g[u][pnt[u]];
    long long v = ed[ind].v;
    if (d[v] < d[u] + 1) {
      continue;
    }
    long long pushed = dfs(v, min(flow, ed[ind].cap - ed[ind].flow));
    if (pushed > 0) {
      ed[ind].flow += pushed;
      ed[ind ^ 1].flow -= pushed;
      return pushed;
    }
  }
  return 0;
}
long long solve() {
  long long ans = 0;
  if (false) cerr << "solve" << endl;
  for (long long i = 48; i >= 0; i--) {
    while (bfs((1LL << i))) {
      if (false) cerr << "bfs ok" << endl;
      fill(pnt.begin(), pnt.end(), 0);
      while (true) {
        long long pushed = dfs(start, INF);
        if (pushed == 0) {
          break;
        } else {
          ans += pushed;
        }
      }
    }
  }
  return ans;
}
void init(long long n_, long long start_, long long end_) {
  n = n_;
  start = start_;
  end = end_;
  g.resize(n);
  d.resize(n);
  pnt.resize(n);
}
void add_edge(long long u, long long v, long long cap) {
  long long pnt = (long long)ed.size();
  auto f = Edge(u, v, cap, 0);
  auto s = Edge(v, u, 0, 0);
  ed.push_back(f);
  ed.push_back(s);
  g[u].push_back(pnt);
  g[v].push_back(pnt + 1);
}
}  // namespace MaxFlow
void zip(vector<long long> &a, vector<long long> &seen) {
  seen = a;
  sort(seen.begin(), seen.end());
  seen.resize(unique(seen.begin(), seen.end()) - seen.begin());
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  vector<long long> xs, seen_x;
  vector<long long> ys, seen_y;
  auto gx = [&](long long x) {
    return lower_bound(seen_x.begin(), seen_x.end(), x) - seen_x.begin();
  };
  auto gy = [&](long long y) {
    return lower_bound(seen_y.begin(), seen_y.end(), y) - seen_y.begin();
  };
  long long no_need, m;
  cin >> no_need >> m;
  vector<pair<long long, long long>> xr;
  vector<pair<long long, long long>> yr;
  for (long long i = 0; i < m; i++) {
    long long x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x2++;
    y2++;
    xr.push_back({x1, x2});
    yr.push_back({y1, y2});
    xs.push_back(x1);
    xs.push_back(x2);
    ys.push_back(y1);
    ys.push_back(y2);
  }
  zip(xs, seen_x);
  zip(ys, seen_y);
  for (auto &[f, s] : xr) {
    f = gx(f);
    s = gx(s);
  }
  for (auto &[f, s] : yr) {
    f = gy(f);
    s = gy(s);
  }
  if (false) cerr << "hr " << endl;
  vector<vector<long long>> gr(seen_x.size(), vector<long long>(seen_y.size()));
  for (long long i = 0; i < m; i++) {
    for (long long x = xr[i].first; x < xr[i].second; x++) {
      for (long long y = yr[i].first; y < yr[i].second; y++) {
        long long dx = seen_x[x + 1] - seen_x[x];
        long long dy = seen_y[y + 1] - seen_y[y];
        gr[x][y] = dx * dy;
      }
    }
  }
  if (false) cerr << "ok" << endl;
  long long all = (long long)(seen_x.size() + seen_y.size());
  long long st = all;
  long long en = all + 1;
  MaxFlow::init(all + 2, st, en);
  for (long long i = 0; i + 1 < (long long)seen_x.size(); i++) {
    MaxFlow::add_edge(st, i, seen_x[i + 1] - seen_x[i]);
  }
  for (long long j = 0; j + 1 < (long long)seen_y.size(); j++) {
    MaxFlow::add_edge(j + (long long)seen_x.size(), en,
                      seen_y[j + 1] - seen_y[j]);
  }
  for (long long i = 0; i < (long long)seen_x.size(); i++) {
    for (long long j = 0; j < (long long)seen_y.size(); j++) {
      if (gr[i][j] > 0) {
        MaxFlow::add_edge(i, j + (long long)seen_x.size(), INF);
      }
    }
  }
  for (long long i = 0; i < (long long)seen_x.size(); i++) {
    for (long long j = 0; j < (long long)seen_y.size(); j++) {
      if (false) cerr << gr[i][j] << ' ';
    }
    if (false) cerr << endl;
  }
  if (false) cerr << endl;
  long long ans = MaxFlow::solve();
  cout << ans << endl;
}
