#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;
}
int a[51], b[51], c[51], l[51], r[51];
int st[51], ed[51];
struct Dinic {
  int V, E, src, tar;
  int head[10100], work[10100], dis[10100];
  int to[60600], cap[60600], nxt[60600];
  int q[10100], qf, qb;
  inline void calc(int n, int m) {
    for (int i = 1; i <= n; i++) {
      add(src, st[i], 0x3f3f3f3f);
      for (int j = st[i]; j < ed[i]; j++) {
        int x = j - st[i] + l[i];
        add(j, j + 1, 1999728 - a[i] * x * x - b[i] * x - c[i]);
      }
      add(ed[i], tar, 0x3f3f3f3f);
    }
    while (m--) {
      int u, v, d;
      scanf("%d %d %d", &u, &v, &d);
      for (int i = l[u], j = i - d; i <= r[u] + 1; i++, j++)
        if (l[v] <= j && j <= r[v] + 1) {
          add(i - l[u] + st[u], j - l[v] + st[v], 0x3f3f3f3f);
        }
    }
    int ans = n * 1999728 - dinic();
    printf("%d", ans);
  }
  inline void init(int n, int s, int t) {
    V = n + 1;
    E = 0;
    src = s;
    tar = t;
    for (int i = 0; i <= V; i++) head[i] = -1;
  }
  inline void add(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++;
  }
  inline bool bfs() {
    for (int i = 0; i <= V; i++) dis[i] = -1;
    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;
  }
  inline 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;
  }
  inline 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;
  }
} dinic;
int main() {
  int n, m, cur = 0;
  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);
    st[i] = cur;
    cur += r[i] - l[i] + 1;
    ed[i] = cur++;
  }
  int src = cur++, tar = cur++;
  dinic.init(cur, src, tar);
  dinic.calc(n, m);
}
