#include <bits/stdc++.h>
using namespace std;
const int M = 55, N = 500, inf = 1e9;
int n, m, X[M * 2], Y[M * 2], e, nx = 0, ny = 0;
struct rectangle {
  int id, x1, x2, y1, y2;
} r[M];
struct edge {
  int to, cap, rev;
};
vector<edge> g[N];
void adde(int u, int v, int w) {
  g[u].push_back((edge){v, w, (int)g[v].size()});
  g[v].push_back((edge){u, 0, (int)g[u].size() - 1});
}
int lev[N], itr[N];
void bfs() {
  memset(lev, -1, sizeof(lev));
  queue<int> Q;
  Q.push(0);
  lev[0] = 0;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = 0; i < (int)g[u].size(); ++i) {
      int v = g[u][i].to;
      if (g[u][i].cap && lev[v] < 0) {
        lev[v] = lev[u] + 1;
        Q.push(v);
      }
    }
  }
}
int dfs(int u, int f) {
  if (u == e) return f;
  for (int &i = itr[u]; i < (int)g[u].size(); ++i) {
    int v = g[u][i].to, r = g[u][i].rev;
    if (lev[v] > lev[u] && g[u][i].cap) {
      int d = dfs(v, min(f, g[u][i].cap));
      if (d) {
        g[u][i].cap -= d;
        g[v][r].cap += d;
        return d;
      }
    }
  }
  return 0;
}
int Max_flow() {
  int flow = 0;
  for (;;) {
    bfs();
    if (lev[e] < 0) return flow;
    memset(itr, 0, sizeof(itr));
    int f = dfs(0, inf);
    while (f) {
      flow += f;
      f = dfs(0, inf);
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d%d", &r[i].x1, &r[i].y1, &r[i].x2, &r[i].y2);
    X[++nx] = r[i].x1;
    X[++nx] = ++r[i].x2;
    Y[++ny] = r[i].y1;
    Y[++ny] = ++r[i].y2;
  }
  X[++nx] = Y[++ny] = n + 1;
  sort(X + 1, X + nx + 1);
  sort(Y + 1, Y + ny + 1);
  nx = unique(X + 1, X + nx + 1) - X - 1;
  ny = unique(Y + 1, Y + ny + 1) - Y - 1;
  for (int i = 1; i <= m; ++i) {
    r[i].x1 = lower_bound(X + 1, X + nx + 1, r[i].x1) - X;
    r[i].x2 = lower_bound(X + 1, X + nx + 1, r[i].x2) - X;
    r[i].y1 = lower_bound(Y + 1, Y + ny + 1, r[i].y1) - Y;
    r[i].y2 = lower_bound(Y + 1, Y + ny + 1, r[i].y2) - Y;
  }
  e = nx + ny + 1;
  for (int i = 1; i <= m; ++i) {
    for (int x = r[i].x1; x < r[i].x2; ++x) {
      for (int y = r[i].y1; y < r[i].y2; ++y) {
        adde(x, nx + y, inf);
      }
    }
  }
  for (int i = 1; i < nx; ++i) adde(0, i, X[i + 1] - X[i]);
  for (int i = 1; i < ny; ++i) adde(nx + i, e, Y[i + 1] - Y[i]);
  cout << Max_flow() << endl;
  return 0;
}
