#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x;
  scanf("%I64d", &x);
  return x;
}
const long long N = 300010;
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,
               greater<pair<long long, long long> > >
    q[N];
long long head[N], nxt[N * 2], vet[N * 2], dep[N], tag[N], n, m, tot, ans;
void insert(long long x, long long y) {
  nxt[++tot] = head[x];
  head[x] = tot;
  vet[tot] = y;
}
void merge(int x, int y) {
  if (q[x].size() < q[y].size()) swap(q[x], q[y]), swap(tag[x], tag[y]);
  while (!q[y].empty())
    q[x].push(make_pair(q[y].top().first + tag[y] - tag[x], q[y].top().second)),
        q[y].pop();
}
void Dfs(long long x, long long pre) {
  for (long long i = head[x]; i; i = nxt[i])
    if (vet[i] != pre) Dfs(vet[i], x), merge(x, vet[i]);
  while (!q[x].empty() && dep[x] <= q[x].top().second) q[x].pop();
  if (x != 1) {
    if (q[x].empty()) {
      puts("-1");
      exit(0);
    }
    long long tmp = q[x].top().first + tag[x];
    ans += tmp;
    tag[x] -= tmp;
  }
}
void dfs(long long x, long long pre) {
  dep[x] = dep[pre] + 1;
  for (long long i = head[x]; i; i = nxt[i])
    if (vet[i] != pre) dfs(vet[i], x);
}
int main() {
  n = read();
  m = read();
  for (long long i = 2; i <= n; ++i) {
    long long x = read(), y = read();
    insert(x, y);
    insert(y, x);
  }
  dfs(1, 0);
  for (long long i = 1; i <= m; ++i) {
    long long x = read(), y = read(), w = read();
    q[x].push(make_pair(w, dep[y]));
  }
  Dfs(1, 0);
  printf("%I64d", ans);
}
