#include <bits/stdc++.h>
using namespace std;
const int maxs = 60;
const int maxn = 20010;
const int maxm = 1000000;
const int inf = 0x3f3f3f3f;
const int Lim = 10000000;
int n, m, ed = 1, S, T;
int he[maxn], dep[maxn], cur[maxn];
struct Edge {
  int v, nx, cap;
  Edge(int _v, int _nx, int _cap) {
    v = _v;
    nx = _nx;
    cap = _cap;
  }
  Edge() { ; }
} e[maxm];
void Add(int u, int v, int cap) {
  e[++ed] = Edge(v, he[u], cap);
  he[u] = ed;
  e[++ed] = Edge(u, he[v], 0);
  he[v] = ed;
}
struct Line {
  int A, B, C, l, r, L;
} poi[maxs];
int F(int id, int x) { return poi[id].A * x * x + poi[id].B * x + poi[id].C; }
bool Bfs() {
  memset(dep, -1, sizeof(dep));
  queue<int> Q;
  Q.push(S);
  dep[S] = 0;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = he[u]; i; i = e[i].nx) {
      if (e[i].cap && dep[e[i].v] == -1) {
        dep[e[i].v] = dep[u] + 1;
        Q.push(e[i].v);
      }
    }
  }
  return dep[T] != -1;
}
int Dfs(int x, int w) {
  if (x == T || w == 0) return w;
  int res = 0;
  for (int &i = cur[x]; i; i = e[i].nx) {
    if (e[i].cap && dep[e[i].v] == dep[x] + 1) {
      int tmp = Dfs(e[i].v, min(e[i].cap, w));
      res += tmp;
      w -= tmp;
      e[i].cap -= tmp;
      e[i ^ 1].cap += tmp;
      if (w == 0) break;
    }
  }
  if (!res) dep[x] = -1;
  return res;
}
void Dinic() {
  int Flow = Lim * n;
  while (Bfs()) {
    memcpy(cur, he, sizeof(he));
    Flow -= Dfs(S, inf);
  }
  printf("%d", Flow);
}
void Graph() {
  scanf("%d%d", &n, &m);
  S = 0;
  T = 14999;
  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d%d", &poi[i].A, &poi[i].B, &poi[i].C);
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &poi[i].l, &poi[i].r);
    Add(S, ++cnt, inf);
    poi[i].L = cnt;
    for (int j = poi[i].l; j <= poi[i].r; ++j) {
      ++cnt;
      Add(cnt - 1, cnt, Lim - F(i, j));
    }
    Add(cnt, T, inf);
  }
  int u, v, d;
  while (m--) {
    scanf("%d%d%d", &u, &v, &d);
    int tmp = poi[u].L - 1;
    for (int j = poi[u].l - 1; j <= poi[u].r; ++j) {
      tmp++;
      if (j - d >= poi[v].l - 1 && j - d <= poi[v].r) {
        Add(tmp, poi[v].L + j - d - poi[v].l + 1, inf);
      }
    }
  }
}
int main() {
  Graph();
  Dinic();
  return 0;
}
