#include <bits/stdc++.h>
using namespace std;
long long n, m, timer, size[630000];
long long rec[630000], dfn[630000];
long long mn[630000 << 1], mark[630000 << 1], f[630000];
long long read() {
  long long x = 0, k = 1;
  char c;
  c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') k = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * k;
}
struct edge {
  long long cnp, to[630000], last[630000], head[630000];
  long long co[630000];
  edge() { cnp = 2; }
  void add(long long u, long long v, long long w = 0) {
    to[cnp] = v, co[cnp] = w, last[cnp] = head[u], head[u] = cnp++;
  }
} E1, G1, G2;
void Push_Down(long long p) {
  mark[p << 1] += mark[p], mark[p << 1 | 1] += mark[p];
  mn[p << 1] += mark[p], mn[p << 1 | 1] += mark[p];
  mark[p] = 0;
}
void Build(long long p, long long l, long long r) {
  if (l == r) {
    mn[p] = 0x3fffffffffffffff;
    return;
  }
  long long mid = (l + r) >> 1;
  Build(p << 1, l, mid), Build(p << 1 | 1, mid + 1, r);
  mn[p] = min(mn[p << 1], mn[p << 1 | 1]);
}
void Update(long long p, long long l, long long r, long long L, long long R,
            long long x) {
  if (L <= l && R >= r) {
    mark[p] += x;
    mn[p] += x;
    return;
  }
  if (L > r || R < l) return;
  long long mid = (l + r) >> 1;
  Push_Down(p);
  Update(p << 1, l, mid, L, R, x), Update(p << 1 | 1, mid + 1, r, L, R, x);
  mn[p] = min(mn[p << 1], mn[p << 1 | 1]);
}
void Modify(long long p, long long l, long long r, long long x, long long y) {
  if (l == r) {
    mn[p] = y;
    return;
  }
  long long mid = (l + r) >> 1;
  Push_Down(p);
  if (x <= mid)
    Modify(p << 1, l, mid, x, y);
  else
    Modify(p << 1 | 1, mid + 1, r, x, y);
  mn[p] = min(mn[p << 1], mn[p << 1 | 1]);
}
long long Query(long long p, long long l, long long r, long long L,
                long long R) {
  if (L <= l && R >= r) return mn[p];
  if (L > r || R < l) return 0x3fffffffffffffff;
  long long mid = (l + r) >> 1;
  return min(Query(p << 1, l, mid, L, R), Query(p << 1 | 1, mid + 1, r, L, R));
}
void pre(long long u, long long fa) {
  rec[u] = 1000000000;
  for (long long i = E1.head[u]; i; i = E1.last[i]) {
    long long v = E1.to[i];
    if (v == fa) continue;
    pre(v, u);
    size[u] += size[v];
    rec[u] = min(rec[u], rec[v]);
  }
  for (long long i = G1.head[u]; i; i = G1.last[i])
    dfn[G1.to[i]] = ++timer, rec[u] = min(rec[u], timer), size[u]++;
}
void dfs(long long u, long long fa) {
  long long sum = 0, flag = 0;
  for (long long i = E1.head[u]; i; i = E1.last[i]) {
    long long v = E1.to[i];
    if (v == fa) continue;
    dfs(v, u);
    sum += f[v];
  }
  if (u == 1) {
    f[1] = sum;
    return;
  }
  for (long long i = G1.head[u]; i; i = G1.last[i])
    Modify(1, 1, m, dfn[G1.to[i]], sum + G1.co[i]);
  for (long long i = G2.head[u]; i; i = G2.last[i])
    Modify(1, 1, m, dfn[G2.to[i]], 0x3fffffffffffffff);
  for (long long i = E1.head[u]; i; i = E1.last[i]) {
    long long v = E1.to[i];
    if (v == fa) continue;
    if (rec[v] < 1000000000)
      Update(1, 1, m, rec[v], rec[v] + size[v] - 1, sum - f[v]);
  }
  f[u] = rec[u] < 1000000000 ? Query(1, 1, m, rec[u], rec[u] + size[u] - 1)
                             : 0x3fffffffffffffff;
  if (f[u] >= 0x3fffffffffffffff) {
    printf("-1");
    exit(0);
  }
}
signed main() {
  n = read(), m = read();
  Build(1, 1, n);
  for (long long i = 1; i < n; i++) {
    long long u = read(), v = read();
    E1.add(u, v), E1.add(v, u);
  }
  for (long long i = 1; i <= m; i++) {
    long long u = read(), v = read(), w = read();
    G1.add(u, i, w), G2.add(v, i, w);
  }
  pre(1, 0);
  dfs(1, 0);
  if (f[1] < 0x3fffffffffffffff)
    printf("%I64d\n", f[1]);
  else
    printf("-1\n");
  return 0;
}
