#include <bits/stdc++.h>
using namespace std;
const long long int INF = (long long int)1e9 + 10;
const long long int INFLL = (long long int)1e18 + 10;
const long double EPS = 1e-8;
const long long int MOD = 1e9 + 7;
template <class T>
T &chmin(T &a, const T &b) {
  return a = min(a, b);
}
template <class T>
T &chmax(T &a, const T &b) {
  return a = max(a, b);
}
template <class T>
inline T sq(T a) {
  return a * a;
}
long long int in() {
  long long int x;
  scanf("%lld", &x);
  return x;
}
int n, m;
vector<int> G[300010];
int s[300010];
int t[300010];
long long int c[300010];
int id[300010];
vector<int> from[300010];
vector<int> to[300010];
long long int dp[300010];
long long int ans;
int cnt;
int inid[300010];
int outid[300010];
long long int node[2000010];
long long int added[2000010];
int size;
void init(int n) {
  size = 1;
  while (size < n) {
    size *= 2;
  }
}
void add(int a, int b, long long int x, int k, int l, int r) {
  if (a <= l and r <= b) {
    added[k] += x;
    chmin(added[k], INFLL);
  } else if (b <= l or r <= a) {
  } else {
    add(a, b, x, k * 2 + 1, l, (l + r) / 2);
    add(a, b, x, k * 2 + 2, (l + r) / 2, r);
    node[k] = min(node[k * 2 + 1] + added[k * 2 + 1],
                  node[k * 2 + 2] + added[k * 2 + 2]);
    chmin(node[k], INFLL);
  }
}
void add(int a, int b, long long int x) { add(a, b, x, 0, 0, size); }
long long int query(int a, int b, int k, int l, int r) {
  if (a <= l and r <= b) {
    long long int res = node[k] + added[k];
    return min(res, INFLL);
  } else if (b <= l or r <= a) {
    return INFLL;
  } else {
    long long int chl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    long long int chr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    long long int res = min(chl, chr) + added[k];
    return min(res, INFLL);
  }
}
long long int query(int a, int b) { return query(a, b, 0, 0, size); }
void dfs(int x, int p) {
  inid[x] = cnt;
  long long int sum = 0;
  for (auto &w : G[x]) {
    if (w != p) {
      dfs(w, x);
      sum += dp[w];
    }
  }
  for (auto &w : G[x]) {
    if (w != p) {
      add(inid[w], outid[w], sum - dp[w]);
    }
  }
  for (auto &w : from[x]) {
    add(cnt, cnt + 1, sum + c[w]);
    id[w] = cnt;
    cnt++;
  }
  for (auto &w : to[x]) {
    add(id[w], id[w] + 1, INFLL);
  }
  outid[x] = cnt;
  dp[x] = query(inid[x], outid[x]);
  if (dp[x] == INFLL) {
    throw -1;
  }
}
int main() {
  n = in();
  m = in();
  init(m);
  for (long long int i = (0); i < (long long int)(n - 1); i++) {
    int a = in() - 1;
    int b = in() - 1;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
  }
  for (long long int i = (0); i < (long long int)(m); i++) {
    s[i] = in() - 1;
    t[i] = in() - 1;
    c[i] = in();
    from[s[i]].emplace_back(i);
    to[t[i]].emplace_back(i);
  }
  for (auto &w : G[0]) {
    try {
      dfs(w, 0);
      ans += dp[w];
    } catch (int e) {
      puts("-1");
      return 0;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
