#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
int n, m;
int dep[maxn];
long long ans = 0;
vector<int> g[maxn];
vector<pair<int, int> > qry[maxn];
class Node {
 public:
  long long delta;
  set<pair<int, long long>, greater<pair<int, long long> > > s1;
  set<pair<long long, int> > s2;
  inline void insert(pair<int, long long> p) {
    s1.insert(make_pair(p.first, p.second - delta));
    s2.insert(make_pair(p.second - delta, p.first));
    return;
  }
  inline void merge(Node &oth) {
    if (s1.size() < oth.s1.size()) swap(*this, oth);
    for (set<pair<int, long long> >::iterator it = oth.s1.begin();
         it != oth.s1.end(); ++it)
      s1.insert(make_pair(it->first, it->second + oth.delta - delta));
    for (set<pair<long long, int> >::iterator it = oth.s2.begin();
         it != oth.s2.end(); ++it)
      s2.insert(make_pair(it->first + oth.delta - delta, it->second));
    return;
  }
  inline void modify(int d) {
    while (s1.size() && (s1.begin()->first) > d) {
      pair<int, long long> p = *s1.begin();
      s2.erase(make_pair(p.second, p.first));
      s1.erase(p);
    }
    if (!s2.size()) {
      printf("%d\n", -1);
      exit(0);
    }
    pair<long long, int> p = *s2.begin();
    ans += p.first + delta;
    delta -= p.first + delta;
    return;
  }
};
inline Node dfs(int u, int p) {
  if (~p) dep[u] = dep[p] + 1;
  Node ret;
  for (int i = (0); i < (g[u].size()); ++i) {
    int v = g[u][i];
    if (v == p) continue;
    Node res = dfs(v, u);
    res.modify(dep[u]);
    ret.merge(res);
  }
  for (int i = (0); i < (qry[u].size()); ++i) {
    int v = qry[u][i].first, w = qry[u][i].second;
    ret.insert(make_pair(dep[v], w));
  }
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (1); i < (n); ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = (0); i < (m); ++i) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    --u;
    --v;
    qry[u].push_back(make_pair(v, w));
  }
  dfs(0, -1);
  printf("%lld\n", ans);
  return 0;
}
