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;
int rd() {
  char c = getchar();
  int t = 0, f = 1;
  while (!isdigit(c)) f = (c == '-') ? -1 : 1, c = getchar();
  while (isdigit(c)) t = t * 10 + c - 48, c = getchar();
  return t * f;
}
void wt(int x) {
  if (x < 0)
    putchar('-'), wt(-x);
  else {
    if (x > 9) wt(x / 10);
    putchar(x % 10 + 48);
  }
}
const int N = 15005;
const int mxf = 1e9;
int head[N], cnt = 1;
struct Edge {
  int to, nxt, fl;
} E[2000005];
void Link(int u, int v, int w) {
  E[++cnt] = (Edge){v, head[u], w}, head[u] = cnt;
  E[++cnt] = (Edge){u, head[v], 0}, head[v] = cnt;
}
int inf;
int pos[55][2000];
int d[N], n, m, a[N], b[N], c[N], l[N], r[N], S, T = N - 1, id, ans;
bool bfs() {
  static int h, t, x, Q[N];
  memset(d, -1, sizeof d);
  h = 0, Q[t = 1] = S, d[S] = 0;
  while (h < t) {
    x = Q[++h];
    for (int y, i = head[x]; i; i = E[i].nxt)
      if (d[y = E[i].to] == -1 && E[i].fl) {
        d[y] = d[x] + 1;
        if (y == T)
          return 1;
        else
          Q[++t] = y;
      }
  }
  return 0;
}
int dfs(int x, int fl) {
  int w = 0, Ret;
  if (x == T || !fl) return fl;
  for (int y, i = head[x]; i; i = E[i].nxt)
    if (d[y = E[i].to] == d[x] + 1 && (Ret = dfs(y, min(fl - w, E[i].fl))))
      E[i].fl -= Ret, E[i ^ 1].fl += Ret, w += Ret;
  if (!w) d[x] = -1;
  return w;
}
int main() {
  n = rd(), m = rd(), inf = -1 << 30;
  for (int i = ((int)1); i <= ((int)n); i++)
    a[i] = rd(), b[i] = rd(), c[i] = rd();
  for (int i = ((int)1); i <= ((int)n); i++) {
    l[i] = rd(), r[i] = rd();
    for (int j = ((int)l[i]); j <= ((int)r[i]); j++)
      inf = max(inf, a[i] * j * j + b[i] * j + c[i]);
  }
  for (int i = ((int)1); i <= ((int)n); i++) {
    for (int j = ((int)l[i] - 1); j <= ((int)r[i]); j++) pos[i][j] = ++id;
    Link(S, pos[i][l[i] - 1], mxf);
    Link(pos[i][r[i]], T, mxf);
    for (int j = ((int)l[i]); j <= ((int)r[i]); j++)
      Link(pos[i][j - 1], pos[i][j], inf - a[i] * j * j - b[i] * j - c[i]);
  }
  while (m--) {
    int u = rd(), v = rd(), d = rd();
    for (int i = ((int)l[u] - 1); i <= ((int)r[u] - 1); i++)
      if (i - d >= l[v] && i - d <= r[v]) Link(pos[u][i], pos[v][i - d], mxf);
  }
  ans = inf * n;
  while (bfs()) ans -= dfs(S, mxf);
  printf("%d\n", ans);
  return 0;
}