#include <bits/stdc++.h>
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
using namespace std;
void update(set<pair<int, long long>> *st, long long base, int d,
            long long cost) {
  auto po = st->lower_bound({d + 1, -1ll});
  if (po != st->begin()) {
    po--;
    if (po->second + base <= cost) return;
  }
  po = st->lower_bound({d, -1ll});
  while (po != st->end() && po->second + base >= cost) po = st->erase(po);
  st->insert({d, cost - base});
}
int n, m;
vector<int> node[323456];
vector<pair<int, int>> worker[323456];
long long ans;
int depth[323456];
bool ok;
set<pair<int, long long>> *dfs(int u, int f, int dep,
                               set<pair<int, long long>> *st, long long &base) {
  depth[u] = dep;
  st->insert({depth[u], 0ll});
  base = 0;
  for (auto it : worker[u]) {
    assert(depth[it.first] != -1);
    update(st, base, depth[it.first], it.second);
  }
  for (int v : node[u])
    if (v != f) {
      set<pair<int, long long>> *tmp = new set<pair<int, long long>>;
      long long tb = 0;
      tmp = dfs(v, u, dep + 1, tmp, tb);
      long long t1 = tmp->size() == 0 ? tb : tmp->rbegin()->second + tb;
      long long t2 = st->size() == 0 ? base : st->rbegin()->second + base;
      base += t1;
      tb += t2;
      if (tmp->size() == 0) {
        ok = false;
      }
      if (!ok) return tmp;
      if (st->size() < tmp->size()) {
        swap(st, tmp);
        swap(base, tb);
      }
      for (auto it : *tmp) {
        update(st, base, it.first, it.second + tb);
      }
    }
  while (st->size() != 0 && st->rbegin()->first >= depth[u]) {
    if (u == 0) ans = st->rbegin()->second + base;
    st->erase(--st->end());
  }
  return st;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < int(n - 1); ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    node[u].push_back(v);
    node[v].push_back(u);
  }
  for (int i = 0; i < int(m); ++i) {
    int u, v, c;
    cin >> u >> v >> c;
    u--;
    v--;
    worker[u].push_back({v, c});
  }
  set<pair<int, long long>> *st = new set<pair<int, long long>>;
  long long base = 0;
  ok = true;
  dfs(0, -1, 0, st, base);
  if (!ok) ans = -1;
  cout << ans << endl;
  return 0;
}
