#include <bits/stdc++.h>
using namespace std;
const long long maxn = 3e5 + 10;
const long long INF = 1e18;
long long n, m, uu[maxn], vv[maxn], cc[maxn], indl[maxn], indr[maxn], ind[maxn],
    minn[4 * maxn], dp[4 * maxn], la[4 * maxn];
vector<int> adj[maxn], ord, sar[maxn], tah[maxn];
void dfs(long long v, long long p) {
  indl[v] = ord.size();
  for (auto u : adj[v])
    if (u != p) dfs(u, v);
  for (auto x : tah[v]) {
    ind[x] = ord.size();
    ord.push_back(x);
  }
  indr[v] = ord.size() - 1;
}
void build(long long l = 0ll, long long r = m - 1, long long node = 1ll) {
  if (l == r) {
    minn[node] = INF;
    return;
  }
  long long mid = (r + l) / 2;
  build(l, mid, 2 * node);
  build(mid + 1, r, (2 * node) + 1);
  minn[node] = INF;
}
void shift(long long l, long long r, long long node) {
  if (minn[node] != INF) minn[node] += la[node];
  if (l == r) {
    la[node] = 0;
    return;
  }
  la[2 * node] += la[node];
  la[(2 * node) + 1] += la[node];
  la[node] = 0;
}
void upd(long long x, long long y, long long va, long long l = 0ll,
         long long r = m - 1, long long node = 1ll) {
  shift(l, r, node);
  if (l > y || r < x) return;
  if (l >= x && r <= y) {
    minn[node] = va;
    return;
  }
  long long mid = (r + l) / 2;
  upd(x, y, va, l, mid, 2 * node);
  upd(x, y, va, mid + 1, r, (2 * node) + 1);
  minn[node] = min(minn[2 * node], minn[(2 * node) + 1]);
}
void add(long long x, long long y, long long va, long long l = 0ll,
         long long r = m - 1, long long node = 1ll) {
  shift(l, r, node);
  if (l > y || r < x) return;
  if (l >= x && r <= y) {
    la[node] += va;
    shift(l, r, node);
    return;
  }
  long long mid = (r + l) / 2;
  add(x, y, va, l, mid, 2 * node);
  add(x, y, va, mid + 1, r, (2 * node) + 1);
  minn[node] = min(minn[2 * node], minn[(2 * node) + 1]);
}
long long getmin(long long x, long long y, long long l = 0ll,
                 long long r = m - 1, long long node = 1ll) {
  shift(l, r, node);
  if (l > y || r < x) return INF;
  if (l >= x && r <= y) return minn[node];
  long long mid = (r + l) / 2;
  return min(getmin(x, y, l, mid, 2 * node),
             getmin(x, y, mid + 1, r, (2 * node) + 1));
}
void solve(long long v, long long p) {
  long long sum = 0;
  for (auto u : adj[v]) {
    if (u != p) {
      solve(u, v);
      sum += dp[u];
    }
  }
  for (auto x : tah[v]) upd(ind[x], ind[x], cc[x] + sum);
  for (auto x : sar[v]) upd(ind[x], ind[x], INF);
  for (auto u : adj[v])
    if (u != p) add(indl[u], indr[u], sum - dp[u]);
  dp[v] = getmin(indl[v], indr[v]);
  if (v == 1) return;
  if (dp[v] == INF) {
    cout << -1;
    exit(0);
  }
}
int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i < n; i++) {
    int v, u;
    cin >> v >> u;
    adj[v].push_back(u);
    adj[u].push_back(v);
  }
  for (int i = 1; i <= m; i++) {
    cin >> uu[i] >> vv[i] >> cc[i];
    sar[vv[i]].push_back(i);
    tah[uu[i]].push_back(i);
  }
  for (int i = 1; i <= 4 * m; i++) minn[i] = INF;
  dfs(1, 0);
  solve(1, 0);
  long long sum = 0;
  for (auto u : adj[1]) sum += dp[u];
  cout << sum;
  return 0;
}
