#include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wunused-function"
using namespace std;
namespace {
namespace shik {
template <class T>
void _R(T &x) {
  cin >> x;
}
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%" SCNd64, &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template <class T, class... U>
void R(T &head, U &...tail) {
  _R(head);
  R(tail...);
}
template <class T>
void _W(const T &x) {
  cout << x;
}
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%" PRId64, x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T>
void _W(const vector<T> &x) {
  for (auto i = x.begin(); i != x.end(); _W(*i++))
    if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &...tail) {
  _W(head);
  putchar(sizeof...(tail) ? ' ' : '\n');
  W(tail...);
}
template <class T, class F = less<T>>
void sort_uniq(vector<T> &v, F f = F()) {
  sort(begin(v), end(v), f);
  v.resize(unique(begin(v), end(v)) - begin(v));
}
template <class T>
inline T bit(T x, int i) {
  return (x >> i) & 1;
}
template <class T>
inline bool chkmax(T &a, const T &b) {
  return b > a ? a = b, true : false;
}
template <class T>
inline bool chkmin(T &a, const T &b) {
  return b < a ? a = b, true : false;
}
template <class T>
using MaxHeap = priority_queue<T>;
template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T = int>
struct MaxFlow {
  struct E {
    int to, rev;
    T cap;
  };
  int n;
  vector<vector<E>> e;
  vector<int> cur, dis;
  void init(int _n) {
    n = _n;
    e.clear();
    e.resize(n);
    cur.resize(n);
    dis.resize(n);
  }
  template <bool dir>
  void _add(int a, int b, T c) {
    if (a == b) return;
    e[a].push_back(E{b, (int)e[b].size() - 0, c});
    e[b].push_back(E{a, (int)e[a].size() - 1, dir ? 0 : c});
  }
  void add(int a, int b, T c) { _add<true>(a, b, c); }
  void add_bi(int a, int b, T c) { _add<false>(a, b, c); }
  bool bfs(int s, int t) {
    memset(dis.data(), -1, dis.size() * sizeof(dis[0]));
    dis[s] = 0;
    vector<int> que{s};
    for (size_t i = 0; i < que.size() && dis[t] == -1; i++) {
      auto p = que[i];
      for (auto &j : e[p]) {
        if (j.cap == 0 || dis[j.to] != -1) continue;
        dis[j.to] = dis[p] + 1;
        que.push_back(j.to);
      }
    }
    return dis[t] != -1;
  }
  T go(int p, T c, int t) {
    if (p == t) return c;
    T ret = 0;
    for (size_t i = cur[p]; i < e[p].size() && ret < c; i++) {
      auto &ei = e[p][i];
      if (ei.cap == 0 || dis[ei.to] <= dis[p]) continue;
      auto f = go(ei.to, min(ei.cap, c - ret), t);
      ret += f;
      ei.cap -= f;
      e[ei.to][ei.rev].cap += f;
    }
    if (ret == 0) dis[p] = -1;
    return ret;
  }
  T maxflow(int s, int t) {
    T f = 0;
    while (bfs(s, t)) {
      memset(cur.data(), 0, cur.size() * sizeof(cur[0]));
      f += go(s, numeric_limits<T>::max(), t);
    }
    return f;
  }
  T maxflow() { return maxflow(0, n - 1); }
};
MaxFlow<> flow;
const int M = 55;
int n, h, m, l[M], r[M], x[M], c[M];
void main() {
  R(n, h, m);
  for (int i = 0; i < int(m); i++) R(l[i], r[i], x[i], c[i]);
  int fn = 0;
  auto f = [&](int t) {
    int ret = fn;
    fn += t;
    return ret;
  };
  int st = f(1);
  vector<int> b(n);
  for (int i = 0; i < int(n); i++) b[i] = f(h + 1);
  vector<int> z(m);
  for (int i = 0; i < int(m); i++) z[i] = f(1);
  int ed = f(1);
  flow.init(fn);
  for (int i = 0; i < int(n); i++)
    for (int j = (1); j <= int(h); j++) {
      flow.add(st, b[i] + j, 2 * j - 1);
      flow.add(b[i] + j, b[i] + j - 1, INT_MAX);
    }
  for (int i = 0; i < int(n); i++)
    for (int j = (0); j <= int(h); j++)
      for (int k = 0; k < int(m); k++)
        if (l[k] <= i + 1 && i + 1 <= r[k] && j > x[k])
          flow.add(b[i] + j, z[k], INT_MAX);
  for (int i = 0; i < int(m); i++) flow.add(z[i], ed, c[i]);
  int mf = flow.maxflow(st, ed);
  int ans = n * h * h - mf;
  W(ans);
}
}  // namespace shik
}  // namespace
int main() {
  shik::main();
  return 0;
}

