#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const int MAXN = 10005;
const int MAXM = 300005;
int n, m, cnt, T, in[MAXN], out[MAXN], cur[MAXN], d[MAXN];
vector<int> Eid[MAXN];
struct Edge {
  int c, v;
} E[MAXM];
void AddEdge(int x, int y, int c) {
  E[++cnt].v = y, E[cnt].c = c;
  Eid[x].push_back(cnt);
  E[++cnt].v = x, E[cnt].c = 0;
  Eid[y].push_back(cnt);
}
bool BFS() {
  static int Q[MAXN];
  int x, i, ql, qr;
  for (i = 0; i <= T; i++) d[i] = -1;
  Q[ql = qr = 0] = 0;
  d[0] = 0;
  while (ql <= qr) {
    x = Q[ql++];
    for (i = 0; i < Eid[x].size(); i++) {
      const Edge &e = E[Eid[x][i]];
      if (e.c && d[e.v] == -1) {
        Q[++qr] = e.v;
        d[e.v] = d[x] + 1;
        if (e.v == T) return 1;
      }
    }
  }
  return 0;
}
int Aug(int x, int a) {
  if (x == T) return a;
  int aug = 0, delta;
  for (int &i = cur[x]; i < Eid[x].size(); i++) {
    Edge &e = E[Eid[x][i]];
    if (e.c && d[e.v] == d[x] + 1) {
      delta = Aug(e.v, min(a, e.c));
      if (delta) {
        e.c -= delta;
        E[Eid[x][i] ^ 1].c += delta;
        aug += delta;
        if (!(a -= delta)) break;
      }
    }
  }
  if (!aug) d[x] = -1;
  return aug;
}
int MaxFlow() {
  int flow = 0;
  while (BFS()) {
    for (int i = 0; i <= T; i++) cur[i] = 0;
    flow += Aug(0, inf);
  }
  return flow;
}
int a[MAXN], b[MAXN], c[MAXN], l[MAXN], r[MAXN], id[55][250], ans, tot;
inline int F(int i, int j) { return j * j * a[i] + j * b[i] + c[i]; }
int main() {
  cnt = -1;
  scanf("%d%d", &n, &m);
  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);
    int tmp = 0;
    for (int j = l[i]; j <= r[i]; j++) tmp = max(tmp, F(i, j));
    ans += tmp;
    for (int j = l[i] - 1; j <= r[i]; j++) id[i][j - l[i] + 1] = ++tot;
    for (int j = l[i]; j <= r[i]; j++)
      AddEdge(id[i][j - l[i]], id[i][j - l[i] + 1], tmp - F(i, j));
  }
  while (m--) {
    int u, v, d;
    scanf("%d%d%d", &u, &v, &d);
    for (int i = l[u]; i <= r[u]; i++) {
      int j = i - d;
      if (j > r[v]) j = r[v] + 1;
      if (j >= l[v]) AddEdge(id[u][i - l[u]], id[v][j - l[v]], inf);
    }
  }
  T = ++tot;
  for (int i = 1; i <= n; i++)
    AddEdge(0, id[i][0], inf), AddEdge(id[i][r[i] - l[i] + 1], T, inf);
  ans -= MaxFlow();
  cout << ans;
}
