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;
using vi = vector<long long>;
template <typename T>
std::istream& operator>>(std::istream& input, std::pair<T, T>& data) {
  input >> data.first >> data.second;
  return input;
}
template <typename T>
std::istream& operator>>(std::istream& input, std::vector<T>& data) {
  for (T& first : data) input >> first;
  return input;
}
template <typename T>
std::ostream& operator<<(std::ostream& output, const pair<T, T>& data) {
  output << "(" << data.first << "," << data.second << ")";
  return output;
}
template <typename T>
std::ostream& operator<<(std::ostream& output, const std::vector<T>& data) {
  for (const T& first : data) output << first << " ";
  return output;
}
std::ostream& operator<<(std::ostream& output, const __int128& first) {
  __int128 n = first;
  if (n == 0) {
    output << "0";
    return output;
  }
  if (n < 0) {
    n = -n;
    output << "-";
  }
  string s;
  while (n) {
    s += '0' + (n % 10);
    n /= 10;
  }
  reverse(s.begin(), s.end());
  cout << s;
  return output;
}
long long div_up(long long a, long long b) {
  return a / b + ((a ^ b) > 0 && a % b);
}
long long div_down(long long a, long long b) {
  return a / b - ((a ^ b) < 0 && a % b);
}
long long math_mod(long long a, long long b) { return a - b * div_down(a, b); }
template <class T>
using V = vector<T>;
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
long long gcd(long long a, long long b) {
  while (b) {
    tie(a, b) = make_pair(b, a % b);
  }
  return a;
}
long long Bit(long long mask, long long bit) { return (mask >> bit) & 1; }
const long long INF = 1e16 + 7, C = 1e9 + 7;
struct Dinic {
  struct edge {
    long long to, flow, cap;
  };
  const static long long N = 57 * 207 + 7;
  vector<edge> e;
  vector<long long> g[N + 7];
  long long dp[N + 7];
  long long ptr[N + 7];
  void clear() {
    for (long long i = 0; i < N + 7; i++) g[i].clear();
    e.clear();
  }
  void addEdge(long long a, long long b, long long cap) {
    g[a].push_back(e.size());
    e.push_back({b, 0, cap});
    g[b].push_back(e.size());
    e.push_back({a, 0, 0});
  }
  long long minFlow, start, finish;
  bool bfs() {
    for (long long i = 0; i < N; i++) dp[i] = -1;
    dp[start] = 0;
    vector<long long> st;
    long long uk = 0;
    st.push_back(start);
    while (uk < st.size()) {
      long long v = st[uk++];
      for (long long to : g[v]) {
        auto ed = e[to];
        if (ed.cap - ed.flow >= minFlow && dp[ed.to] == -1) {
          dp[ed.to] = dp[v] + 1;
          st.push_back(ed.to);
        }
      }
    }
    return dp[finish] != -1;
  }
  long long dfs(long long v, long long flow) {
    if (v == finish) return flow;
    for (; ptr[v] < g[v].size(); ptr[v]++) {
      long long to = g[v][ptr[v]];
      edge ed = e[to];
      if (ed.cap - ed.flow >= minFlow && dp[ed.to] == dp[v] + 1) {
        long long add = dfs(ed.to, min(flow, ed.cap - ed.flow));
        if (add) {
          e[to].flow += add;
          e[to ^ 1].flow -= add;
          return add;
        }
      }
    }
    return 0;
  }
  long long dinic(long long start, long long finish) {
    Dinic::start = start;
    Dinic::finish = finish;
    long long flow = 0;
    for (minFlow = (1ll << 60); minFlow; minFlow >>= 1) {
      while (bfs()) {
        for (long long i = 0; i < N; i++) ptr[i] = 0;
        while (long long now = dfs(start, INF)) flow += now;
      }
    }
    return flow;
  }
} dinic;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  V<vi> f(n, vi(3));
  cin >> f;
  vi l(n), r(n);
  vi start(n);
  const long long S = dinic.N - 2, T = dinic.N - 1;
  long long ptr = 0;
  for (long long i = (0); i < (n); ++i) {
    cin >> l[i] >> r[i];
    start[i] = ptr;
    ptr += r[i] - l[i] + 2;
    dinic.addEdge(S, start[i], INF);
    dinic.addEdge(start[i] + r[i] + 1 - l[i], T, INF);
    for (long long first = l[i]; first <= r[i]; ++first) {
      dinic.addEdge(start[i] + first - l[i], start[i] + first - l[i] + 1,
                    C - (f[i][0] * first * first + f[i][1] * first + f[i][2]));
    }
  }
  for (long long _ = (0); _ < (m); ++_) {
    long long u, v, d;
    cin >> u >> v >> d;
    u--;
    v--;
    for (long long first = l[u]; first <= r[u]; ++first) {
      if (first - d <= l[v]) {
      } else if (first - d <= r[v]) {
        dinic.addEdge(start[u] + first - l[u], start[v] + (first - d) - l[v],
                      INF);
      } else {
        dinic.addEdge(start[u] + first - l[u], T, INF);
      }
    }
  }
  cout << n * C - dinic.dinic(S, T) << '\n';
}