#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, const U &b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, const U &b) {
  if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
template <class T1, class T2>
inline void gn(T1 &x1, T2 &x2) {
  gn(x1), gn(x2);
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
int V, E, src, tar;
int head[20000], work[20000], to[200000], cap[200000], nxt[200000];
int dis[20000], q[20000], qf, qb;
void init(int n, int s, int t) {
  V = n;
  E = 0;
  src = s;
  tar = t;
  memset(head, -1, sizeof(int) * V);
}
void add_edge(int u, int v, int c) {
  to[E] = v;
  cap[E] = c;
  nxt[E] = head[u];
  head[u] = E++;
  to[E] = u;
  cap[E] = 0;
  nxt[E] = head[v];
  head[v] = E++;
}
bool bfs() {
  memset(dis, -1, sizeof(dis));
  qf = qb = 0;
  q[qb++] = src;
  dis[src] = 0;
  while (qf < qb && dis[tar] == -1) {
    int u = q[qf++];
    for (int i = head[u]; i >= 0; i = nxt[i]) {
      int v = to[i];
      if (dis[v] == -1 && cap[i] > 0) {
        dis[v] = dis[u] + 1;
        q[qb++] = v;
      }
    }
  }
  return dis[tar] >= 0;
}
int dfs(int u, int &bot) {
  int v, bot1, delta;
  if (u == tar) return bot;
  for (int &i = work[u]; i >= 0; i = nxt[i]) {
    int v = to[i];
    if (dis[v] != dis[u] + 1 || cap[i] == 0) continue;
    bot1 = min(bot, cap[i]);
    if (delta = dfs(v, bot1)) {
      cap[i] -= delta;
      cap[i ^ 1] += delta;
      bot = bot1;
      return delta;
    }
  }
  return 0;
}
int dinic() {
  int ans = 0, delta, bot;
  while (bfs()) {
    memcpy(work, head, sizeof(int) * V);
    delta = 0x3f3f3f3f;
    while (delta = dfs(src, bot = 0x3f3f3f3f)) ans += delta;
  }
  return ans;
}
int a[55], b[55], c[55], l[55], r[55];
int main() {
  int n, m;
  cin >> n >> m;
  int M = 2000000;
  init(202 * n + 10, 0, 202 * n + 1);
  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 i = 1; i <= n; i++) {
    add_edge(src, i, 0x3f3f3f3f);
    add_edge(i + n * 201, tar, 0x3f3f3f3f);
    for (int j = -100; j <= 100; j++) {
      if (j < l[i] || j > r[i])
        add_edge(i + n * (j + 100), i + n * (j + 101), 0x3f3f3f3f);
      else
        add_edge(i + n * (j + 100), i + n * (j + 101),
                 M - (a[i] * j * j + b[i] * j + c[i]));
    }
  }
  for (int i = 0; i < m; i++) {
    int u, v, d;
    scanf("%d %d %d", &u, &v, &d);
    for (int j = -100; j <= 101; j++) {
      if (j + d >= -100 && j + d <= 101) {
        add_edge(u + n * (j + d + 100), v + n * (j + 100), 0x3f3f3f3f);
      }
    }
  }
  int ans = dinic();
  ans = M * n - ans;
  cout << ans << endl;
  return 0;
}
