#include <bits/stdc++.h>
using namespace std;
template <typename T>
void _R(T &x) {
  cin >> x;
}
void _R(int &x) { scanf("%d", &x); }
void _R(long long &x) { scanf("%" PRId64, &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template <typename T, typename... U>
void R(T &head, U &...tail) {
  _R(head);
  R(tail...);
}
template <typename T>
void _W(const T &x) {
  cout << x;
}
void _W(const int &x) { printf("%d", x); }
template <typename T>
void _W(const vector<T> &x) {
  for (auto i = x.cbegin(); i != x.cend(); i++) {
    if (i != x.cbegin()) putchar(' ');
    _W(*i);
  }
}
void W() {}
template <typename T, typename... U>
void W(const T &head, const U &...tail) {
  _W(head);
  putchar(sizeof...(tail) ? ' ' : '\n');
  W(tail...);
}
const int N = 3e5 + 10;
struct F {
  int to, len;
};
int n, m;
vector<int> e[N];
vector<F> f[N];
int dep[N], sz[N];
void dfs(int p, int lv) {
  dep[p] = -lv;
  sz[p] = ((int)(f[p]).size());
  for (int i : e[p]) {
    e[i].erase(find(begin(e[i]), end(e[i]), p));
    dfs(i, lv + 1);
    sz[p] += sz[i];
  }
  if (!e[p].empty()) {
    int w = 0;
    for (int i = 0; i < int(((int)(e[p]).size())); i++)
      if (sz[e[p][i]] > sz[e[p][w]]) w = i;
    swap(e[p][0], e[p][w]);
  }
}
inline void ins(map<int, long long> &mp, int k, long long v) {
  {
    auto it = mp.lower_bound(k);
    if (it != mp.end() && it->second <= v) return;
  }
  while (1) {
    auto it = mp.upper_bound(k);
    if (it == mp.begin()) break;
    it--;
    if (it->second < v) break;
    mp.erase(it);
  }
  mp[k] = v;
}
struct DP {
  long long d;
  map<int, long long> mp;
  void ins(int k, long long v) { ::ins(mp, k, v - d); }
  long long min() { return mp.begin()->second + d; }
};
inline void merge(DP &a, DP &b) {
  for (auto &i : b.mp) a.ins(i.first, i.second + b.d);
}
DP dp[N];
int dpi[N];
bool go(int p) {
  for (int i : e[p])
    if (!go(i)) return 0;
  long long base = 0;
  for (int i : e[p]) {
    auto &j = dp[dpi[i]];
    base += j.min();
  }
  for (int i : e[p]) {
    auto &j = dp[dpi[i]];
    j.d += base - j.min();
    if (!dpi[p])
      dpi[p] = dpi[i];
    else
      merge(dp[dpi[p]], j);
  }
  if (!dpi[p]) dpi[p] = p;
  auto &me = dp[dpi[p]];
  for (auto &i : f[p]) me.ins(dep[i.to], base + i.len);
  while (!me.mp.empty() && me.mp.begin()->first <= (dep[p] - (p == 1)))
    me.mp.erase(me.mp.begin());
  ;
  return !me.mp.empty();
}
int main() {
  R(n, m);
  if (n == 1) {
    W(0);
    return 0;
  }
  for (int i = 0; i < int(n - 1); i++) {
    int a, b;
    R(a, b);
    e[a].push_back(b);
    e[b].push_back(a);
  }
  for (int i = 0; i < int(m); i++) {
    int a, b, c;
    R(a, b, c);
    f[a].push_back(F{b, c});
  }
  dfs(1, 1);
  if (!go(1))
    W(-1);
  else
    W(dp[dpi[1]].min());
  return 0;
}
