#include <bits/stdc++.h>
namespace fastIO {
bool IOerror = 0;
inline char nc() {
  static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000;
  if (p1 == pend) {
    p1 = buf;
    pend = buf + fread(buf, 1, 100000, stdin);
    if (pend == p1) {
      IOerror = 1;
      return -1;
    }
  }
  return *p1++;
}
inline bool blank(char ch) {
  return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
}
inline void read(int &x) {
  char ch;
  while (blank(ch = nc()))
    ;
  if (IOerror) return;
  for (x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0')
    ;
}
};  // namespace fastIO
template <class T>
inline T lowbit(T x) {
  return x & (-x);
}
template <class T>
inline T sqr(T x) {
  return x * x;
}
template <class T>
inline bool scan(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF) return 0;
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
const double pi = 3.14159265358979323846264338327950288L;
using namespace std;
int n, m, q;
int s, t;
struct node {
  int u, v, next;
  int cap, flow, cost;
} edge[1000005];
int head[105], cnt;
int pre[105];
int dis[105];
bool vis[105];
int N;
void init() {
  memset(head, -1, sizeof head);
  cnt = 0;
}
void add_edge(int u, int v, int cap, int cost) {
  edge[cnt] = (node){u, v, head[u], cap, 0, cost};
  head[u] = cnt++;
  edge[cnt] = (node){v, u, head[v], 0, 0, -cost};
  head[v] = cnt++;
}
bool spfa(int s, int t) {
  queue<int> q;
  for (int i = 0; i < N; i++) {
    dis[i] = 1e9;
    vis[i] = 0;
    pre[i] = -1;
  }
  dis[s] = 0;
  vis[s] = 1;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    for (int i = head[u]; i != -1; i = edge[i].next) {
      int v = edge[i].v;
      if (edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost) {
        dis[v] = dis[u] + edge[i].cost;
        pre[v] = i;
        if (!vis[v]) {
          vis[v] = 1;
          q.push(v);
        }
      }
    }
  }
  if (pre[t] == -1)
    return 0;
  else
    return 1;
}
int MCMF(int s, int t, int &cost) {
  int flow = 0;
  cost = 0;
  while (spfa(s, t)) {
    int Min = 1e9;
    for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].v]) {
      if (Min > edge[i].cap - edge[i].flow) {
        Min = edge[i].cap - edge[i].flow;
      }
    }
    for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].v]) {
      edge[i].flow += Min;
      edge[i ^ 1].flow -= Min;
      cost += edge[i].cost * Min;
    }
    flow += Min;
  }
  return flow;
}
int Min[105], Max[105];
int main() {
  scanf("%d%d", &n, &q);
  init();
  for (int i = 1; i <= n; i++) {
    Min[i] = 1;
    Max[i] = n;
  }
  for (int i = 1; i <= q; i++) {
    int op, l, r, v;
    scanf("%d%d%d%d", &op, &l, &r, &v);
    if (op == 1) {
      for (int i = l; i <= r; i++) {
        Min[i] = max(Min[i], v);
        ;
      }
    } else
      for (int i = l; i <= r; i++) {
        Max[i] = min(Max[i], v);
      }
  }
  s = 0, t = n * 2 + 1, N = t + 1;
  for (int i = 1; i <= n; i++) {
    add_edge(s, i, 1, 0);
  }
  for (int i = 1; i <= n; i++) {
    int sum = 0;
    for (int j = 1; j <= n; j++) {
      add_edge(i + n, t, 1, j * j - sum);
      sum = j * j;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (Min[i] > Max[i]) {
      printf("-1\n");
      return 0;
    }
    for (int j = Min[i]; j <= Max[i]; j++) {
      add_edge(i, j + n, 1, 0);
    }
  }
  int cost;
  int ans = MCMF(s, t, cost);
  if (ans == n) {
    printf("%d\n", cost);
  } else
    puts("-1");
  return 0;
}
