#include <bits/stdc++.h>
using namespace std;
template <class T>
bool setmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  } else
    return false;
}
template <class T>
bool setmax(T &a, T b) {
  if (b > a) {
    a = b;
    return true;
  } else
    return false;
}
const long long INFI = 1LL << 60;
template <size_t N, size_t E>
struct MaxFlow {
  struct Edge {
    int y;
    long long w;
    Edge *next, *oppo;
  } edges[E * 2];
  int e_cnt;
  Edge *g[N], *ce[N];
  int n, src, dst;
  int d[N], q[N];
  bool build() {
    memset(d, 0, sizeof(d));
    d[q[1] = src] = 1;
    for (int ss = 0, tt = 1; ss < tt;) {
      int x = q[++ss];
      for (Edge *e = ce[x] = g[x]; e != NULL; e = e->next)
        if (e->w > 0 && d[e->y] == 0) d[q[++tt] = e->y] = d[x] + 1;
    }
    return d[dst] > 0;
  }
  long long find(int x, long long rest) {
    if (x == dst) return rest;
    long long ans = 0;
    for (Edge *&e = ce[x]; e != NULL; e = e->next)
      if (e->w > 0 && d[e->y] > d[x]) {
        long long cur = find(e->y, min(e->w, rest));
        e->w -= cur;
        e->oppo->w += cur;
        ans += cur;
        rest -= cur;
        if (rest == 0) break;
      }
    return ans;
  }
  void initialize(int n, int s, int t) {
    this->n = n;
    this->src = s;
    this->dst = t;
    memset(g, 0, sizeof(g));
    e_cnt = 0;
  }
  void add_edge(int x, int y, long long w1, long long w2) {
    Edge *e1 = &edges[e_cnt++], *e2 = &edges[e_cnt++];
    e1->y = y, e1->w = w1, e1->oppo = e2, e1->next = g[x], g[x] = e1;
    e2->y = x, e2->w = w2, e2->oppo = e1, e2->next = g[y], g[y] = e2;
  }
  long long compute() {
    long long ans = 0;
    while (build()) ans += find(src, INFI);
    return ans;
  }
};
const int MAXN = 55;
const int MAXM = 110;
const int MAXV = 210;
const int MAXNodes = MAXN * MAXV;
const int MAXEdges = MAXN * MAXV + MAXM * MAXV;
int a[MAXN], b[MAXN], c[MAXN], l[MAXN], r[MAXN];
int u[MAXM], v[MAXM], d[MAXM];
int n, m;
int vn[MAXN], vx[MAXN][MAXV], vi[MAXN][MAXV];
int vs, vt;
MaxFlow<MAXNodes, MAXEdges> net;
long long eva(int i, long long x) { return a[i] * x * x + b[i] * x + c[i]; }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (int)(1); i <= (int)(n); ++i)
    scanf("%d%d%d", a + i, b + i, c + i);
  for (int i = (int)(1); i <= (int)(n); ++i) scanf("%d%d", l + i, r + i);
  for (int i = (int)(1); i <= (int)(m); ++i)
    scanf("%d%d%d", u + i, v + i, d + i);
  int ncnt = 0;
  vs = ++ncnt;
  vt = ++ncnt;
  for (int i = (int)(1); i <= (int)(n); ++i) {
    vi[i][0] = ++ncnt;
    vn[i] = 0;
    for (int x = (int)(l[i]); x <= (int)(r[i]); ++x) {
      ++vn[i];
      vx[i][vn[i]] = x;
      vi[i][vn[i]] = ++ncnt;
    }
  }
  net.initialize(ncnt, vs, vt);
  long long BIG = 1001000, ans_t = 0;
  for (int i = (int)(1); i <= (int)(n); ++i) {
    ans_t += BIG;
    net.add_edge(vs, vi[i][0], INFI, 0);
    net.add_edge(vi[i][vn[i]], vt, INFI, 0);
    for (int j = (int)(1); j <= (int)(vn[i]); ++j)
      net.add_edge(vi[i][j - 1], vi[i][j], BIG - eva(i, vx[i][j]), 0);
  }
  for (int k = (int)(1); k <= (int)(m); ++k) {
    int i1 = u[k], i2 = v[k], d0 = d[k];
    for (int j1 = (int)(1); j1 <= (int)(vn[i1]); ++j1) {
      for (int j2 = (int)(vn[i2]); j2 >= (int)(1); --j2)
        if (vx[i1][j1] > vx[i2][j2] + d0) {
          net.add_edge(vi[i1][j1 - 1], vi[i2][j2], INFI, 0);
          break;
        }
    }
  }
  long long ans = ans_t - net.compute();
  cout << ans << endl;
  return 0;
}
