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;
const int N = 10100;
const int inf2 = 1e8;
const int inf = INT_MAX;
const int mx = 210000;
struct Edge {
  int nxt, to, ci;
} e[N * 8];
int h[N], e_num = 1;
void add(int from, int to, int ci) {
  e[++e_num] = (Edge){h[from], to, ci};
  h[from] = e_num;
}
void _add(int u, int v, int ci) {
  add(u, v, ci);
  add(v, u, 0);
}
int n, m, S, T, d[N];
queue<int> q;
bool bfs() {
  while (!q.empty()) q.pop();
  memset(d, 0, sizeof(d));
  d[S] = 1;
  q.push(S);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = h[u]; i; i = e[i].nxt) {
      int v = e[i].to;
      if (e[i].ci && !d[v]) {
        d[v] = d[u] + 1;
        if (v == T) return 1;
        q.push(v);
      }
    }
  }
  return 0;
}
int dinic(int u, int flow) {
  if (u == T) return flow;
  int rst = flow;
  for (int i = h[u]; i && rst; i = e[i].nxt) {
    int v = e[i].to;
    if (e[i].ci && d[v] == d[u] + 1) {
      int k = dinic(v, min(rst, e[i].ci));
      if (!k) d[v] = 0;
      rst -= k;
      e[i].ci -= k;
      e[i ^ 1].ci += k;
    }
  }
  return flow - rst;
}
int a[52], b[52], c[52], l[52], r[52];
int f(int num, int x) {
  return -1 * (a[num] * x * x + b[num] * x + c[num]) + mx;
}
int num[52][220];
int w[52][52], vis[52], cnm[52][52];
int u[110], v[110], di[110];
int main() {
  scanf("%d%d", &n, &m);
  int tot = 0;
  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 j = l[i]; j <= r[i]; j++) num[i][j + 100] = ++tot;
  }
  S = tot + 1, T = S + 1;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &u[i], &v[i], &di[i]);
    swap(u[i], v[i]);
    if (!cnm[u[i]][v[i]])
      w[u[i]][v[i]] = di[i], cnm[u[i]][v[i]] = 1;
    else
      w[u[i]][v[i]] = min(w[u[i]][v[i]], di[i]);
    if (!vis[v[i]]) vis[v[i]] = 1, q.push(v[i]);
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    for (int v = 1; v <= n; v++)
      if (cnm[v][u] && l[v] < l[u] - w[v][u]) {
        l[v] = l[u] - w[v][u];
        if (!vis[v]) vis[v] = 1, q.push(v);
      }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (cnm[i][j] && !vis[i]) vis[i] = 1, q.push(i);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    for (int v = 1; v <= n; v++)
      if (cnm[u][v] && r[v] > r[u] + w[u][v]) {
        r[v] = r[u] + w[u][v];
        if (!vis[v]) vis[v] = 1, q.push(v);
      }
  }
  for (int i = 1; i <= n; i++) _add(S, num[i][r[i] + 100], inf2);
  for (int i = 1; i <= n; i++) {
    for (int j = r[i]; j > l[i]; j--)
      _add(num[i][j + 100], num[i][j + 99], f(i, j));
    _add(num[i][l[i] + 100], T, f(i, l[i]));
  }
  for (int i = 1; i <= m; i++) {
    for (int j = l[u[i]]; j <= r[v[i]] - di[i]; j++)
      _add(num[u[i]][j + 100], num[v[i]][j + 100 + di[i]], inf2);
  }
  int flow, maxflow = 0;
  while (bfs())
    while ((flow = dinic(S, inf))) maxflow += flow;
  printf("%d", -maxflow + n * mx);
  return 0;
}