#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char c = getchar();
  long long x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
long long n, m, f[300005], dep[300005], rt[300005];
vector<long long> e[300005];
long long cnt, tag[300005], ls[300005], rs[300005], dis[300005], val[300005],
    id[300005];
vector<pair<long long, long long> > v[300005];
inline void pusht(long long p, long long v) { tag[p] += v, val[p] += v; }
inline void pushdown(long long p) {
  if (!tag[p]) return;
  if (ls[p]) pusht(ls[p], tag[p]);
  if (rs[p]) pusht(rs[p], tag[p]);
  tag[p] = 0;
}
long long merge(long long x, long long y) {
  if (!x || !y) return x | y;
  if (val[x] > val[y]) swap(x, y);
  pushdown(x), rs[x] = merge(rs[x], y);
  if (dis[rs[x]] > dis[ls[x]]) swap(ls[x], rs[x]);
  dis[x] = dis[ls[x]] + 1;
  return x;
}
void dfs(long long u, long long pa) {
  dep[u] = dep[pa] + 1;
  for (auto p : v[u]) {
    long long x = ++cnt;
    dis[x] = 1;
    val[x] = p.first, id[x] = p.second;
    rt[u] = merge(rt[u], x);
  }
  long long sum = 0;
  for (auto v : e[u]) {
    if (v == pa) continue;
    dfs(v, u);
    sum += f[v];
    pusht(rt[v], -f[v]), rt[u] = merge(rt[u], rt[v]);
  }
  pusht(rt[u], sum);
  while (rt[u] && dep[u] <= dep[id[rt[u]]])
    pushdown(rt[u]), rt[u] = merge(ls[rt[u]], rs[rt[u]]);
  if (!rt[u]) {
    puts("-1");
    exit(0);
  }
  f[u] = val[rt[u]];
}
signed main() {
  n = read(), m = read();
  for (register long long i = (2); i <= (n); ++i) {
    long long u = read(), v = read();
    e[u].push_back(v), e[v].push_back(u);
  }
  for (register long long i = (1); i <= (m); ++i) {
    long long x = read(), y = read(), k = read();
    v[x].push_back(make_pair(k, y));
  }
  long long res = 0;
  for (auto v : e[1]) dfs(v, 1), res += f[v];
  cout << res;
  return 0;
}
