#include <bits/stdc++.h>
using namespace std;
namespace Flow {
int S = 1, T = 2, D = 1, C = 2, Q[200005], st[200005], Dis[200005];
struct Lin {
  int v, f, next;
} E[200005];
void Link(int u, int v, int f) {
  E[++D] = (Lin){v, f, st[u]};
  st[u] = D;
  E[++D] = (Lin){u, 0, st[v]};
  st[v] = D;
}
bool BFS() {
  for (int i = 1; i <= C; i++) Dis[i] = -1;
  Dis[Q[1] = S] = 0;
  for (int l = 0, r = 1; l < r;) {
    int u = Q[++l];
    for (int i = st[u], v; i; i = E[i].next)
      if (E[i].f && Dis[v = E[i].v] == -1) {
        Dis[Q[++r] = v] = Dis[u] + 1;
        if (v == T) return 1;
      }
  }
  return 0;
}
int DFS(int u, int Lim) {
  if (u == T) return Lim;
  int c = 0;
  for (int i = st[u], v; i; i = E[i].next)
    if (E[i].f && Dis[u] + 1 == Dis[v = E[i].v]) {
      int t = DFS(v, ((E[i].f) < (Lim - c) ? (E[i].f) : (Lim - c)));
      if (t) {
        E[i].f -= t;
        E[i ^ 1].f += t;
        c += t;
        if (c == Lim) break;
      } else
        Dis[v] = -1;
    }
  return c;
}
int Max_Flow() {
  int ret = 0;
  for (; BFS();) ret += DFS(S, (1 << 30));
  return ret;
}
};  // namespace Flow
int N, M, A[55], B[55], C[55], L[55], R[55], G[55];
int main() {
  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]);
    Flow::Link(Flow::S, ++Flow::C, (1 << 30));
    G[i] = Flow::C;
    for (int j = L[i]; j <= R[i]; j++)
      Flow::Link(Flow::C, Flow::C + 1,
                 1000000 - (A[i] * j * j + B[i] * j + C[i])),
          ++Flow::C;
    Flow::Link(Flow::C, Flow::T, (1 << 30));
  }
  for (int i = 1; i <= M; i++) {
    int u, v, d;
    scanf("%d%d%d", &u, &v, &d);
    for (int j = L[u]; j <= R[u]; j++)
      if (j - d >= L[v] && j - d <= R[v])
        Flow::Link(G[u] + j - L[u], G[v] + j - d - L[v], (1 << 30));
      else if (j - d > R[v])
        Flow::Link(G[u] + j - L[u], Flow::T, (1 << 30));
  }
  printf("%d\n", N * 1000000 - Flow::Max_Flow());
}
