#include <bits/stdc++.h>
using namespace std;
const int N = 6e5 + 5;
const long long INF = 1e16;
int n, m, ts, st[N], en[N], Id[N], C[N];
long long dp[N], Lz[N * 4], Mn[N * 4];
vector<int> Adj[N], A[N], B[N];
void DFS(int v, int p) {
  st[v] = ts++;
  for (int &i : A[v]) Id[i] = ts++;
  for (int &u : Adj[v])
    if (u != p) DFS(u, v);
  en[v] = ts;
}
inline void Shift(int id) {
  Lz[(id * 2)] += Lz[id];
  Mn[(id * 2)] = min(Mn[(id * 2)] + Lz[id], INF);
  Lz[(id * 2 + 1)] += Lz[id];
  Mn[(id * 2 + 1)] = min(Mn[(id * 2 + 1)] + Lz[id], INF);
  Lz[id] = 0;
}
void Add(int le, int ri, long long val, int id = 1, int l = 0, int r = N) {
  if (ri <= l || r <= le) return;
  if (le <= l && r <= ri) {
    Lz[id] += val;
    Mn[id] = min(Mn[id] + val, INF);
    return;
  }
  Shift(id);
  Add(le, ri, val, (id * 2), l, ((l + r) >> 1));
  Add(le, ri, val, (id * 2 + 1), ((l + r) >> 1), r);
  Mn[id] = min(Mn[(id * 2)], Mn[(id * 2 + 1)]);
}
long long Get(int le, int ri, int id = 1, int l = 0, int r = N) {
  if (ri <= l || r <= le) return (INF);
  if (le <= l && r <= ri) return (Mn[id]);
  Shift(id);
  return (min(Get(le, ri, (id * 2), l, ((l + r) >> 1)),
              Get(le, ri, (id * 2 + 1), ((l + r) >> 1), r)));
}
void DFS2(int v, int p) {
  long long s = 0;
  for (int &u : Adj[v])
    if (u != p) DFS2(u, v), s += dp[u];
  for (int &u : Adj[v])
    if (u != p) Add(st[u], en[u], s - dp[u]);
  for (int &i : A[v]) Add(Id[i], Id[i] + 1, C[i] + s - INF);
  dp[v] = min(Get(st[v], en[v]), INF);
  for (int &i : B[p]) Add(Id[i], Id[i] + 1, INF - Get(Id[i], Id[i] + 1));
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, v, u; i < n; i++)
    scanf("%d%d", &v, &u), Adj[v].push_back(u), Adj[u].push_back(v);
  for (int i = 1, v, u; i <= m; i++) {
    scanf("%d%d%d", &u, &v, &C[i]);
    if (v == u)
      m--, i--;
    else
      A[u].push_back(i), B[v].push_back(i);
  }
  DFS(1, 0);
  fill(Mn, Mn + N * 4, INF);
  long long s = 0;
  for (int &u : Adj[1]) {
    DFS2(u, 1);
    if (dp[u] == INF) return puts("-1");
    s += dp[u];
  }
  return !printf("%lld", s);
}
