#include <bits/stdc++.h>
using namespace std;
const long long INF = 100000000000000ll;
const int N = 300010;
const int Nlg = N * 20;
struct EDGE {
  int u, v, nex;
  EDGE() {}
  EDGE(int _u, int _v, int _nex) {
    u = _u;
    v = _v;
    nex = _nex;
  }
} e[N * 2];
int len, fir[N];
int n, m, c[N], cal[N], rt[N];
long long f[N];
bool check;
vector<int> g[N];
struct TREE {
  int lc[Nlg], rc[Nlg], cnt;
  long long laz[Nlg], t[Nlg];
  void pdw(int u) {
    if (laz[u]) {
      if (lc[u]) t[lc[u]] += laz[u], laz[lc[u]] += laz[u];
      if (rc[u]) t[rc[u]] += laz[u], laz[rc[u]] += laz[u];
      laz[u] = 0;
    }
  }
  void pup(int u) {
    if (lc[u]) pdw(lc[u]);
    if (rc[u]) pdw(rc[u]);
    if (lc[u] && rc[u])
      t[u] = min(t[lc[u]], t[rc[u]]);
    else if (lc[u])
      t[u] = t[lc[u]];
    else if (rc[u])
      t[u] = t[rc[u]];
  }
  void modify(int &u, int l, int r, int p, long long c) {
    if (!u) u = ++cnt;
    pdw(u);
    if (l == r) {
      t[u] = c;
      return;
    }
    int mid = (l + r) / 2;
    if (p <= mid)
      modify(lc[u], l, mid, p, c);
    else
      modify(rc[u], mid + 1, r, p, c);
    pup(u);
  }
  void merge(int &u1, int u2) {
    if (!u1) {
      u1 = u2;
      return;
    }
    if (!u2) return;
    pdw(u1);
    pdw(u2);
    t[u1] = min(t[u1], t[u2]);
    merge(lc[u1], lc[u2]);
    merge(rc[u1], rc[u2]);
  }
} T;
void dfs1(int u, int fa) {
  for (int k = fir[u]; k != -1; k = e[k].nex) {
    int v = e[k].v;
    if (v == fa) continue;
    dfs1(v, u);
    cal[u] += cal[v];
  }
  int siz = g[u].size();
  for (int i = 0; i < siz; i++) cal[u] += (g[u][i] > 0 ? 1 : -1);
  if (u != 1 && !cal[u]) check = 0;
}
void dfs2(int u, int fa) {
  long long sum = 0;
  for (int k = fir[u]; k != -1; k = e[k].nex) {
    int v = e[k].v;
    if (v == fa) continue;
    dfs2(v, u);
    sum += f[v];
  }
  for (int k = fir[u]; k != -1; k = e[k].nex) {
    int v = e[k].v;
    if (v == fa) continue;
    T.laz[rt[v]] += sum - f[v];
    T.t[rt[v]] += sum - f[v];
    T.merge(rt[u], rt[v]);
  }
  int siz = g[u].size();
  for (int i = 0; i < siz; i++) {
    if (g[u][i] > 0)
      T.modify(rt[u], 1, m, g[u][i], c[g[u][i]] + sum);
    else
      T.modify(rt[u], 1, m, -g[u][i], INF);
  }
  f[u] = T.t[rt[u]];
  if (u == 1) f[u] = sum;
}
void addedge(int u, int v) {
  e[++len] = EDGE(u, v, fir[u]);
  fir[u] = len;
}
void read() {
  len = 0;
  memset(fir, -1, sizeof fir);
  scanf("%d%d", &n, &m);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    addedge(u, v);
    addedge(v, u);
  }
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d%d", &u, &v, &c[i]);
    g[u].push_back(i);
    g[v].push_back(-i);
  }
}
void solve() {
  check = 1;
  dfs1(1, 0);
  if (!check) {
    printf("-1\n");
    return;
  }
  T.cnt = 0;
  dfs2(1, 0);
  printf("%I64d\n", f[1]);
}
int main() {
  read();
  solve();
  return 0;
}