### Prompt

Construct a CPP code solution to the problem outlined:

You are planning to build housing on a street. There are n spots available on the street on which you can build a house. The spots are labeled from 1 to n from left to right. In each spot, you can build a house with an integer height between 0 and h.

In each spot, if a house has height a, you will gain a^2 dollars from it.

The city has m zoning restrictions. The i-th restriction says that the tallest house from spots l_i to r_i (inclusive) must be at most x_i.

You would like to build houses to maximize your profit. Determine the maximum profit possible.

Input

The first line contains three integers n, h, and m (1 ≤ n,h,m ≤ 50) — the number of spots, the maximum height, and the number of restrictions.

Each of the next m lines contains three integers l_i, r_i, and x_i (1 ≤ l_i ≤ r_i ≤ n, 0 ≤ x_i ≤ h) — left and right limits (inclusive) of the i-th restriction and the maximum possible height in that range.

Output

Print a single integer, the maximum profit you can make.

Examples

Input


3 3 3
1 1 1
2 2 3
3 3 2


Output


14


Input


4 10 2
2 3 8
3 4 7


Output


262

Note

In the first example, there are 3 houses, the maximum height of a house is 3, and there are 3 restrictions. The first restriction says the tallest house between 1 and 1 must be at most 1. The second restriction says the tallest house between 2 and 2 must be at most 3. The third restriction says the tallest house between 3 and 3 must be at most 2.

In this case, it is optimal to build houses with heights [1, 3, 2]. This fits within all the restrictions. The total profit in this case is 1^2 + 3^2 + 2^2 = 14.

In the second example, there are 4 houses, the maximum height of a house is 10, and there are 2 restrictions. The first restriction says the tallest house from 2 to 3 must be at most 8. The second restriction says the tallest house from 3 to 4 must be at most 7.

In this case, it's optimal to build houses with heights [10, 8, 7, 7]. We get a profit of 10^2+8^2+7^2+7^2 = 262. Note that there are two restrictions on house 3 and both of them must be satisfied. Also, note that even though there isn't any explicit restrictions on house 1, we must still limit its height to be at most 10 (h=10).

### Response

