#include <bits/stdc++.h>
using namespace std;
void getre() {
  int x = 0;
  printf("%d\n", 1 / x);
}
void gettle() {
  int res = 1;
  while (1) res <<= 1;
  printf("%d\n", res);
}
template <typename T, typename S>
inline bool upmin(T &a, const S &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T, typename S>
inline bool upmax(T &a, const S &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename N, typename PN>
inline N flo(N a, PN b) {
  return a >= 0 ? a / b : -((-a - 1) / b) - 1;
}
template <typename N, typename PN>
inline N cei(N a, PN b) {
  return a > 0 ? (a - 1) / b + 1 : -(-a / b);
}
template <typename N>
N gcd(N a, N b) {
  return b ? gcd(b, a % b) : a;
}
template <typename N>
inline int sgn(N a) {
  return a > 0 ? 1 : (a < 0 ? -1 : 0);
}
inline void gn(long long &x) {
  int sg = 1;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  c == '-' ? (sg = -1, x = 0) : (x = c - '0');
  while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0';
  x *= sg;
}
inline void gn(int &x) {
  long long t;
  gn(t);
  x = t;
}
inline void gn(unsigned long long &x) {
  long long t;
  gn(t);
  x = t;
}
inline void gn(double &x) {
  double t;
  scanf("%lf", &t);
  x = t;
}
inline void gn(long double &x) {
  double t;
  scanf("%lf", &t);
  x = t;
}
inline void gs(char *s) { scanf("%s", s); }
inline void gc(char &c) {
  while ((c = getchar()) > 126 || c < 33)
    ;
}
inline void pc(char c) { putchar(c); }
inline long long sqr(long long a) { return a * a; }
inline double sqrf(double a) { return a * a; }
const long long inf = 1e18;
const double pi = 3.14159265358979323846264338327950288L;
const double eps = 1e-6;
const int TREE_MAXV = 300000 + 5;
struct edge {
  int v, next;
} e[TREE_MAXV * 2];
int g[TREE_MAXV], etot;
int qu[TREE_MAXV], pre[TREE_MAXV];
int h[TREE_MAXV];
int n;
void ae(int u, int v) {
  e[etot].v = v;
  e[etot].next = g[u];
  g[u] = etot++;
}
void bfs(int rt) {
  int p = 0, q = 0;
  pre[rt] = 0;
  qu[q++] = rt;
  h[rt] = 0;
  while (p != q) {
    int u = qu[p++];
    for (int i = g[u]; ~i; i = e[i].next)
      if (e[i].v != pre[u]) {
        pre[e[i].v] = u;
        qu[q++] = e[i].v;
        h[e[i].v] = h[u] + 1;
      }
  }
}
void tree_init() {
  static bool ini = 0;
  if (!ini) {
    ini = 1;
    memset(g, -1, sizeof(g));
  } else {
    for (int i = 0; i <= n; i++) g[i] = -1;
  }
  etot = 0;
}
void readedge() {
  for (int i = 1; i < n; i++) {
    int x, y;
    gn(x);
    gn(y);
    ae(x, y);
    ae(y, x);
  }
}
int m;
struct pat {
  int son, pre, co;
  void read() {
    gn(son);
    gn(pre);
    gn(co);
  }
} pa[333333];
vector<int> lis[333333];
map<int, long long> f[333333];
long long del[333333];
multiset<long long> mi[333333];
void inse(int id, int h, long long nu) {
  if (!f[id].count(h)) {
    f[id][h] = nu;
    mi[id].insert(nu);
  } else if (nu < f[id][h]) {
    multiset<long long>::iterator it = mi[id].find(f[id][h]);
    mi[id].erase(it);
    f[id][h] = nu;
    mi[id].insert(nu);
  }
}
void dp(int u) {
  int masz = -1, mav;
  for (int i = g[u]; ~i; i = e[i].next)
    if (e[i].v != pre[u]) {
      int v = e[i].v;
      if (upmax(masz, ((int)(f[v]).size()))) {
        mav = v;
      }
    }
  long long totmi = 0;
  if (masz != -1) {
    for (int i = g[u]; ~i; i = e[i].next)
      if (e[i].v != pre[u]) {
        int v = e[i].v;
        if (mi[v].empty()) {
          printf("-1\n");
          exit(0);
        }
        totmi += *mi[v].begin() + del[v];
      }
    del[mav] += (totmi - (*mi[mav].begin() + del[mav]));
    for (int i = g[u]; ~i; i = e[i].next)
      if (e[i].v != pre[u] && e[i].v != mav) {
        int v = e[i].v;
        for (__typeof((f[v]).begin()) it = (f[v]).begin(); it != (f[v]).end();
             it++) {
          inse(mav, it->first,
               it->second + del[v] + (totmi - *mi[v].begin() - del[v]) -
                   del[mav]);
        }
      }
    f[u].swap(f[mav]);
    mi[u].swap(mi[mav]);
    swap(del[u], del[mav]);
  } else {
    inse(u, h[u], 0);
  }
  for (int j = (0), _ed = (((int)(lis[u]).size())); j < _ed; j++) {
    int p = pa[lis[u][j]].pre;
    int c = pa[lis[u][j]].co;
    if (p == u) continue;
    int hi = h[p];
    inse(u, hi, c + totmi - del[u]);
  }
  if (u == 1) return;
  if (f[u].count(h[u])) {
    multiset<long long>::iterator it = mi[u].find(f[u][h[u]]);
    mi[u].erase(it);
    f[u].erase(h[u]);
  }
}
int main() {
  gn(n);
  gn(m);
  tree_init();
  readedge();
  for (int _ = (1), _ed = (m + 1); _ < _ed; _++) {
    pa[_].read();
    lis[pa[_].son].push_back(_);
  }
  bfs(1);
  for (int i = n - 1; i >= 0; i--) dp(qu[i]);
  cout << del[1] + f[1][0] << endl;
  return 0;
}
