#include <bits/stdc++.h>
using namespace std;
const int N = (((int)1e4 + 32) / 32) * 32;
bitset<N> is[N];
int mt[N], rev_mt[N], used[N];
int memn;
bitset<N> nusedmt;
bool dfs(int pos) {
  used[pos] = 1;
  if (rev_mt[pos] != -1) nusedmt[rev_mt[pos]] = 0;
  for (int i = N / 32 - 1; i >= 0; i--) {
    unsigned int *l = (unsigned int *)(&nusedmt) + i;
    unsigned int *r = (unsigned int *)(&is[pos]) + i;
    unsigned int cur = ((*l) & (*r));
    while (cur > 0) {
      int bt = __builtin_ctz(cur);
      cur -= (1U << bt);
      bt += i * 32;
      assert(is[pos][bt]);
      if (mt[bt] == -1) {
        rev_mt[pos] = bt;
        mt[bt] = pos;
        nusedmt[bt] = 0;
        return true;
      }
    }
  }
  for (int i = N / 32 - 1; i >= 0; i--) {
    unsigned int *l = (unsigned int *)(&nusedmt) + i;
    unsigned int *r = (unsigned int *)(&is[pos]) + i;
    unsigned int cur = ((*l) & (*r));
    while (cur > 0) {
      int bt = __builtin_ctz(cur);
      cur -= (1U << bt);
      bt += i * 32;
      assert(is[pos][bt]);
      if (mt[bt] != -1) {
        if (!used[mt[bt]] && dfs(mt[bt])) {
          nusedmt[bt] = 0;
          rev_mt[pos] = bt;
          mt[bt] = pos;
          return true;
        }
      }
    }
  }
  return false;
}
void solve(int n) {
  memn = n;
  int q;
  cin >> q;
  for (int i = 0; i < (int)(N); ++i) is[i] = ~bitset<N>();
  for (int i = 0; i < (int)(q); ++i) {
    int x1, x2, y1, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    --x1, --x2, --y1, --y2;
    for (int x = x1; x <= x2; x++)
      for (int y = y1; y <= y2; y++) is[x][y] = 0;
  }
  int ans = 0;
  fill(rev_mt, rev_mt + N, -1);
  fill(mt, mt + N, -1);
  vector<int> order(n);
  iota((order).begin(), (order).end(), 0);
  reverse((order).begin(), (order).end());
  while (true) {
    bool upd = false;
    fill(used, used + N, 0);
    nusedmt.reset();
    for (int i = 0; i < (int)(n); ++i) nusedmt[i] = 1;
    for (int i : order)
      if (!used[i] && rev_mt[i] == -1) {
        bool was = dfs(i);
        upd |= was;
        ans += was;
      }
    if (!upd) break;
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  while (cin >> n) solve(n);
}
