#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char c;
  for (c = getchar(); !isdigit(c); c = getchar())
    ;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
}
const int N = 255, P = 998244353, oo = 0x3f3f3f3f;
int mul(int a, int b) { return 1ll * a * b % P; }
int add(int a, int b) {
  a += b;
  return a >= P ? a - P : a;
}
int sub(int a, int b) {
  a -= b;
  return a < 0 ? a + P : a;
}
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
namespace Dinic {
const int S = N * N;
struct Edge {
  int d, cap, nxt;
} es[S << 2];
int vn, g[S], lev[S], itr[S], q[S], h, t, en = 1;
void add(int s, int d, int cap) {
  es[++en] = (Edge){d, cap, g[s]}, g[s] = en;
  es[++en] = (Edge){s, 0, g[d]}, g[d] = en;
}
void bfs(int s) {
  int v;
  memset(lev, -1, sizeof(lev));
  h = t = 0;
  q[t++] = s;
  lev[s] = 0;
  while (h < t) {
    v = q[h++];
    for (int i = g[v], d = es[i].d; i; i = es[i].nxt, d = es[i].d) {
      if (es[i].cap > 0 && lev[d] < 0) {
        lev[d] = lev[v] + 1;
        q[t++] = es[i].d;
      }
    }
  }
}
int dfs(int v, int t, int f) {
  if (v == t) return f;
  for (int &i = itr[v]; i; i = es[i].nxt) {
    if (es[i].cap <= 0 || lev[es[i].d] <= lev[v]) continue;
    int a = dfs(es[i].d, t, min(es[i].cap, f));
    if (a <= 0) continue;
    es[i].cap -= a;
    es[i ^ 1].cap += a;
    return a;
  }
  return 0;
}
int sol(int s, int t) {
  int i, res = 0, a;
  while (true) {
    bfs(s);
    if (lev[t] < 0) break;
    for (i = 0; i <= vn; ++i) itr[i] = g[i];
    for (a = dfs(s, t, oo); a > 0; a = dfs(s, t, oo)) {
      res += a;
    }
  }
  return res;
}
}  // namespace Dinic
using namespace Dinic;
int n, m;
vector<int> xx, yy;
struct Rec {
  int x1, y1, x2, y2;
} rec[N];
int f(vector<int> &a, int x) {
  return lower_bound(a.begin(), a.end(), x) - a.begin() + 1;
}
int main(int argc, char *argv[]) {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= (m); ++i) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    --x1, --y1;
    xx.push_back(x1), xx.push_back(x2);
    yy.push_back(y1), yy.push_back(y2);
    rec[i] = (Rec){x1, y1, x2, y2};
  }
  xx.push_back(0), yy.push_back(0);
  sort(xx.begin(), xx.end()), sort(yy.begin(), yy.end());
  xx.erase(unique(xx.begin(), xx.end()), xx.end());
  yy.erase(unique(yy.begin(), yy.end()), yy.end());
  int sx = xx.size(), sxy = sx + yy.size(), src = sxy + m + 1, snk = src + 1;
  vn = snk;
  for (int i = 1; i < (xx.size()); ++i) {
    add(src, i + 1, xx[i] - xx[i - 1]);
  }
  for (int i = 1; i < (yy.size()); ++i) {
    add(sx + i + 1, snk, yy[i] - yy[i - 1]);
  }
  for (int i = 1; i <= (m); ++i) {
    int x1 = rec[i].x1, x2 = rec[i].x2, y1 = rec[i].y1, y2 = rec[i].y2;
    x1 = f(xx, x1);
    x2 = f(xx, x2);
    y1 = f(yy, y1);
    y2 = f(yy, y2);
    for (int j = x1 + 1; j <= x2; ++j) add(j, sxy + i, oo);
    for (int j = y1 + 1; j <= y2; ++j) add(sxy + i, sx + j, oo);
  }
  cout << sol(src, snk) << endl;
}