```cpp
#include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wunused-function"
using namespace std;
namespace {
namespace shik {
template <class T>
void _R(T &x) {
  cin >> x;
}
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%" SCNd64, &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template <class T, class... U>
void R(T &head, U &...tail) {
  _R(head);
  R(tail...);
}
template <class T>
void _W(const T &x) {
  cout << x;
}
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%" PRId64, x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T>
void _W(const vector<T> &x) {
  for (auto i = x.begin(); i != x.end(); _W(*i++))
    if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &...tail) {
  _W(head);
  putchar(sizeof...(tail) ? ' ' : '\n');
  W(tail...);
}
template <class T, class F = less<T>>
void sort_uniq(vector<T> &v, F f = F()) {
  sort(begin(v), end(v), f);
  v.resize(unique(begin(v), end(v)) - begin(v));
}
template <class T>
inline T bit(T x, int i) {
  return (x >> i) & 1;
}
template <class T>
inline bool chkmax(T &a, const T &b) {
  return b > a ? a = b, true : false;
}
template <class T>
inline bool chkmin(T &a, const T &b) {
  return b < a ? a = b, true : false;
}
template <class T>
using MaxHeap = priority_queue<T>;
template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T = int>
struct MaxFlow {
  struct E {
    int to, rev;
    T cap;
  };
  int n;
  vector<vector<E>> e;
  vector<int> cur, dis;
  void init(int _n) {
    n = _n;
    e.clear();
    e.resize(n);
    cur.resize(n);
    dis.resize(n);
  }
  template <bool dir>
  void _add(int a, int b, T c) {
    if (a == b) return;
    e[a].push_back(E{b, (int)e[b].size() - 0, c});
    e[b].push_back(E{a, (int)e[a].size() - 1, dir ? 0 : c});
  }
  void add(int a, int b, T c) { _add<true>(a, b, c); }
  void add_bi(int a, int b, T c) { _add<false>(a, b, c); }
  bool bfs(int s, int t) {
    memset(dis.data(), -1, dis.size() * sizeof(dis[0]));
    dis[s] = 0;
    vector<int> que{s};
    for (size_t i = 0; i < que.size() && dis[t] == -1; i++) {
      auto p = que[i];
      for (auto &j : e[p]) {
        if (j.cap == 0 || dis[j.to] != -1) continue;
        dis[j.to] = dis[p] + 1;
        que.push_back(j.to);
      }
    }
    return dis[t] != -1;
  }
  T go(int p, T c, int t) {
    if (p == t) return c;
    T ret = 0;
    for (size_t i = cur[p]; i < e[p].size() && ret < c; i++) {
      auto &ei = e[p][i];
      if (ei.cap == 0 || dis[ei.to] <= dis[p]) continue;
      auto f = go(ei.to, min(ei.cap, c - ret), t);
      ret += f;
      ei.cap -= f;
      e[ei.to][ei.rev].cap += f;
    }
    if (ret == 0) dis[p] = -1;
    return ret;
  }
  T maxflow(int s, int t) {
    T f = 0;
    while (bfs(s, t)) {
      memset(cur.data(), 0, cur.size() * sizeof(cur[0]));
      f += go(s, numeric_limits<T>::max(), t);
    }
    return f;
  }
  T maxflow() { return maxflow(0, n - 1); }
};
MaxFlow<> flow;
const int M = 55;
int n, h, m, l[M], r[M], x[M], c[M];
void main() {
  R(n, h, m);
  for (int i = 0; i < int(m); i++) R(l[i], r[i], x[i], c[i]);
  int fn = 0;
  auto f = [&](int t) {
    int ret = fn;
    fn += t;
    return ret;
  };
  int st = f(1);
  vector<int> b(n);
  for (int i = 0; i < int(n); i++) b[i] = f(h + 1);
  vector<int> z(m);
  for (int i = 0; i < int(m); i++) z[i] = f(1);
  int ed = f(1);
  flow.init(fn);
  for (int i = 0; i < int(n); i++)
    for (int j = (1); j <= int(h); j++) {
      flow.add(st, b[i] + j, 2 * j - 1);
      flow.add(b[i] + j, b[i] + j - 1, INT_MAX);
    }
  for (int i = 0; i < int(n); i++)
    for (int j = (0); j <= int(h); j++)
      for (int k = 0; k < int(m); k++)
        if (l[k] <= i + 1 && i + 1 <= r[k] && j > x[k])
          flow.add(b[i] + j, z[k], INT_MAX);
  for (int i = 0; i < int(m); i++) flow.add(z[i], ed, c[i]);
  int mf = flow.maxflow(st, ed);
  int ans = n * h * h - mf;
  W(ans);
}
}  // namespace shik
}  // namespace
int main() {
  shik::main();
  return 0;
}

```