#include <bits/stdc++.h>
using namespace std;
const long double PI = acos(-1.0);
struct FlowEdge {
  int v, u;
  long long cap, flow = 0;
  FlowEdge(int v, int u, long long cap) : v(v), u(u), cap(cap) {}
};
struct Dinic {
  const long long flow_inf = 1e18;
  vector<FlowEdge> edges;
  vector<vector<int>> adj;
  int n, m = 0;
  int s, t;
  vector<int> level, ptr;
  queue<int> q;
  Dinic(int n, int s, int t) : n(n), s(s), t(t) {
    adj.resize(n);
    level.resize(n);
    ptr.resize(n);
  }
  void add_edge(int v, int u, long long cap) {
    edges.emplace_back(v, u, cap);
    edges.emplace_back(u, v, 0);
    adj[v].push_back(m);
    adj[u].push_back(m + 1);
    m += 2;
  }
  bool bfs() {
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int id : adj[v]) {
        if (edges[id].cap - edges[id].flow < 1) continue;
        if (level[edges[id].u] != -1) continue;
        level[edges[id].u] = level[v] + 1;
        q.push(edges[id].u);
      }
    }
    return level[t] != -1;
  }
  long long dfs(int v, long long pushed) {
    if (pushed == 0) return 0;
    if (v == t) return pushed;
    for (int& cid = ptr[v]; cid < (int)adj[v].size(); cid++) {
      int id = adj[v][cid];
      int u = edges[id].u;
      if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
        continue;
      long long tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
      if (tr == 0) continue;
      edges[id].flow += tr;
      edges[id ^ 1].flow -= tr;
      return tr;
    }
    return 0;
  }
  long long flow() {
    long long f = 0;
    while (true) {
      fill(level.begin(), level.end(), -1);
      level[s] = 0;
      q.push(s);
      if (!bfs()) break;
      fill(ptr.begin(), ptr.end(), 0);
      while (long long pushed = dfs(s, flow_inf)) {
        f += pushed;
      }
    }
    return f;
  }
};
pair<int, int> a[55], b[55];
int INF = 1e9;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> xs, ys;
  for (int i = 0; i < m; ++i) {
    cin >> a[i].first >> a[i].second;
    cin >> b[i].first >> b[i].second;
    --a[i].first;
    --a[i].second;
    if (a[i].first) xs.push_back(a[i].first);
    ;
    if (a[i].second) ys.push_back(a[i].second);
    xs.push_back(b[i].first);
    ys.push_back(b[i].second);
  }
  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());
  Dinic d((int)xs.size() + (int)ys.size() + 2, 0,
          (int)xs.size() + (int)ys.size() + 1);
  for (int i = 0; i < (int)xs.size(); ++i) {
    d.add_edge(0, i + 1, xs[i] - (i == 0 ? 0 : xs[i - 1]));
  }
  for (int i = 0; i < (int)ys.size(); ++i) {
    d.add_edge((int)xs.size() + 1 + i, (int)xs.size() + (int)ys.size() + 1,
               ys[i] - (i == 0 ? 0 : ys[i - 1]));
  }
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < (int)xs.size(); ++j) {
      for (int k = 0; k < (int)ys.size(); ++k) {
        if (xs[j] <= b[i].first and xs[j] > a[i].first and
            ys[k] <= b[i].second and ys[k] > a[i].second) {
          d.add_edge(j + 1, (int)xs.size() + 1 + k, INF);
        }
      }
    }
  }
  cout << d.flow() << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
