#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> v[300010];
class sgt {
 public:
  long long mn[1200010], tag[1200010];
  void pushnow(int p, long long v) {
    mn[p] += v;
    tag[p] += v;
  }
  void pushdown(int p) {
    if (tag[p]) {
      pushnow((p << 1), tag[p]);
      pushnow(((p << 1) | 1), tag[p]);
      tag[p] = 0;
    }
  }
  void set(int p, int l, int r, int pos, long long v) {
    if (l == r) {
      mn[p] = v;
      return;
    }
    pushdown(p);
    int mid = (l + r) >> 1;
    if (pos <= mid)
      set((p << 1), l, mid, pos, v);
    else
      set(((p << 1) | 1), mid + 1, r, pos, v);
    mn[p] = min(mn[(p << 1)], mn[((p << 1) | 1)]);
  }
  void modify(int p, int l, int r, int ml, int mr, long long v) {
    if (ml <= l && r <= mr) {
      pushnow(p, v);
      return;
    }
    pushdown(p);
    int mid = (l + r) >> 1;
    if (ml <= mid) modify((p << 1), l, mid, ml, mr, v);
    if (mr > mid) modify(((p << 1) | 1), mid + 1, r, ml, mr, v);
    mn[p] = min(mn[(p << 1)], mn[((p << 1) | 1)]);
  }
  long long query(int p, int l, int r, int ql, int qr) {
    if (ql <= l && r <= qr) return mn[p];
    pushdown(p);
    int mid = (l + r) >> 1;
    long long ret = 0x3f3f3f3f3f3f3f3f;
    if (ql <= mid) ret = query((p << 1), l, mid, ql, qr);
    if (qr > mid) ret = min(ret, query(((p << 1) | 1), mid + 1, r, ql, qr));
    return ret;
  }
} T;
int s[300010], e[300010], c[300010], id[300010], tim = 0, L[300010], R[300010];
long long f[300010];
vector<int> st[300010], ed[300010];
void dfs(int np, int fath) {
  L[np] = tim + 1;
  for (int &x : st[np]) id[x] = ++tim;
  for (int &x : v[np]) {
    if (x == fath) continue;
    dfs(x, np);
  }
  R[np] = tim;
  if (L[np] > R[np]) {
    puts("-1");
    exit(0);
  }
}
void dfs2(int np, int fath) {
  long long sum = 0;
  for (int &x : v[np]) {
    if (x == fath) continue;
    dfs2(x, np);
    sum += f[x];
  }
  for (int &x : ed[np]) T.set(1, 1, m, id[x], 0x3f3f3f3f3f3f3f3f);
  for (int &x : st[np]) T.modify(1, 1, m, id[x], id[x], sum);
  for (int &x : v[np]) {
    if (x == fath) continue;
    T.modify(1, 1, m, L[x], R[x], sum - f[x]);
  }
  f[np] = T.query(1, 1, m, L[np], R[np]);
  if (np != 1 && f[np] >= 1e18) {
    puts("-1");
    exit(0);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, ti, tj; i < n; i++) {
    scanf("%d%d", &ti, &tj);
    v[ti].push_back(tj);
    v[tj].push_back(ti);
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &s[i], &e[i], &c[i]);
    st[s[i]].push_back(i);
    ed[e[i]].push_back(i);
  }
  dfs(1, 0);
  for (int i = 1; i <= m; i++) T.set(1, 1, m, id[i], c[i]);
  dfs2(1, 0);
  long long ans = 0;
  for (int &x : v[1]) ans += f[x];
  printf("%lld\n", ans);
  return 0;
}
