#include <bits/stdc++.h>
using namespace std;
const int N = 600005;
int rt[N], dep[N], tot;
long long f[N], ans;
struct node {
  int l, r, fa, d;
  long long z;
  pair<long long, int> x;
} t[N];
struct edge {
  int to, next;
} g[N << 1];
int head[N], cnt;
vector<pair<long long, int> > p[N];
void add(int u, int v) {
  g[++cnt] = (edge){v, head[u]};
  head[u] = cnt;
}
void add1(int x, long long k) {
  if (x) t[x].z += k, t[x].x.first += k;
}
void pushdown(int x) {
  if (t[x].z) add1(t[x].l, t[x].z), add1(t[x].r, t[x].z), t[x].z = 0;
}
int merge(int x, int y) {
  if (!x || !y) return x + y;
  if (t[x].x.first > t[y].x.first) swap(x, y);
  pushdown(x);
  t[t[x].r = merge(t[x].r, y)].fa = x;
  if (t[t[x].r].d > t[t[x].l].d) swap(t[x].l, t[x].r);
  t[x].d = t[t[x].r].d + 1;
  return x;
}
void dfs(int x, int fa) {
  dep[x] = dep[fa] + 1;
  for (int i = 0; i < p[x].size(); i++) {
    t[++tot].x = p[x][i];
    t[tot].d = 1;
    rt[x] = merge(rt[x], tot);
  }
  long long s = 0;
  for (int i = head[x]; i; i = g[i].next) {
    int y = g[i].to;
    if (y != fa) {
      dfs(y, x);
      s += f[y];
      add1(rt[y], -f[y]);
      rt[x] = merge(rt[x], rt[y]);
    }
  }
  add1(rt[x], s);
  while (rt[x] && dep[t[rt[x]].x.second] >= dep[x])
    pushdown(rt[x]), rt[x] = merge(t[rt[x]].l, t[rt[x]].r);
  if (!rt[x]) return (void)(printf("-1\n"), exit(0));
  f[x] = t[rt[x]].x.first;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  for (int i = 1; i <= m; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    p[x].push_back(make_pair(z, y));
  }
  dep[1] = 1;
  long long ans = 0;
  for (int i = head[1]; i; i = g[i].next) {
    int y = g[i].to;
    dfs(y, 1);
    ans += f[y];
  }
  printf("%lld\n", ans);
}
