#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10, INF = 1e9 + 7;
int n, m, h[N];
long long MINCOST[N], extra[N];
vector<int> adj[N];
set<pair<long long, int> > paths[N];
bool vis[N];
int dfs(int v) {
  vis[v] = true;
  int maxhv = h[v];
  for (auto u : adj[v])
    if (!vis[u]) {
      h[u] = h[v] + 1;
      int maxhu = dfs(u);
      maxhv = min(maxhu, maxhv);
      MINCOST[v] += MINCOST[u];
      if (maxhu > h[v]) {
        while (!paths[u].empty() && h[paths[u].begin()->second] > h[v])
          paths[u].erase(paths[u].begin());
        if (paths[u].empty()) {
          cout << -1 << '\n';
          exit(0);
        }
        MINCOST[v] += (paths[u].begin()->first - extra[u]);
        maxhv = min(maxhv, h[paths[u].begin()->second]);
        extra[u] = paths[u].begin()->first;
        paths[u].erase(paths[u].begin());
      }
      if (paths[u].size() > paths[v].size()) {
        paths[u].swap(paths[v]);
        swap(extra[u], extra[v]);
      }
      for (auto p : paths[u])
        paths[v].insert({p.first - extra[u] + extra[v], p.second});
      paths[u].clear();
    }
  return maxhv;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i < n; i++) {
    int v, u;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    paths[u].insert({c, v});
  }
  dfs(1);
  cout << MINCOST[1] << '\n';
  return 0;
}
