#include <bits/stdc++.h>
using namespace std;
const int foo = (int)1e9;
const int coo = (int)1e9;
namespace MincostMaxflow {
const int maxv = 1e5 + 5;
const int maxe = 1e6 + 5;
int n, s, t, E;
int adj[maxe], nxt[maxe], lst[maxv], frm[maxv];
int cap[maxe], flw[maxe], totalFlow;
int cst[maxe], pot[maxe], dst[maxv], totalCost;
void init(int nn, int ss, int tt) {
  n = nn, s = ss, t = tt;
  fill_n(lst, n, -1), E = 0;
}
void add(int u, int v, int ca, int co) {
  adj[E] = v, cap[E] = ca, flw[E] = 0, cst[E] = +co, nxt[E] = lst[u],
  lst[u] = E++;
  adj[E] = u, cap[E] = 0, flw[E] = 0, cst[E] = -co, nxt[E] = lst[v],
  lst[v] = E++;
}
void bellman() {
  fill_n(pot, n, 0);
  while (1) {
    int found = 0;
    for (int u = 0; u < n; u++)
      for (int e = lst[u]; e != -1; e = nxt[e])
        if (flw[e] < cap[e]) {
          int v = adj[e];
          if (pot[v] > pot[u] + cst[e]) {
            pot[v] = pot[u] + cst[e];
            found = 1;
          }
        }
    if (!found) break;
  }
}
int dijkstra() {
  priority_queue<pair<int, int> > que;
  fill_n(dst, n, coo), dst[s] = 0;
  que.push(make_pair(-dst[s], s));
  while (que.size()) {
    int dnow = -que.top().first;
    int u = que.top().second;
    que.pop();
    if (dst[u] < dnow) continue;
    for (int e = lst[u]; e != -1; e = nxt[e])
      if (flw[e] < cap[e]) {
        int v = adj[e];
        int dnxt = dnow + cst[e] + pot[u] - pot[v];
        if (dst[v] > dnxt) {
          dst[v] = dnxt;
          frm[v] = e;
          que.push(make_pair(-dnxt, v));
        }
      }
  }
  return dst[t] < coo;
}
int mincost() {
  totalCost = 0, totalFlow = 0;
  bellman();
  while (1) {
    if (!dijkstra()) break;
    int mn = foo;
    for (int v = t, e = frm[v]; v != s; v = adj[e ^ 1], e = frm[v])
      mn = min(mn, cap[e] - flw[e]);
    for (int v = t, e = frm[v]; v != s; v = adj[e ^ 1], e = frm[v]) {
      flw[e] += mn;
      flw[e ^ 1] -= mn;
    }
    totalFlow += mn;
    totalCost += mn * (dst[t] - pot[s] + pot[t]);
    for (int u = 0; u < n; u++) pot[u] += dst[u];
  }
  return totalCost;
}
}  // namespace MincostMaxflow
int l[105], r[105], n, m;
void read() {
  cin >> n >> m;
  for (int i = (1); i <= (n); ++i) l[i] = 1, r[i] = n;
  while (m--) {
    int type, x, y, z;
    cin >> type >> x >> y >> z;
    if (type == 1) {
      for (int i = (x); i <= (y); ++i) l[i] = max(l[i], z);
    } else {
      for (int i = (x); i <= (y); ++i) r[i] = min(r[i], z);
    }
  }
}
void solve() {
  int src = 0, tgt = 2 * n + 1;
  MincostMaxflow::init(2 * n + 3, src, tgt);
  for (int i = (1); i <= (n); ++i) {
    MincostMaxflow::add(src, i, 1, 0);
    if (r[i] < l[i]) {
      cout << -1;
      exit(0);
    }
    for (int x = (l[i]); x <= (r[i]); ++x) {
      MincostMaxflow::add(i, x + n, 1, 0);
    }
  }
  for (int i = (1); i <= (n); ++i) {
    for (int j = (1); j <= (n); ++j) {
      MincostMaxflow::add(i + n, tgt, 1, 2 * j - 1);
    }
  }
  cout << MincostMaxflow::mincost();
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  read();
  solve();
  return 0;
}
