#include <bits/stdc++.h>
const int max_N = 302401;
int n, m, A[max_N], B[max_N], C[max_N], dep[max_N];
std::vector<int> T[max_N], Cd[max_N], Cu[max_N];
long long ans;
int read() {
  char c = getchar();
  int ans = 0;
  bool flag = true;
  while (!isdigit(c)) flag &= (c != '-'), c = getchar();
  while (isdigit(c)) ans = 10 * ans + c - '0', c = getchar();
  return flag ? ans : -ans;
}
void Write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x < 10)
    putchar(x + '0');
  else
    Write(x / 10), putchar(x % 10 + '0');
}
int min(int x, int y) { return x < y ? x : y; }
int max(int x, int y) { return x > y ? x : y; }
void _min(int &x, int y) {
  if (x > y) x = y;
}
void _max(int &x, int y) {
  if (x < y) x = y;
}
namespace pairing_heap {
int son[max_N][2], root[max_N], val[max_N], tag[max_N];
void change(int rt, int x) {
  if (rt) tag[rt] += x, val[rt] += x;
}
void pushdown(int rt) {
  if (tag[rt])
    change(son[rt][0], tag[rt]), change(son[rt][1], tag[rt]), tag[rt] = 0;
}
int merge(int x, int y) {
  assert(!son[x][1] && !son[y][1]);
  if (!x || !y) return x | y;
  pushdown(x), pushdown(y);
  if (val[x] > val[y]) std::swap(x, y);
  return son[y][1] = son[x][0], son[x][0] = y, x;
}
void link(int x, int y) { root[y] = merge(x, root[y]); }
int del(int rt) {
  pushdown(rt);
  int x = son[rt][0], y = son[x][1];
  if (!x) return 0;
  if (!y) return x;
  pushdown(x), pushdown(y);
  son[rt][0] = son[y][1], son[x][1] = son[y][1] = 0;
  return merge(merge(x, y), del(rt));
}
void pop(int x) { root[x] = del(root[x]); }
}  // namespace pairing_heap
void dfs(int x, int fa) {
  using namespace pairing_heap;
  dep[x] = dep[fa] + 1;
  for (auto i : T[x])
    if (i != fa) dfs(i, x), link(pairing_heap::root[i], x);
  while (root[x] && dep[B[root[x]]] >= dep[x]) pop(x);
  for (auto i : Cd[x]) link(i, x);
  if (!root[x]) puts("-1"), exit(0);
  ans += val[root[x]], change(root[x], -val[root[x]]);
}
int main() {
  n = read(), m = read();
  for (int i = 1, u, v; i < n; i++)
    u = read(), v = read(), T[u].push_back(v), T[v].push_back(u);
  for (int i = 1; i <= m; i++) {
    A[i] = read(), B[i] = read(), C[i] = read();
    if (A[i] != B[i])
      Cd[A[i]].push_back(i), Cu[B[i]].push_back(i), pairing_heap::val[i] = C[i];
  }
  for (auto i : T[1]) dfs(i, 1);
  return Write(ans), 0;
}
