#include <bits/stdc++.h>
using std::pair;
using std::vector;
const int MAXN = 3e5 + 20;
namespace IO_base {
const int MAXB = 1 << 10;
char gbuf[MAXB], *ps = gbuf, *pt = gbuf;
inline char Getchar() {
  if (ps == pt) {
    ps = gbuf;
    pt = gbuf + fread(gbuf, 1, MAXB, stdin);
  }
  return (ps == pt) ? EOF : *ps++;
}
}  // namespace IO_base
namespace IO_number {
int read() {
  int x = 0;
  char c = IO_base::Getchar();
  bool f = 0;
  while (c < '0' || c > '9')
    (c == '-') ? f = 1, c = IO_base::Getchar() : c = IO_base::Getchar();
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (48 ^ c), c = IO_base::Getchar();
  return (f) ? -x : x;
}
}  // namespace IO_number
using namespace IO_number;
namespace LeftistTree {
inline void _swap(int &x, int &y) { x ^= y ^= x ^= y; }
struct node {
  int rank, ls, rs;
  int key, cid;
  int tag;
  node(const pair<int, int> &_data)
      : rank(0), ls(0), rs(0), key(_data.first), cid(_data.second), tag(0) {}
};
vector<node> data(1, node(pair<int, int>(0, 0)));
inline int _min(const int &x, const int &y) {
  return (data[x].key < data[y].key) ? x : y;
}
inline void pushdown(const int &_x) {
  node &x = data[_x];
  if (x.tag) {
    data[x.ls].key += x.tag, data[x.ls].tag += x.tag;
    data[x.rs].key += x.tag, data[x.rs].tag += x.tag;
    x.tag = 0;
  }
}
int _merge(int x, int y) {
  if (!x || !y)
    return x + y;
  else {
    if (_min(x, y) == y) _swap(x, y);
    pushdown(x);
    data[x].rs = _merge(data[x].rs, y);
    if (data[data[x].ls].rank < data[data[x].rs].rank)
      _swap(data[x].ls, data[x].rs);
    data[x].rank = data[data[x].rs].rank + 1;
    return x;
  }
}
inline int pop_min(const int &rt) {
  pushdown(rt);
  return _merge(data[rt].ls, data[rt].rs);
}
inline int merge(const int &x, const int &y) { return _merge(x, y); }
inline void add_all(const int &x, const int &val) {
  data[x].key += val, data[x].tag += val;
}
int new_LT(const vector<pair<int, int> > &arr) {
  int rt = 0;
  for (pair<int, int> i : arr) {
    data.push_back(node(i));
    rt = merge(rt, data.size() - 1);
  }
  return rt;
}
}  // namespace LeftistTree
using namespace LeftistTree;
namespace Graph {
struct edge {
  int to, nxt;
  edge(const int &_to, const int &_nxt) : to(_to), nxt(_nxt) {}
};
vector<int> first;
vector<edge> e(1, edge(0, 0));
inline void addedge(const int &u, const int &v) {
  e.push_back(edge(v, first[u])), first[u] = e.size() - 1;
  e.push_back(edge(u, first[v])), first[v] = e.size() - 1;
}
}  // namespace Graph
using namespace Graph;
vector<vector<pair<int, int> > > chain_add;
vector<vector<int> > chain_del;
vector<bool> chain_is_del;
long long Ans;
int dfs(const int &u, const int &fa) {
  int rt = new_LT(chain_add[u]);
  for (int l = first[u]; l; l = e[l].nxt)
    if (e[l].to != fa) rt = merge(rt, dfs(e[l].to, u));
  for (int i : chain_del[u]) chain_is_del[i] = 1;
  while (rt && chain_is_del[LeftistTree::data[rt].cid]) rt = pop_min(rt);
  if (rt) {
    Ans += LeftistTree::data[rt].key;
    add_all(rt, -LeftistTree::data[rt].key);
  } else if (u != 1)
    puts("-1"), exit(0);
  return rt;
}
int main() {
  const int n = read(), m = read();
  first.resize(n + 1, 0), e.reserve(2 * (n - 1) + 1);
  for (int i = 0; i < n - 1; ++i) addedge(read(), read());
  chain_add.resize(n + 1), chain_del.resize(n + 1);
  chain_is_del.resize(m, 0);
  for (int i = 0; i < m; ++i) {
    const int _u = read(), _v = read(), _c = read();
    chain_add[_u].push_back(pair<int, int>(_c, i));
    chain_del[_v].push_back(i);
  }
  dfs(1, 0);
  printf("%lld", Ans);
}
