Problem: Nanami likes playing games, and is also really good at it. This day she was playing a new game which involved operating a power plant. Nanami's job is to control the generators in the plant and produce maximum output.

There are n generators in the plant. Each generator should be set to a generating level. Generating level is an integer (possibly zero or negative), the generating level of the i-th generator should be between li and ri (both inclusive). The output of a generator can be calculated using a certain quadratic function f(x), where x is the generating level of the generator. Each generator has its own function, the function of the i-th generator is denoted as fi(x).

However, there are m further restrictions to the generators. Let the generating level of the i-th generator be xi. Each restriction is of the form xu ≤ xv + d, where u and v are IDs of two different generators and d is an integer.

Nanami found the game tedious but giving up is against her creed. So she decided to have a program written to calculate the answer for her (the maximum total output of generators). Somehow, this became your job.

Input

The first line contains two integers n and m (1 ≤ n ≤ 50; 0 ≤ m ≤ 100) — the number of generators and the number of restrictions.

Then follow n lines, each line contains three integers ai, bi, and ci (|ai| ≤ 10; |bi|, |ci| ≤ 1000) — the coefficients of the function fi(x). That is, fi(x) = aix2 + bix + ci.

Then follow another n lines, each line contains two integers li and ri ( - 100 ≤ li ≤ ri ≤ 100).

Then follow m lines, each line contains three integers ui, vi, and di (1 ≤ ui, vi ≤ n; ui ≠ vi; |di| ≤ 200), describing a restriction. The i-th restriction is xui ≤ xvi + di.

Output

Print a single line containing a single integer — the maximum output of all the generators. It is guaranteed that there exists at least one valid configuration.

Examples

Input

3 3
0 1 0
0 1 1
0 1 2
0 3
1 2
-100 100
1 2 0
2 3 0
3 1 0


Output

9


Input

5 8
1 -8 20
2 -4 0
-1 10 -10
0 1 0
0 -1 1
1 9
1 4
0 10
3 11
7 9
2 1 3
1 2 3
2 3 3
3 2 3
3 4 3
4 3 3
4 5 3
5 4 3


Output

46

Note

In the first sample, f1(x) = x, f2(x) = x + 1, and f3(x) = x + 2, so we are to maximize the sum of the generating levels. The restrictions are x1 ≤ x2, x2 ≤ x3, and x3 ≤ x1, which gives us x1 = x2 = x3. The optimal configuration is x1 = x2 = x3 = 2, which produces an output of 9.

In the second sample, restrictions are equal to |xi - xi + 1| ≤ 3 for 1 ≤ i < n. One of the optimal configurations is x1 = 1, x2 = 4, x3 = 5, x4 = 8 and x5 = 7.

Solution (in CPP):

Solution:
#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;
}