#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
vector<int> e[N];
int dep[N];
set<pair<int, int> > S[N];
int d[N];
bool flag;
long long ans;
void dfs1(int v, int p) {
  for (auto u : e[v]) {
    if (u == p) continue;
    dep[u] = dep[v] + 1;
    dfs1(u, v);
  }
}
inline void upd(int &x, int y) {
  if (y < x) x = y;
}
void dfs(int v, int p) {
  for (auto u : e[v]) {
    if (u == p) continue;
    dfs(u, v);
    if (flag) return;
    if (S[v].size() < S[u].size()) swap(S[v], S[u]), swap(d[v], d[u]);
    for (auto &it : S[u])
      S[v].insert(make_pair(it.first - d[u] + d[v], it.second));
  }
  if (v == 1) return;
  while (!S[v].empty() && S[v].begin()->second >= dep[v])
    S[v].erase(S[v].begin());
  if (S[v].empty()) {
    flag = true;
    return;
  }
  auto it = S[v].begin();
  ans += it->first - d[v];
  d[v] = it->first;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    e[x].push_back(y);
    e[y].push_back(x);
  }
  dfs1(1, 0);
  for (int i = 1; i <= m; i++) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    S[u].insert(make_pair(c, dep[v]));
  }
  dfs(1, 0);
  if (flag)
    puts("-1");
  else
    printf("%I64d\n", ans);
  return 0;
}
