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 <typename T, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
template <class T>
inline void print(T first) {
  if (first < 0) {
    putchar('-');
    return print(-first);
  }
  if (first < 10) {
    putchar('0' + first);
    return;
  }
  print(first / 10);
  putchar(first % 10 + '0');
}
template <class T, class T1>
inline void gn(T &first, T1 &second) {
  gn(first);
  gn(second);
}
template <class T, class T1, class T2>
inline void gn(T &first, T1 &second, T2 &z) {
  gn(first);
  gn(second);
  gn(z);
}
template <class T>
inline void println(T first) {
  print(first);
  putchar('\n');
}
template <class T>
inline void printsp(T first) {
  print(first);
  putchar(' ');
}
template <class T1, class T2>
inline void print(T1 x1, T2 x2) {
  printsp(x1), println(x2);
}
template <class T1, class T2, class T3>
inline void print(T1 x1, T2 x2, T3 x3) {
  printsp(x1), printsp(x2), println(x3);
}
template <class T1, class T2, class T3, class T4>
inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {
  printsp(x1), printsp(x2), printsp(x3), println(x4);
}
long long power(long long a, long long b, long long m, long long ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
long long a[155], b[155], c[155];
long long L[155], R[155];
long long first[155], second[155], d[155];
long long V, E, src, tar;
long long head[20000], work[20000], dis[20000];
long long to[400000], cap[400000], nxt[400000];
long long q[20000], qf, qb;
void init(long long n, long long s, long long t) {
  memset(head, -1, sizeof(long long) * n);
  V = n;
  E = 0;
  src = s;
  tar = t;
}
void add_edge(long long u, long long v, long long c) {
  to[E] = v;
  cap[E] = c;
  nxt[E] = head[u];
  head[u] = E++;
  to[E] = u;
  cap[E] = 0;
  nxt[E] = head[v];
  head[v] = E++;
}
bool bfs() {
  memset(dis, -1, sizeof(dis));
  qf = qb = 0;
  q[qb++] = src;
  dis[src] = 0;
  while (qf < qb && dis[tar] == -1) {
    long long u = q[qf++];
    for (long long i = head[u]; i >= 0; i = nxt[i]) {
      long long v = to[i];
      if (dis[v] == -1 && cap[i] > 0) {
        dis[v] = dis[u] + 1;
        q[qb++] = v;
      }
    }
  }
  return dis[tar] >= 0;
}
long long dfs(long long u, long long &bot) {
  long long v, bot1, delta;
  if (u == tar) return bot;
  for (long long &i = work[u]; i >= 0; i = nxt[i]) {
    long long v = to[i];
    if (dis[v] != dis[u] + 1 || cap[i] == 0) continue;
    bot1 = min(bot, cap[i]);
    if (delta = dfs(v, bot1)) {
      cap[i] -= delta;
      cap[i ^ 1] += delta;
      bot = bot1;
      return delta;
    }
  }
  return 0;
}
long long dinic() {
  long long ans = 0, delta, bot;
  while (bfs()) {
    memcpy(work, head, sizeof(long long) * V);
    while (delta = dfs(src, bot = 0x3f3f3f3f3f3f3fLL)) ans += delta;
  }
  return ans;
}
int main() {
  long long n, m;
  gn(n, m);
  init(n * 202 + 10, 0, n * 202 + 1);
  for (long long i = 1; i <= n; i++) {
    gn(a[i], b[i], c[i]);
  }
  for (long long i = 1; i <= n; i++) {
    gn(L[i], R[i]);
  }
  for (long long i = 0; i < m; i++) {
    gn(first[i], second[i], d[i]);
  }
  long long mx = 200000000000LL;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= 201; j++) {
      if (j >= L[i] + 101 && j <= R[i] + 101)
        add_edge(i + n * (j - 1), i + n * j,
                 mx - (a[i] * (j - 101) * (j - 101) + b[i] * (j - 101) + c[i]));
      else
        add_edge(i + n * (j - 1), i + n * j, 0x3f3f3f3f3f3f3fLL);
    }
    add_edge(src, i, 0x3f3f3f3f3f3f3fLL);
    add_edge(i + 201 * n, tar, 0x3f3f3f3f3f3f3fLL);
  }
  for (long long i = 0; i < m; i++) {
    for (long long j = max(0LL, d[i]); j <= 201; j++) {
      long long k = j - d[i];
      if (k < 0 || k > 201) continue;
      add_edge(first[i] + n * j, second[i] + n * k, 0x3f3f3f3f3f3f3fLL);
    }
  }
  println(-dinic() + n * mx);
  return 0;
}