#include <bits/stdc++.h>
using namespace std;
long long int powm(long long int a, int b, int n) {
  long long int rm = 1;
  while (b) {
    if (b % 2) {
      rm = (rm * a) % n;
    }
    a = (a * a) % n;
    b /= 2;
  }
  return rm;
}
long long int inf = 1e15L;
set<pair<long long int, int> > myset[int(3e5 + 1000)];
vector<int> E[int(3e5 + 1000)], Up[int(3e5 + 1000)], Down[int(3e5 + 1000)];
int Uidx[int(3e5 + 1000)];
long long int Cost[int(3e5 + 1000)] = {0}, Extra[int(3e5 + 1000)] = {0};
int L[int(3e5 + 1000)], x[int(3e5 + 1000)], y[int(3e5 + 1000)],
    c[int(3e5 + 1000)], P[int(3e5 + 1000)], set_size[int(3e5 + 1000)] = {0};
bool deactivate[int(3e5 + 1000)] = {false};
void dfs(int node, int root, int level) {
  L[node] = level;
  int idx = -1;
  for (auto cld : E[node]) {
    idx++;
    if (cld != root) {
      dfs(cld, node, level + 1);
    } else
      Uidx[node] = idx;
  }
}
int merge(int u, int v) {
  int add_ext = Extra[v] - Extra[u];
  while (!myset[v].empty()) {
    pair<long long int, int> val = *myset[v].begin();
    myset[v].erase(val);
    set_size[v]--;
    if (deactivate[val.second]) continue;
    val.first += add_ext;
    myset[u].insert(val);
    set_size[u]++;
  }
}
void go(int node, int root) {
  int sz = E[node].size();
  vector<long long int> arr(sz + 1);
  long long int tot = 0;
  for (int i = 0; i < sz; i++) {
    int cld = E[node][i];
    if (cld == root) continue;
    go(cld, node);
    int k = P[cld];
    pair<long long int, int> val = make_pair(inf, -1);
    while (!myset[k].empty()) {
      val = *myset[k].begin();
      if (!deactivate[val.second]) break;
      myset[k].erase(val);
      set_size[k]--;
    }
    if (val.first < inf)
      arr[i] = min(val.first + Extra[k], inf);
    else
      arr[i] = inf;
    tot = min(tot + arr[i], inf);
  }
  Cost[node] = tot;
  int prev_set_sz = -1, mx_idx = -1;
  for (int i = 0; i < sz; ++i) {
    int cld = E[node][i];
    if (cld == root) continue;
    if (set_size[P[cld]] > prev_set_sz) {
      prev_set_sz = set_size[P[cld]];
      mx_idx = i;
    }
  }
  int kf = (mx_idx == -1 ? node : P[E[node][mx_idx]]);
  if (tot >= inf) return;
  for (int i = 0; i < sz; ++i) {
    int cld = E[node][i];
    if (cld == root) continue;
    Extra[P[cld]] = min(Extra[P[cld]] + tot - arr[i], inf);
  }
  for (int i = 0; i < sz; ++i) {
    int cld = E[node][i];
    if (cld == root || i == mx_idx) continue;
    merge(kf, P[cld]);
  }
  pair<long long int, int> val;
  for (auto id : Up[node]) {
    myset[kf].insert(make_pair(min(tot + c[id] - Extra[kf], inf), x[id]));
    set_size[kf]++;
  }
  P[node] = kf;
  deactivate[node] = true;
}
int main() {
  int n, m, u, v;
  scanf("%d", &n);
  ;
  scanf("%d", &m);
  ;
  for (int i = int(1); i <= (int)n - 1; i++) {
    cin >> u >> v;
    E[v].push_back(u);
    E[u].push_back(v);
  }
  dfs(1, 0, 1);
  for (int i = int(1); i <= (int)m; i++) {
    cin >> x[i] >> y[i] >> c[i];
    if (x[i] == y[i]) continue;
    if (L[x[i]] > L[y[i]]) swap(x[i], y[i]);
    Down[x[i]].push_back(i);
    Up[y[i]].push_back(i);
  }
  go(1, 0);
  if (Cost[1] >= inf) Cost[1] = -1;
  cout << Cost[1] << endl;
  return 0;
}
