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;
}
struct GraphView {
  int direct, LR, n;
  string code;
  vector<pair<int, int> > edge;
  vector<string> node_str;
  vector<string> edge_str;
  GraphView() {}
  GraphView(int graph_node, int digraph = 1, int graph_direction = 1) {
    n = graph_node, direct = digraph, LR = graph_direction, node_str.resize(n);
    for (int i = 0; i < n; i++) {
      stringstream ss;
      ss << i;
      node_str[i] = ss.str();
    }
  }
  void init(int graph_node, int digraph = 1, int graph_direction = 1) {
    n = graph_node, direct = digraph, LR = graph_direction, node_str.resize(n);
    for (int i = 0; i < n; i++) {
      stringstream ss;
      ss << i;
      node_str[i] = ss.str();
    }
    edge.clear(), edge_str.clear();
  }
  void node_name(int i, string name) { node_str[i] = name; }
  void add_edge(int fr, int to, string label) {
    edge.push_back(make_pair(fr, to));
    edge_str.push_back(label);
  }
  string int_to_str(int d) {
    string res = "";
    do {
      res += (char)(d % 26 + 'A');
      d = d / 26;
    } while (d);
    return res;
  }
  string edge_style() {
    if (direct) return "->";
    return "--";
  }
  void make_dot() {
    code = "";
    if (direct)
      code = "digraph ";
    else
      code = "graph ";
    code += "{\n";
    if (LR) code += "    rankdir = LR\n";
    for (int i = 0; i < n; i++) {
      code += "    " + int_to_str(i) + " [label = \"" + node_str[i] + "\"];\n";
    }
    for (int i = 0; i < edge.size(); i++) {
      int u = edge[i].first, v = edge[i].second;
      code += "    " + int_to_str(u) + " " + edge_style() + " " +
              int_to_str(v) + " [label = \"" + edge_str[i] + "\"];\n";
    }
    code += "}\n";
  }
  void view(string file_name) {
    FILE *fp;
    string dotfile = file_name + ".dot";
    string giffile = file_name + ".gif";
    if ((fp = fopen(dotfile.c_str(), "w")) == NULL) return;
    fprintf(fp, "%s", code.c_str());
    fclose(fp);
    string command = "dot -Tgif " + dotfile + " -o " + giffile;
    system(command.c_str());
  }
};
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 view() {
    GraphView gv;
    gv.init(nv);
    for (int i = 0; i < nv; i++) {
      for (int j = 0; j < graph[i].size(); j++) {
        gv.add_edge(i, graph[i][j].to, graph[i][j].str());
      }
    }
    gv.make_dot();
    gv.view("flow");
  }
  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;
}