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;
int getint() {
  unsigned int c;
  int x = 0;
  while (((c = getchar()) - '0') >= 10) {
    if (c == '-') return -getint();
    if (!~c) exit(0);
  }
  do {
    x = (x << 3) + (x << 1) + (c - '0');
  } while (((c = getchar()) - '0') < 10);
  return x;
}
int getstr(char str[]) {
  int c, n = 0;
  while ((c = getchar()) <= ' ') {
    if (!~c) exit(0);
  }
  do {
    str[n++] = c;
  } while ((c = getchar()) > ' ');
  str[n] = 0;
  return n;
}
template <class T>
inline bool chmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
inline bool chmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename Ft>
struct MF {
  static const int V = 202 * 51 + 2;
  static const Ft feps = (Ft)1e-10, finf = (Ft)(1 << 28);
  Ft tof;
  int nv, dst[V], srt[V], que[V], qs, qe, src, snk;
  struct edge {
    int to, rv;
    Ft cap;
    edge(int to, int rv, Ft cap) : to(to), rv(rv), cap(cap) {}
    string str() {
      stringstream ss;
      ss << rv << ' ' << cap;
      return ss.str();
    }
  };
  vector<edge> graph[V];
  void init(int v = V) {
    nv = v, tof = 0;
    for (v = 0; v < nv; v++) graph[v].clear();
  }
  void add_edge(int fr, int to, Ft fcap, Ft rcap = 0) {
    if (fr == to) return;
    int fe = graph[fr].size(), te = graph[to].size();
    graph[fr].push_back(edge(to, te, fcap));
    graph[to].push_back(edge(fr, fe, rcap));
  }
  Ft aug(int u, Ft flo) {
    if (u == snk) return flo;
    Ft df;
    for (int &i = srt[u]; i < graph[u].size(); i++) {
      edge &e = graph[u][i];
      if (e.cap > feps and dst[u] < dst[e.to]) {
        if (flo > e.cap) flo = e.cap;
        if ((df = aug(e.to, flo)) > feps) {
          e.cap -= df, graph[e.to][e.rv].cap += df;
          return df;
        }
      }
    }
    return 0;
  }
  Ft dinic(int source, int sink, Ft flo_lim = finf) {
    int i, v;
    src = source, snk = sink;
    Ft df;
    for (; tof + feps < flo_lim;) {
      memset(dst, ~0, sizeof(dst));
      for (dst[src] = 0, qs = qe = 0, que[qe++] = src; qs != qe;) {
        v = que[qs++];
        for (i = 0; i < graph[v].size(); i++) {
          const edge &e = graph[v][i];
          if (e.cap > feps and !~dst[e.to]) {
            dst[e.to] = dst[v] + 1, que[qe++] = e.to;
            if (v == snk) {
              qs = qe = 0;
              break;
            }
          }
        }
      }
      if (!~dst[snk]) return tof;
      memset(srt, 0, sizeof(srt));
      for (; (df = aug(src, flo_lim - tof)) > feps;) tof += df;
    }
    return tof;
  }
  void cap_inc(int fr, int to, Ft inc) {
    int i;
    for (i = 0; i < graph[fr].size(); i++)
      if (graph[fr][i].to == to) {
        graph[fr][i].cap += inc;
        return;
      }
    add_edge(fr, to, inc);
  }
  Ft cap_dec(int fr, int to, Ft dec) {
    int i;
    Ft df, flo, res;
    for (i = 0; i < graph[fr].size(); i++) {
      if (graph[fr][i].to == to and graph[fr][i].cap > feps) {
        df = min(graph[fr][i].cap, dec);
        dec -= df, graph[fr][i].cap -= df;
        if (dec <= feps) return 0;
      }
    }
    flo = dinic(fr, to, dec);
    res = 0;
    if (dec - flo > feps)
      res = dec - flo, dinic(fr, src, res), dinic(snk, to, res);
    for (i = 0; i < graph[to].size(); i++) {
      if (graph[to][i].to == fr and graph[to][i].cap > feps) {
        df = min(graph[to][i].cap, dec);
        dec -= df, graph[to][i].cap -= df;
        if (dec <= feps) break;
      }
    }
    return res;
  }
};
int n, m;
int coef[55 * 3];
int lb[55], ub[55];
int us[111], vs[111], ds[111];
int nodesrt[55];
MF<int> mf;
int quad(int x, int i) {
  return coef[i * 3] * x * x + coef[i * 3 + 1] * x + coef[i * 3 + 2];
}
inline int node(int j, int i) { return nodesrt[i] + j; }
int main() {
  int i, j, tcc, tc = 1 << 28, level;
  for (tcc = 0; tcc < tc; tcc++) {
    n = getint(), m = getint();
    for (i = 0; i < n; i++) {
      coef[i * 3] = getint();
      coef[i * 3 + 1] = getint();
      coef[i * 3 + 2] = getint();
    }
    for (i = 0; i < n; i++) {
      lb[i] = getint();
      ub[i] = getint();
    }
    for (i = 0; i < m; i++) {
      us[i] = getint() - 1, vs[i] = getint() - 1, ds[i] = getint();
    }
    int s = 0, t = 1, v = 2, maxVal = 0;
    for (i = 0; i < n; i++) {
      nodesrt[i] = v;
      for (level = lb[i]; level <= ub[i]; level++) {
        chmax(maxVal, quad(level, i) + 1);
        v++;
      }
      v++;
    }
    mf.init(v);
    const int inf = 1 << 28;
    for (i = 0; i < n; i++) {
      mf.add_edge(s, node(0, i), inf);
      for (level = lb[i], j = 0; level <= ub[i]; level++, j++) {
        mf.add_edge(node(j, i), node(j + 1, i), maxVal - quad(level, i));
      }
      mf.add_edge(node(j, i), t, inf);
    }
    for (i = 0; i < m; i++) {
      int u = us[i], v = vs[i], d = ds[i];
      for (level = lb[u]; level <= ub[u]; level++) {
        int v_val = level - d;
        if (lb[v] <= v_val and v_val <= ub[v] + 1) {
          int uid = level - lb[u];
          int vid = v_val - lb[v];
          mf.add_edge(node(uid, u), node(vid, v), inf);
        }
      }
    }
    cout << maxVal * n - mf.dinic(s, t) << endl;
  }
  return 0;
}