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;
namespace flow {
class FlowNetwork {
  struct Edge {
    int v;
    int cap;
    int flow;
    Edge* back;
    Edge(int _v, int _cap) : v(_v), cap(_cap), flow(0) {}
  };

 public:
  int N;
  vector<list<Edge>> adj;
  int S, T;
  vector<long long> extra;
  queue<int> unprocessed;
  vector<int> height;
  int raises;

 private:
  void _push(Edge& e, int f) {
    if (e.v != S && e.v != T && extra[e.v] == 0 && f > 0) {
      unprocessed.push(e.v);
    }
    e.flow += f;
    e.back->flow -= f;
    extra[e.v] += f;
    extra[e.back->v] -= f;
  }
  void _raise(int u) {
    height[u] = 2 * N;
    for (Edge& e : adj[u]) {
      if (e.flow < e.cap) height[u] = min(height[u], height[e.v] + 1);
    }
    ++raises;
  }
  void _bfs(int s, vector<bool>& visited) {
    queue<int> Q;
    Q.push(s);
    while (!Q.empty()) {
      int u = Q.front();
      Q.pop();
      for (Edge& e : adj[u]) {
        if (!visited[e.v] && e.back->flow < e.back->cap) {
          height[e.v] = height[u] + 1;
          visited[e.v] = true;
          Q.push(e.v);
        }
      }
    }
  }
  void _relabel() {
    vector<bool> visited(N);
    visited[S] = visited[T] = true;
    _bfs(T, visited);
    _bfs(S, visited);
  }
  void _process(int u) {
    while (extra[u] > 0) {
      for (Edge& e : adj[u]) {
        if (e.flow == e.cap || height[e.v] != height[u] - 1) continue;
        _push(e, min(extra[u], (long long)(e.cap - e.flow)));
        if (!extra[u]) return;
      }
      if (raises >= N / 2) {
        raises = 0;
        _relabel();
      } else {
        _raise(u);
      }
    }
  }

 public:
  FlowNetwork(int _N) : N(_N) {
    adj = vector<list<Edge>>(N);
    extra = vector<long long>(N);
    height = vector<int>(N);
  }
  void addEdge(int u, int v, int cap) {
    adj[u].push_back(Edge(v, cap));
    adj[v].push_back(Edge(u, 0));
    adj[u].back().back = &adj[v].back();
    adj[v].back().back = &adj[u].back();
  }
  long long computeFlow(int s, int t) {
    S = s;
    T = t;
    for (Edge& e : adj[S]) {
      _push(e, e.cap);
    }
    for (int i = 0; i < (N); ++i) height[i] = 0;
    height[S] = N;
    raises = 0;
    while (!unprocessed.empty()) {
      int v = unprocessed.front();
      unprocessed.pop();
      _process(v);
    }
    return extra[T];
  }
  vector<pair<int, int>> cut() {
    _relabel();
    vector<pair<int, int>> result;
    for (int i = 0; i < (N); ++i)
      for (Edge& e : adj[i]) {
        if (height[i] >= N && height[e.v] < N && e.cap > 0)
          result.push_back(pair<int, int>(i, e.v));
      }
    return result;
  }
};
}  // namespace flow
int A[100], B[100], C[100], L[100], R[100];
int N;
const int INF = 2000000000;
const int BIG = 100000000;
int main() {
  int M;
  scanf("%d%d", &N, &M);
  for (int i = 0; i < (N); ++i) scanf("%d%d%d", &A[i], &B[i], &C[i]);
  for (int i = 0; i < (N); ++i) scanf("%d%d", &L[i], &R[i]);
  int S = 0, T = 1;
  flow::FlowNetwork net(205 * N);
  for (int i = 0; i < (N); ++i) {
    for (int j = -101; j <= 101; ++j) {
      int v = 205 * i + 103 + j;
      if (j == -101) net.addEdge(S, v, INF);
      if (j < 101) {
        int value = (j >= L[i] && j <= R[i])
                        ? BIG - (A[i] * j * j + B[i] * j + C[i])
                        : INF;
        net.addEdge(v, v + 1, value);
      } else
        net.addEdge(v, T, INF);
    }
  }
  for (int i = 0; i < (M); ++i) {
    int u, v, d;
    scanf("%d%d%d", &u, &v, &d);
    --u, --v;
    d = -d;
    for (int j = -101; j <= 101; ++j) {
      if (j + d > 101 || j + d < -101) continue;
      net.addEdge(205 * u + 103 + j, 205 * v + 103 + j + d, INF);
    }
  }
  printf("%d\n", (int)(N * (long long)BIG - net.computeFlow(S, T)));
}