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 MAXN = 10005;
const int MAXM = 880010;
const int INF = 0x3f3f3f3f;
struct Node {
  int from, to, next;
  int cap;
} edge[MAXM];
int tol;
int head[MAXN];
int dep[MAXN];
int gap[MAXN];
int n;
void init() {
  tol = 0;
  memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int w) {
  edge[tol].from = u;
  edge[tol].to = v;
  edge[tol].cap = w;
  edge[tol].next = head[u];
  head[u] = tol++;
  edge[tol].from = v;
  edge[tol].to = u;
  edge[tol].cap = 0;
  edge[tol].next = head[v];
  head[v] = tol++;
}
void BFS(int start, int end) {
  memset(dep, -1, sizeof(dep));
  memset(gap, 0, sizeof(gap));
  gap[0] = 1;
  int que[MAXN];
  int front, rear;
  front = rear = 0;
  dep[end] = 0;
  que[rear++] = end;
  while (front != rear) {
    int u = que[front++];
    if (front == MAXN) front = 0;
    for (int i = head[u]; i != -1; i = edge[i].next) {
      int v = edge[i].to;
      if (dep[v] != -1) continue;
      que[rear++] = v;
      if (rear == MAXN) rear = 0;
      dep[v] = dep[u] + 1;
      ++gap[dep[v]];
    }
  }
}
int SAP(int start, int end) {
  int res = 0;
  BFS(start, end);
  int cur[MAXN];
  int S[MAXN];
  int top = 0;
  memcpy(cur, head, sizeof(head));
  int u = start;
  int i;
  while (dep[start] < n) {
    if (u == end) {
      int temp = INF;
      int inser;
      for (i = 0; i < top; i++)
        if (temp > edge[S[i]].cap) {
          temp = edge[S[i]].cap;
          inser = i;
        }
      for (i = 0; i < top; i++) {
        edge[S[i]].cap -= temp;
        edge[S[i] ^ 1].cap += temp;
      }
      res += temp;
      top = inser;
      u = edge[S[top]].from;
    }
    if (u != end && gap[dep[u] - 1] == 0) break;
    for (i = cur[u]; i != -1; i = edge[i].next)
      if (edge[i].cap != 0 && dep[u] == dep[edge[i].to] + 1) break;
    if (i != -1) {
      cur[u] = i;
      S[top++] = i;
      u = edge[i].to;
    } else {
      int min = n;
      for (i = head[u]; i != -1; i = edge[i].next) {
        if (edge[i].cap == 0) continue;
        if (min > dep[edge[i].to]) {
          min = dep[edge[i].to];
          cur[u] = i;
        }
      }
      --gap[dep[u]];
      dep[u] = min + 1;
      ++gap[dep[u]];
      if (u != start) u = edge[S[--top]].from;
    }
  }
  return res;
}
int A[55], B[55], C[55], mx[55];
int L[55], R[55], id[55][205];
inline int cal(int x, int y) { return A[x] * y * y + B[x] * y + C[x]; }
int main() {
  int N, M;
  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);
  }
  for (int i = 1; i <= N; ++i) {
    mx[i] = -INF;
    for (int j = L[i]; j <= R[i]; ++j) {
      mx[i] = max(mx[i], cal(i, j));
    }
  }
  int S = 0, T = 1, label = 1;
  for (int i = 1; i <= N; ++i) {
    for (int j = L[i] + 100; j <= R[i] + 100; ++j) {
      id[i][j] = ++label;
    }
  }
  init();
  for (int i = 1; i <= N; ++i) {
    addedge(S, id[i][L[i] + 100], mx[i] - cal(i, L[i]));
    for (int j = L[i] + 101; j <= R[i] + 100; ++j) {
      addedge(id[i][j - 1], id[i][j], mx[i] - cal(i, j - 100));
    }
    addedge(id[i][R[i] + 100], T, INF);
  }
  for (int i = 0; i < M; ++i) {
    int u, v, d, p = S;
    scanf("%d%d%d", &u, &v, &d);
    for (int j = L[u]; j <= R[u]; p = id[u][j + 100], ++j) {
      int x = j - d - 1;
      if (x < L[v]) continue;
      if (x >= R[v]) {
        addedge(p, T, INF);
        continue;
      }
      addedge(p, id[v][x + 100], INF);
    }
  }
  int ans = 0;
  for (int i = 1; i <= N; ++i) ans += mx[i];
  n = label + 3;
  ans -= SAP(S, T);
  printf("%d\n", ans);
  return 0;
}