#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;
struct MaxFlow {
  struct edge {
    int a, b;
    ll c;
  };
  vector<edge> es;
  vector<vector<int>> g;
  vector<int> d, pt;
  MaxFlow(int n) : g(n + 1), d(n + 1), pt(n + 1) {}
  void addEdge(int a, int b, ll c = INF) {
    es.push_back({a, b, c});
    g[a].push_back((int)es.size() - 1);
    es.push_back({b, a, 0});
    g[b].push_back((int)es.size() - 1);
  }
  bool bfs(int source, int sink) {
    queue<int> q({source});
    fill(d.begin(), d.end(), (int)g.size() + 1);
    d[source] = 0;
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      if (x == sink) break;
      for (int k : g[x]) {
        edge& e = es[k];
        if (e.c > 0 && d[e.b] > d[e.a] + 1) {
          d[e.b] = d[e.a] + 1;
          q.push(e.b);
        }
      }
    }
    return d[sink] != (int)g.size() + 1;
  }
  ll flow(int x, int sink, ll fl = INF) {
    if (x == sink || fl == 0) return fl;
    for (int& i = pt[x]; i < (int)g[x].size(); i++) {
      edge& e = es[g[x][i]];
      edge& oe = es[g[x][i] ^ 1];
      if (d[e.b] == d[e.a] + 1) {
        if (ll pf = flow(e.b, sink, min(e.c, fl))) {
          e.c -= pf;
          oe.c += pf;
          return pf;
        }
      }
    }
    return 0;
  }
  ll getMaxFlow(int source, int sink) {
    ll r = 0;
    while (bfs(source, sink)) {
      fill(pt.begin(), pt.end(), 0);
      while (ll t = flow(source, sink)) r += t;
    }
    return r;
  }
};
int bins(const vector<int>& vec, int val) {
  int low = 0;
  int high = vec.size();
  while (low != high) {
    int mid = (low + high) >> 1;
    if (vec[mid] < val)
      low = mid + 1;
    else
      high = mid;
  }
  return low;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> xs(m);
  vector<pair<int, int>> ys(m);
  vector<int> xev(2 * m);
  vector<int> yev(2 * m);
  for (int i = 0; i < m; ++i) {
    cin >> xs[i].first >> ys[i].first >> xs[i].second >> ys[i].second;
    ++xs[i].second;
    ++ys[i].second;
    xev[2 * i] = xs[i].first;
    xev[2 * i + 1] = xs[i].second;
    yev[2 * i] = ys[i].first;
    yev[2 * i + 1] = ys[i].second;
  }
  sort(xev.begin(), xev.end());
  sort(yev.begin(), yev.end());
  xev.erase(unique(xev.begin(), xev.end()), xev.end());
  yev.erase(unique(yev.begin(), yev.end()), yev.end());
  for (int i = 0; i < m; ++i) {
    xs[i].first = bins(xev, xs[i].first);
    xs[i].second = bins(xev, xs[i].second);
    ys[i].first = bins(yev, ys[i].first);
    ys[i].second = bins(yev, ys[i].second);
  }
  n = xev.size() + yev.size() + 2;
  int source = n - 2;
  int sink = n - 1;
  MaxFlow mf(n);
  for (int i = 0; i < m; ++i) {
    for (int a = xs[i].first; a < xs[i].second; ++a) {
      for (int b = ys[i].first; b < ys[i].second; ++b) {
        mf.addEdge(a, b + xev.size());
      }
    }
  }
  for (int i = 0; i + 1 < xev.size(); ++i) {
    mf.addEdge(source, i, xev[i + 1] - xev[i]);
  }
  for (int i = 0; i + 1 < yev.size(); ++i) {
    mf.addEdge(i + xev.size(), sink, yev[i + 1] - yev[i]);
  }
  ll res = mf.getMaxFlow(source, sink);
  cout << res << '\n';
}
