#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
template <class T>
void mxi(T& a, const T& b) {
  a = max(a, b);
}
template <class T>
void mni(T& a, const T& b) {
  a = min(a, b);
}
long double EPS = 1e-9;
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 0x3f3f3f3f;
struct edge {
  int u, v;
  int flow;
  int cost;
  edge() {}
  edge(int u, int v, int f, int c) : u(u), v(v), flow(f), cost(c) {}
};
struct MinCostMaxFlow {
  int N;
  vector<vector<int> > G;
  vector<edge> E;
  int numEdges;
  vector<int> found, dad;
  vector<int> dist;
  MinCostMaxFlow(int N) : N(N), G(N), numEdges(0), found(N), dad(N), dist(N) {}
  void addEdge(int from, int to, int capacity, int cost) {
    G[from].push_back(numEdges++);
    E.push_back(edge(from, to, capacity, cost));
    G[to].push_back(numEdges++);
    E.push_back(edge(to, from, 0, int(-1) * cost));
  }
  bool spfa(int s, int t) {
    fill(dad.begin(), dad.end(), -1);
    fill(dist.begin(), dist.end(), INF);
    fill(found.begin(), found.end(), 0);
    queue<int> Q;
    dist[s] = 0;
    Q.push(s);
    found[s] = true;
    while (!Q.empty()) {
      int u = Q.front();
      Q.pop();
      if (u == t) continue;
      for (int i = 0; i < (int)G[u].size(); ++i) {
        edge& pres = E[G[u][i]];
        int v = pres.v;
        if (pres.flow <= 0) continue;
        if (dist[u] + pres.cost < dist[v]) {
          dad[v] = G[u][i];
          dist[v] = dist[u] + pres.cost;
          if (!found[v]) Q.push(v), found[v] = true;
        }
      }
      found[u] = false;
    }
    return (dad[t] != -1);
  }
  int dfs(int t) {
    int flow = INF;
    for (int i = dad[t]; i != -1; i = dad[E[i].u]) {
      if (E[i].flow < flow) flow = E[i].flow;
    }
    for (int i = dad[t]; i != -1; i = dad[E[i].u]) {
      E[i].flow -= flow;
      E[i ^ 1].flow += flow;
    }
    return flow;
  }
  pair<int, int> getMaxFlow(int s, int t) {
    int totflow = 0;
    int totcost = 0;
    while (spfa(s, t)) {
      int amt = dfs(t);
      totflow += amt;
      totcost += dist[t] * (int)amt;
    }
    return make_pair(totflow, totcost);
  }
};
void solve() {
  int n, q;
  cin >> n >> q;
  int r[n + 1], l[n + 1];
  for (int o = 0; o < (n); o++) r[o + 1] = n, l[o + 1] = 1;
  while (q--) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a == 1)
      for (int i = b; i <= c; i++) mxi(l[i], d);
    else
      for (int i = b; i <= c; i++) mni(r[i], d);
  }
  for (int i = 1; i <= n; i++)
    if (l[i] > r[i]) {
      cout << -1 << '\n';
      return;
    }
  MinCostMaxFlow Flow(2 + 2 * n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      Flow.addEdge(0, j, 1, 2 * i - 1);
    }
    Flow.addEdge(n + i, 2 * n + 1, 1, 0);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = l[i]; j <= r[i]; j++) Flow.addEdge(j, n + i, 1, 0);
  }
  pair<int, int> ans = Flow.getMaxFlow(0, 2 * n + 1);
  cout << ans.second << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t, tab;
  t = 1;
  tab = t;
  while (t--) {
    solve();
  }
  cerr << ((double)clock() / CLOCKS_PER_SEC);
}
