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 MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long fpow(long long n, long long k, int p = MOD) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
  return a > val ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
  return a < val ? a = val, 1 : 0;
}
void addmod(int& a, int val, int p = MOD) {
  if ((a = (a + val)) >= p) a -= p;
}
void submod(int& a, int val, int p = MOD) {
  if ((a = (a - val)) < 0) a += p;
}
int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
const int oo = (int)1e9;
struct Dinic {
  int n, s, t, E, adj[1000010], next[1000010], last[100010], run[100010],
      level[100010], que[100010];
  int flow[1000010], cap[1000010];
  void init(int _n, int _s, int _t) {
    n = _n;
    s = _s;
    t = _t;
    E = 0;
    fill_n(last, n + 1, -1);
  }
  void add(int u, int v, int c1, int c2) {
    adj[E] = v;
    flow[E] = 0;
    cap[E] = c1;
    next[E] = last[u];
    last[u] = E++;
    adj[E] = u;
    flow[E] = 0;
    cap[E] = c2;
    next[E] = last[v];
    last[v] = E++;
  }
  int bfs() {
    fill_n(level, n + 1, -1);
    level[s] = 0;
    int qsize = 0;
    que[qsize++] = s;
    for (int i = 0; i < qsize; i++) {
      for (int u = que[i], e = last[u]; ~e; e = next[e]) {
        int v = adj[e];
        if (flow[e] < cap[e] && !~level[v]) {
          level[v] = level[u] + 1;
          que[qsize++] = v;
        }
      }
    }
    return level[t] != -1;
  }
  int dfs(int u, int bot) {
    if (u == t) return bot;
    for (int& e = run[u]; ~e; e = next[e]) {
      int v = adj[e];
      int delta = 0;
      if (level[v] == level[u] + 1 && flow[e] < cap[e] &&
          (delta = dfs(v, min(bot, cap[e] - flow[e]))) > 0) {
        flow[e] += delta;
        flow[e ^ 1] -= delta;
        return delta;
      }
    }
    return 0;
  }
  int maxflow() {
    int total = 0;
    while (bfs()) {
      for (int i = 0; i <= n; i++) run[i] = last[i];
      for (int delta = dfs(s, oo); delta > 0; delta = dfs(s, oo))
        total += delta;
    }
    return total;
  }
} dinic;
const int maxn = 60;
int n, m;
int a[maxn];
int b[maxn];
int c[maxn];
int l[maxn];
int r[maxn];
int id[maxn][210];
int f(int i, int x) { return a[i] * x * x + b[i] * x + c[i]; }
void solve() {
  cin >> n >> m;
  int tot = 0;
  for (int i = (0); i < (n); i++) cin >> a[i] >> b[i] >> c[i];
  int dmax = -INF;
  for (int i = (0); i < (n); i++) {
    cin >> l[i] >> r[i];
    for (int j = (l[i] - 1); j < (r[i] + 1); j++) {
      id[i][j - l[i] + 1] = tot++;
      chkmax(dmax, f(i, j));
    }
  }
  int s = tot++, t = tot++;
  dinic.init(tot, s, t);
  for (int i = (0); i < (n); i++) {
    dinic.add(s, id[i][0], INF, 0);
    dinic.add(id[i][r[i] - l[i] + 1], t, INF, 0);
    for (int j = (l[i] - 1); j < (r[i]); j++) {
      dinic.add(id[i][j - l[i] + 1], id[i][j - l[i] + 2], dmax - f(i, j + 1),
                0);
    }
  }
  while (m--) {
    int u, v, d;
    cin >> u >> v >> d;
    u--;
    v--;
    for (int i = (l[u] - 1); i < (r[u] + 1); i++) {
      if (i - d >= l[v] - 1 && i - d <= r[v]) {
        dinic.add(id[u][i - l[u] + 1], id[v][i - d - l[v] + 1], INF, 0);
      }
    }
  }
  cout << dmax * n - dinic.maxflow() << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}