#include <bits/stdc++.h>
using namespace std;
int const N = 1e6 + 7;
long long const oo = 1e15 + 5;
long long dp[N];
int n, m, u, v, c[N], idx[N], ds[N], de[N], t;
vector<int> tree[N], wf[N], wt[N];
struct Node {
  long long min, lazy;
} seg[N << 2];
void dfs(int cur = 1, int par = 0) {
  ds[cur] = t + 1;
  for (int i = 0; i < wf[cur].size(); i++) idx[wf[cur][i]] = ++t;
  for (int i = 0, to; i < tree[cur].size(); i++)
    if ((to = tree[cur][i]) != par) dfs(to, cur);
  de[cur] = t;
}
inline void down(int rt) {
  if (seg[rt].lazy) {
    long long lazy = seg[rt].lazy;
    seg[rt << 1].min = min(seg[rt << 1].min + lazy, oo);
    seg[rt << 1 | 1].min = min(seg[rt << 1 | 1].min + lazy, oo);
    seg[rt << 1].lazy = min(seg[rt << 1].lazy + lazy, oo);
    seg[rt << 1 | 1].lazy = min(seg[rt << 1 | 1].lazy + lazy, oo);
    seg[rt].lazy = 0;
  }
}
inline void up(int rt) {
  seg[rt].min = min(oo, min(seg[rt << 1].min, seg[rt << 1 | 1].min));
}
void add(int L, int R, long long v, int l = 1, int r = m, int rt = 1) {
  if (L <= l && R >= r) {
    seg[rt].min = min(seg[rt].min + v, oo);
    seg[rt].lazy = min(seg[rt].lazy + v, oo);
    assert(seg[rt].min > 0 && seg[rt].lazy >= 0);
    return;
  }
  down(rt);
  int mid = (l + r) >> 1;
  if (L <= mid) add(L, R, v, l, mid, rt << 1);
  if (R > mid) add(L, R, v, mid + 1, r, rt << 1 | 1);
  up(rt);
}
void update(int p, long long v, int l = 1, int r = m, int rt = 1) {
  if (l == r) {
    seg[rt].min = v;
    seg[rt].lazy = 0;
    return;
  }
  down(rt);
  int mid = (l + r) >> 1;
  if (p <= mid)
    update(p, v, l, mid, rt << 1);
  else
    update(p, v, mid + 1, r, rt << 1 | 1);
  up(rt);
}
long long query(int L, int R, int l = 1, int r = m, int rt = 1) {
  if (L <= l && R >= r) {
    assert(seg[rt].min > 0);
    return seg[rt].min;
  }
  down(rt);
  int mid = (l + r) >> 1;
  long long lv = oo, rv = oo;
  if (L <= mid) lv = query(L, R, l, mid, rt << 1);
  if (R > mid) rv = query(L, R, mid + 1, r, rt << 1 | 1);
  return min(lv, rv);
}
long long solve(int cur = 1, int par = 1) {
  long long sum = 0;
  for (int i = 0, to; i < tree[cur].size(); i++)
    if ((to = tree[cur][i]) != par) {
      sum += solve(to, cur);
      sum = min(sum, oo);
    }
  if (cur == 1) return (dp[1] = sum) == oo ? -1 : sum;
  for (int i = 0, ci; i < wf[cur].size(); i++) {
    ci = wf[cur][i];
    update(idx[ci], min(sum + c[ci], oo));
  }
  for (int i = 0; i < wt[cur].size(); i++) update(idx[wt[cur][i]], oo);
  for (int i = 0, to; i < tree[cur].size(); i++)
    if ((to = tree[cur][i]) != par)
      add(ds[to], de[to], (sum == oo || dp[to] == oo) ? oo : (sum - dp[to]));
  dp[cur] = min(query(ds[cur], de[cur]), oo);
  assert(dp[cur] > 0);
  return dp[cur];
}
int main() {
  cin >> n >> m;
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> c[i];
    wf[u].push_back(i);
    wt[v].push_back(i);
  }
  int max_son = 0;
  for (int i = 1; i < n; i++) max_son = max(int(tree[i].size()), max_son);
  if (max_son - 1 > m) {
    cout << -1 << endl;
  } else {
    dfs();
    add(1, m, oo);
    cout << solve() << endl;
  }
  return 0;
}
