#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 100, S = 4 * N;
const long long INF = 1e16;
long long n, m;
long long c[N], dp[N];
long long s[N], e[N], id[N], T;
vector<long long> add[N], rem[N], G[N];
void dfs(long long u, long long p) {
  s[u] = T;
  for (long long x : add[u]) id[x] = T++;
  for (long long v : G[u])
    if (v != p) dfs(v, u);
  e[u] = T;
}
bool act[N];
long long mn[S], ex[S];
void pull(long long x, long long xl, long long xr) {
  mn[x] = (xr - xl > 1 ? min(mn[2 * x], mn[2 * x + 1]) + ex[x]
                       : (act[xl] ? ex[x] : INF));
}
void setactive(long long q, bool qv, long long x = 1, long long xl = 0,
               long long xr = T) {
  if (xr - xl == 1) {
    act[xl] = qv;
    pull(x, xl, xr);
    return;
  }
  long long mid = (xl + xr) / 2, l = 2 * x, r = l + 1;
  if (q < mid)
    setactive(q, qv, l, xl, mid);
  else
    setactive(q, qv, r, mid, xr);
  pull(x, xl, xr);
}
void upd(long long ql, long long qr, long long q, long long x = 1,
         long long xl = 0, long long xr = T) {
  if (xr <= ql || xl >= qr) return;
  if (xl >= ql && xr <= qr) {
    ex[x] += q;
    pull(x, xl, xr);
    return;
  }
  long long mid = (xl + xr) / 2, l = 2 * x, r = l + 1;
  upd(ql, qr, q, l, xl, mid);
  upd(ql, qr, q, r, mid, xr);
  pull(x, xl, xr);
}
long long get(long long ql, long long qr, long long x = 1, long long xl = 0,
              long long xr = T) {
  if (ql >= xr || qr <= xl) return INF;
  if (xl >= ql && xr <= qr) return mn[x];
  long long mid = (xl + xr) / 2, l = 2 * x, r = l + 1;
  return min(get(ql, qr, l, xl, mid), get(ql, qr, r, mid, xr)) + ex[x];
}
void solve(long long u, long long p) {
  for (long long v : G[u])
    if (v != p) solve(v, u);
  for (long long x : add[u])
    setactive(id[x], true), upd(id[x], id[x] + 1, c[x]);
  for (long long x : rem[u]) setactive(id[x], false);
  if (p != -1) {
    for (long long v : G[u])
      if (v != p) {
        upd(s[u], e[u], dp[v]);
        upd(s[v], e[v], -dp[v]);
      }
    dp[u] = get(s[u], e[u]);
  } else
    for (long long v : G[u]) dp[u] += dp[v];
  if (dp[u] >= INF / 2) {
    cout << -1;
    exit(0);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  cin >> n >> m;
  for (long long i = 0; i < n - 1; i++) {
    long long u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  for (long long i = 0; i < m; i++) {
    long long u, v;
    cin >> u >> v >> c[i];
    u--;
    v--;
    add[u].push_back(i);
    rem[v].push_back(i);
  }
  dfs(0, -1);
  solve(0, -1);
  cout << dp[0] << endl;
}
