#include <bits/stdc++.h>
using namespace std;
const int LIM = 1000000;
int n, m, a[110], b[110], c[110], L[110], R[110], id[110][2100], tot;
namespace MaxFlow {
const int N = 201000, M = 2001000;
int head[N], cur[N], dep[N], cnt, S, T, res;
struct node {
  int to, next, val;
} edge[M];
void ae(int u, int v, int w) {
  edge[cnt].next = head[u], edge[cnt].to = v, edge[cnt].val = w,
  head[u] = cnt++;
  edge[cnt].next = head[v], edge[cnt].to = u, edge[cnt].val = 0,
  head[v] = cnt++;
}
queue<int> q;
inline bool bfs() {
  memset(dep, 0, sizeof(dep)), q.push(S), dep[S] = 1;
  while (!q.empty()) {
    register int x = q.front();
    q.pop();
    for (register int i = cur[x] = head[x]; i != -1; i = edge[i].next)
      if (edge[i].val && !dep[edge[i].to])
        dep[edge[i].to] = dep[x] + 1, q.push(edge[i].to);
  }
  return dep[T] > 0;
}
bool reach;
inline int dfs(int x, int flow) {
  if (x == T) {
    res += flow, reach = true;
    return flow;
  }
  int used = 0;
  for (register int &i = cur[x]; i != -1; i = edge[i].next) {
    if (!edge[i].val || dep[edge[i].to] != dep[x] + 1) continue;
    register int ff = dfs(edge[i].to, min(edge[i].val, flow - used));
    if (ff) {
      edge[i].val -= ff, edge[i ^ 1].val += ff, used += ff;
      if (used == flow) break;
    }
  }
  return used;
}
inline void Dinic() {
  while (bfs()) {
    reach = true;
    while (reach) reach = false, dfs(S, 0x3f3f3f3f);
  }
}
}  // namespace MaxFlow
using namespace MaxFlow;
int main() {
  scanf("%d%d", &n, &m), S = 1, T = 2, tot = 2, memset(head, -1, sizeof(head));
  for (int i = 1; i <= n; i++) scanf("%d%d%d", &a[i], &b[i], &c[i]);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &L[i], &R[i]);
    for (int j = L[i]; j <= R[i]; j++) id[i][(j) + L[i]] = ++tot;
    ae(S, id[i][(L[i]) + L[i]],
       LIM - (a[i] * (L[i]) * (L[i]) + b[i] * (L[i]) + c[i]));
    for (int j = L[i] + 1; j <= R[i]; j++)
      ae(id[i][(j - 1) + L[i]], id[i][(j) + L[i]],
         LIM - (a[i] * (j) * (j) + b[i] * (j) + c[i]));
    ae(id[i][(R[i]) + L[i]], T, 0x3f3f3f3f);
  }
  for (int i = 1, u, v, d; i <= m; i++) {
    scanf("%d%d%d", &u, &v, &d);
    if (L[v] <= L[u] - d - 1 && L[u] - d - 1 <= R[v])
      ae(S, id[v][(L[u] - d - 1) + L[v]], 0x3f3f3f3f);
    for (int j = L[u]; j <= R[u]; j++) {
      if (j - d < L[v]) continue;
      if (j - d <= R[v])
        ae(id[u][(j) + L[u]], id[v][(j - d) + L[v]], 0x3f3f3f3f);
      else
        ae(id[u][(j) + L[u]], T, 0x3f3f3f3f);
    }
  }
  Dinic();
  printf("%d\n", LIM * n - res);
  return 0;
}
