#include <bits/stdc++.h>
using namespace std;
int tot, head[300009], rt[300009], deep[300009], n, m, tott, bo;
long long val[300009];
inline int rd() {
  int x = 0;
  char c = getchar();
  bool f = 0;
  while (!isdigit(c)) {
    if (c == '-') f = 1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  return f ? -x : x;
}
struct edge {
  int n, to;
} e[300009 << 1];
inline void add(int u, int v) {
  e[++tot].n = head[u];
  e[tot].to = v;
  head[u] = tot;
}
struct node {
  int dep;
  long long val;
};
struct seg {
  int l, r, dep;
  node x;
  long long la;
} tr[300009];
inline void pushdown(int cnt) {
  if (tr[cnt].l) {
    tr[tr[cnt].l].x.val += tr[cnt].la;
    tr[tr[cnt].l].la += tr[cnt].la;
  }
  if (tr[cnt].r) {
    tr[tr[cnt].r].x.val += tr[cnt].la;
    tr[tr[cnt].r].la += tr[cnt].la;
  }
  tr[cnt].la = 0;
}
int merge(int u, int v) {
  if (!u || !v) return u ^ v;
  pushdown(u);
  pushdown(v);
  if (tr[u].x.val > tr[v].x.val) swap(u, v);
  tr[u].r = merge(tr[u].r, v);
  if (tr[tr[u].l].dep < tr[tr[u].r].dep) swap(tr[u].l, tr[u].r);
  tr[u].dep = tr[tr[u].r].dep + 1;
  return u;
}
inline void pop(int x) {
  pushdown(rt[x]);
  rt[x] = merge(tr[rt[x]].l, tr[rt[x]].r);
}
inline void upd(int x, long long val) {
  tr[x].x.val += val;
  tr[x].la += val;
}
void checker(int x) {
  pushdown(x);
  if (tr[x].l) checker(tr[x].l);
  if (tr[x].r) checker(tr[x].r);
  printf("%d %d miao  ", tr[x].x.val, tr[x].x.dep);
}
void dfs(int u, int fa) {
  long long sum = 0;
  deep[u] = deep[fa] + 1;
  bool tag = 0;
  for (int i = head[u]; i; i = e[i].n)
    if (e[i].to != fa) {
      int v = e[i].to;
      dfs(v, u);
      tag = 1;
      sum += val[v];
    }
  if (rt[u]) upd(rt[u], sum);
  if (u == 1) val[u] = sum;
  for (int i = head[u]; i; i = e[i].n)
    if (e[i].to != fa) {
      int v = e[i].to;
      upd(rt[v], sum - val[v]);
      rt[u] = merge(rt[u], rt[v]);
    }
  while (deep[tr[rt[u]].x.dep] >= deep[u]) pop(u);
  if (rt[u]) val[u] = min(val[u], tr[rt[u]].x.val);
  if (val[u] > 1e15) {
    bo = 1;
  }
}
int main() {
  n = rd();
  m = rd();
  int u, v, w;
  for (int i = 1; i <= n; ++i) val[i] = 1e16;
  for (int i = 1; i < n; ++i) {
    u = rd();
    v = rd();
    add(u, v);
    add(v, u);
  }
  for (int i = 1; i <= m; ++i) {
    u = rd();
    v = rd();
    w = rd();
    ++tott;
    tr[tott].x = node{v, w};
    rt[u] = merge(rt[u], tott);
  }
  dfs(1, 0);
  if (bo)
    puts("-1");
  else
    printf("%lld", val[1]);
  return 0;
}
