#include <bits/stdc++.h>
using namespace std;
const int Maxn = 300050;
const long long inf = 1e15;
int nxt[Maxn << 1], head[Maxn], to[Maxn << 1], cnt;
void AddEdge(int u, int v) { nxt[++cnt] = head[u], head[u] = cnt, to[cnt] = v; }
int in[Maxn], out[Maxn], dfn;
class Farm {
 public:
  int id, c;
  Farm(int _id = 0, int _c = 0) { id = _id, c = _c; }
};
vector<Farm> add[Maxn], del[Maxn];
int pos[Maxn];
void dfs(int u, int v) {
  in[u] = dfn;
  for (int i = 0; i < add[u].size(); ++i) pos[add[u][i].id] = ++dfn;
  for (int i = head[u]; i; i = nxt[i])
    if (to[i] != v) dfs(to[i], u);
  out[u] = dfn;
}
class Node {
 public:
  int l, r;
  long long mn, lazy;
} v[Maxn << 3];
int here[Maxn];
void push_down(int node) {
  if (v[node].lazy) {
    if (v[node].lazy > inf) v[node].lazy = inf;
    v[node].mn += v[node].lazy;
    v[node << 1].lazy += v[node].lazy, v[(node << 1) | 1].lazy += v[node].lazy;
    v[node].lazy = 0;
  }
}
void push_up(int node) {
  if (node == 1) return;
  int fa = node >> 1;
  push_down(fa), push_down(node), push_down(node ^ 1);
  v[fa].mn = min(v[node].mn, v[node ^ 1].mn);
  push_up(fa);
}
void build(int l, int r, int node) {
  v[node].l = l, v[node].r = r, v[node].mn = inf;
  if (l == r) {
    v[node].mn = inf, here[l] = node;
    return;
  }
  int mid = (l + r) >> 1;
  build(l, mid, node << 1), build(mid + 1, r, (node << 1) | 1);
}
void Change(int l, int r, int node, long long val) {
  push_down(node);
  if (v[node].l >= l && v[node].r <= r) {
    v[node].lazy = val;
    push_down(node);
    return;
  }
  int mid = (v[node].l + v[node].r) >> 1;
  if (mid >= l) Change(l, r, node << 1, val);
  if (mid < r) Change(l, r, (node << 1) | 1, val);
  push_down(node << 1), push_down((node << 1) | 1);
  v[node].mn = min(v[node << 1].mn, v[(node << 1) | 1].mn);
}
long long dp[Maxn];
long long ask(int l, int r, int node) {
  push_down(node);
  if (v[node].l >= l && v[node].r <= r) return v[node].mn;
  int mid = (v[node].l + v[node].r) >> 1;
  long long tmp = inf;
  if (mid >= l) tmp = ask(l, r, node << 1);
  if (mid < r) tmp = min(tmp, ask(l, r, (node << 1) | 1));
  return tmp;
}
void work(int u, int fa) {
  long long all = 0;
  for (int i = head[u]; i; i = nxt[i])
    if (to[i] != fa) {
      work(to[i], u);
      all += dp[to[i]];
    }
  for (int i = 0; i < add[u].size(); ++i) {
    int p = pos[add[u][i].id];
    p = here[p];
    v[p].mn = (long long)all + add[u][i].c;
    push_up(p);
  }
  for (int i = 0; i < del[u].size(); ++i) {
    int p = pos[del[u][i].id];
    p = here[p];
    v[p].mn = inf;
    push_up(p);
  }
  for (int i = head[u]; i; i = nxt[i])
    if (to[i] != fa) {
      int e = to[i];
      if (in[e] == out[e]) continue;
      if (all <= inf)
        Change(in[e] + 1, out[e], 1, all - dp[e]);
      else
        Change(in[e] + 1, out[e], 1, inf);
    }
  if (in[u] == out[u])
    dp[u] = inf;
  else
    dp[u] = ask(in[u] + 1, out[u], 1);
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1, u, v; i < n; ++i) {
    scanf("%d%d", &u, &v);
    AddEdge(u, v), AddEdge(v, u);
  }
  for (int i = 1, u, v, c; i <= m; ++i) {
    scanf("%d%d%d", &u, &v, &c);
    if (u == v) continue;
    add[u].push_back(Farm(i, c));
    del[v].push_back(Farm(i, c));
  }
  dfs(1, 0);
  build(1, m, 1);
  work(1, 0);
  long long ans = 0;
  for (int i = head[1]; i; i = nxt[i]) {
    ans += dp[to[i]];
    if (ans > inf) ans = inf;
  }
  if (ans >= inf)
    puts("-1");
  else
    printf("%lld\n", ans);
  return 0;
}
