#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int mod = 1000000007;
const double pi = acos(-1.0);
inline void gn(long long& x) {
  int sg = 1;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  c == '-' ? (sg = -1, x = 0) : (x = c - '0');
  while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0';
  x *= sg;
}
inline void gn(int& x) {
  long long t;
  gn(t);
  x = t;
}
inline void gn(unsigned long long& x) {
  long long t;
  gn(t);
  x = t;
}
long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; }
long long powmod(long long a, long long x, long long mod) {
  long long t = 1ll;
  while (x) {
    if (x & 1) t = t * a % mod;
    a = a * a % mod;
    x >>= 1;
  }
  return t;
}
const int maxn = 10000, maxm = 1e7 + 5;
class CostFlow {
 private:
  struct edge {
    int to, c, w, n;
  } e[maxm];
  int head[maxn], tot;
  int dis[maxn], pa[maxn], S, T;
  bool spfa() {
    static bool inq[maxn];
    queue<int> Q;
    memset(dis, 0x3f, sizeof(int) * (T + 5));
    dis[S] = 0;
    Q.push(S);
    while (!Q.empty()) {
      int u = Q.front();
      Q.pop();
      inq[u] = 0;
      for (int p = head[u]; p; p = e[p].n)
        if (e[p].c) {
          int to = e[p].to;
          if (dis[to] > dis[u] + e[p].w) {
            dis[to] = dis[u] + e[p].w;
            pa[to] = p;
            if (!inq[to]) {
              Q.push(to);
              inq[to] = 1;
            }
          }
        }
    }
    return dis[T] < inf;
  }
  int augment() {
    int x, a = inf;
    for (x = T; x != S; x = e[pa[x] ^ 1].to) a = min(e[pa[x]].c, a);
    for (x = T; x != S; x = e[pa[x] ^ 1].to) {
      e[pa[x]].c -= a;
      e[pa[x] ^ 1].c += a;
    }
    return dis[T] * a;
  }

 public:
  inline void init(int s, int t) {
    memset(head, 0, sizeof(int) * (t + 1));
    tot = 1;
    S = s, T = t;
  }
  inline void addEdge(int x, int y, int w, int c) {
    e[++tot] = (edge){y, c, w, head[x]}, head[x] = tot;
    e[++tot] = (edge){x, 0, -w, head[y]}, head[y] = tot;
  }
  int Run() {
    int ans = 0;
    while (spfa()) ans += augment();
    return ans;
  }
} solve;
int id[100][100];
int low[55], high[55];
int main() {
  int n, q;
  gn(n);
  gn(q);
  for (int i = (1); i <= (n); i++) low[i] = 1, high[i] = n;
  for (int i = (1); i <= (q); i++) {
    int t, l, r, v;
    gn(t);
    gn(l);
    gn(r);
    gn(v);
    if (t == 1)
      for (int j = (l); j <= (r); j++) low[j] = max(low[j], v);
    else
      for (int j = (l); j <= (r); j++) high[j] = min(high[j], v);
  }
  for (int i = (1); i <= (n); i++)
    if (low[i] > high[i]) return 0 * puts("-1");
  ;
  int tot = n;
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (n); j++) id[i][j] = ++tot;
  int S = 0, T = ++tot;
  solve.init(S, T);
  for (int i = (1); i <= (n); i++) solve.addEdge(S, i, 0, 1);
  for (int i = (1); i <= (n); i++) {
    for (int j = (low[i]); j <= (high[i]); j++) {
      for (int k = (1); k <= (n); k++) {
        solve.addEdge(i, id[j][k], 0, 1);
      }
    }
  }
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (n); j++)
      solve.addEdge(id[i][j], T, j * j - (j - 1) * (j - 1), 1);
  printf("%d\n", solve.Run());
  return 0;
}
