#include <bits/stdc++.h>
using namespace std;
const int N = 300010;
set<pair<long long, long long> > st[N];
long long toadd[N], ans;
int n, m, h[N];
vector<int> adj[N];
bool ansb = true;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
void dfs(int c, int p) {
  h[c] = h[p] + 1;
  for (int ne : adj[c]) {
    if (ne == p) continue;
    dfs(ne, c);
  }
  if (c == 0) return;
  while (!st[c].empty() && h[st[c].begin()->second] >= h[c])
    st[c].erase(st[c].begin());
  if (st[c].empty()) ansb = false;
  int flag = st[c].begin()->first + toadd[c];
  ans += flag;
  if (st[c].size() <= st[p].size()) {
    auto hint = st[p].begin();
    for (auto it = st[c].begin(); it != st[c].end(); it++)
      hint = st[p].insert(
          hint, pair<long long, long long>(
                    it->first + toadd[c] - toadd[p] - flag, it->second));
  } else {
    auto hint = st[c].begin();
    for (auto it = st[p].begin(); it != st[p].end(); it++)
      hint = st[c].insert(
          hint, pair<long long, long long>(
                    it->first + toadd[p] - toadd[c] + flag, it->second));
    toadd[p] = -flag + toadd[c];
    st[p].swap(st[c]);
  }
  st[c].erase(st[c].begin(), st[c].end());
}
int main() {
  n = in(), m = in();
  for (int i = 1; i < n; i++) {
    int u = in() - 1, v = in() - 1;
    adj[u].push_back(v), adj[v].push_back(u);
  }
  for (int i = 0; i < m; i++) {
    int u = in() - 1, v = in() - 1, ci = in();
    st[u].insert(pair<long long, long long>(ci, v));
  }
  dfs(0, 0);
  if (!ansb) return cout << -1, 0;
  cout << ans;
}
