#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
#define MAXN 300005
typedef long long ll;
vector <int> v[MAXN];
int n, m, depth[MAXN];
ll res, D[MAXN];
struct PQ {
	int p; ll cost;
	bool operator < (const PQ a) const { return cost > a.cost; }
};
priority_queue <PQ> pq[MAXN];
void dfs(int x, int par) {
	for (auto &y : v[x]) {
		if (y == par) continue;
		depth[y] = depth[x] + 1;
		dfs(y, x);
	}
}
void go(int x, int par) {
	for (auto &y : v[x]) {
		if (y == par) continue;
		go(y, x);
		if (res == -1) return;
		if (pq[x].size() < pq[y].size()) {
			swap(pq[x], pq[y]);
			swap(D[x], D[y]);
		}
		while (!pq[y].empty()) {
			pq[x].push({ pq[y].top().p, pq[y].top().cost + D[y] - D[x] });
			pq[y].pop();
		}
	}
	if (x == 1) return;
	while (!pq[x].empty() && depth[pq[x].top().p] >= depth[x]) pq[x].pop();
	if (pq[x].size() == 0) res = -1;
	else {
		auto top = pq[x].top();
		res += top.cost + D[x];
		D[x] = -top.cost;
	}
}
int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i < n; i++) {
		int x, y; scanf("%d%d", &x, &y);
		v[x].push_back(y);
		v[y].push_back(x);
	}
	dfs(1, 0);
	for (int i = 1; i <= m; i++) {
		int x, y, w; scanf("%d%d%d", &x, &y, &w);
		pq[x].push({ y, w });
	}
	go(1, 0);
	printf("%I64d", res);
}


#include <stdio.h>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXN 300005
typedef long long ll;
int n, m, depth[MAXN];
vector<int> a[MAXN];
ll res, upd[MAXN];
priority_queue <pair<ll, int>> q[MAXN];
void dfs1(int u, int par) {
	depth[u] = depth[par] + 1;
	for (auto &v : a[u]) if (v != par) dfs1(v, u);
}
void dfs2(int u, int par) {
	for (auto &v : a[u]) {
		if (v != par) {
			dfs2(v, u);
			if (res == -1) return;
			if (q[u].size() < q[v].size()) {
				swap(q[u], q[v]);
				swap(upd[u], upd[v]);
			}
			while (!q[v].empty()) {
				q[u].push({ -(-q[v].top().first + upd[v] - upd[u]), q[v].top().second });
				q[v].pop();
			}
		}
	}
	while (!q[u].empty() && depth[u] <= q[u].top().second) q[u].pop();
	if (u == 1) return;
	if (q[u].empty()) res = -1;
	else {
		res += (-q[u].top().first) + upd[u];
		upd[u] -= (-q[u].top().first) + upd[u];
	}
}
int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1, u, v; i < n; i++) {
		scanf("%d%d", &u, &v);
		a[u].push_back(v);
		a[v].push_back(u);
	}
	dfs1(1, 0);
	for (int i = 1; i <= m; i++) {
		int u, v, c; scanf("%d%d%d", &u, &v, &c);
		q[u].push({ -c, depth[v] });
	}
	dfs2(1, 0);
	printf("%I64d", res);
}


#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
#define MAXN 300005
typedef long long ll;
vector <int> v[MAXN];
int n, m, depth[MAXN];
ll res, D[MAXN];
struct PQ {
	int p; ll cost;
	bool operator < (const PQ a) const { return cost > a.cost; }
};
priority_queue <PQ> pq[MAXN];
void dfs(int x, int par) {
	for (auto &y : v[x]) {
		if (y == par) continue;
		depth[y] = depth[x] + 1;
		dfs(y, x);
	}
}
void go(int x, int par) {
	for (auto &y : v[x]) {
		if (y == par) continue;
		go(y, x);
		if (res == -1) return;
		while (!pq[y].empty()) {
			pq[x].push({ pq[y].top().p, pq[y].top().cost + D[y] - D[x] });
			pq[y].pop();
		}
	}
	if (x == 1) return;
	while (!pq[x].empty() && depth[pq[x].top().p] >= depth[x]) pq[x].pop();
	if (pq[x].size() == 0) res = -1;
	else {
		auto top = pq[x].top();
		res += top.cost + D[x];
		D[x] = -top.cost;
	}
}
int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i < n; i++) {
		int x, y; scanf("%d%d", &x, &y);
		v[x].push_back(y);
		v[y].push_back(x);
	}
	dfs(1, 0);
	for (int i = 1; i <= m; i++) {
		int x, y, w; scanf("%d%d%d", &x, &y, &w);
		pq[x].push({ y, w });
	}
	go(1, 0);
	printf("%I64d", res);
}


#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(I,A)  for(__typeof(A.begin()) I = A.begin(); I != A.end(); ++I)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAIN(A,X)  (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
typedef signed long long slong;
typedef long double ldouble;
const slong Infinity = 1000000100;
const ldouble Epsilon = 1e-9;

template<typename T, typename U> ostream& operator << (ostream& os, const pair<T,U>&p) { return os << "(" << p.X << "," << p.Y << ")"; }
template<typename T> ostream& operator << (ostream &os, const vector<T>& V) { os << "["; FORW(i,0,SIZE(V)) os << V[i] << ((i==SIZE(V)-1) ? "" : ","); return os << "]"; }
template<typename T> ostream& operator << (ostream &os, const set<T>& S) {os << "("; for(T i: S) os << i << (i==*S.rbegin()?"":","); return os << ")"; }
template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& M){os << "{"; for(pair<T,U> i: M) os << i << (i.X==M.rbegin()->X?"":","); return os << "}"; }
template<typename T, typename F> T lbound(T p, T q, F f) { while(p < q) { T r = p+(q-p)/2; if(f(r)) q = r; else p = r+1; } return p; }
template<typename T, typename F> T lboundl(T p, T q, F f) { FOR(i,1,70) { T r = (p+q)/2; if(f(r)) q = r; else p = r; } return p; }

typedef pair<slong,slong> pll;
typedef pair<set<pll>, slong> depek;

const int N = 400000;
int lvl[N];

vector<int> v[N];
vector<pll> options[N];

void wrzuc(depek& dp, pll o, slong stala=0)
{
    o.X += stala - dp.Y;
    /*auto it = dp.X.lower_bound(o);
    if(it != dp.X.begin())
    {
        it--;
        if((*it).Y <= o.Y) return;
    }*/
    dp.X.insert(o);

    /*while(42)
    {
        it = dp.X.upper_bound(o);
        if(it == dp.X.end()) break;
        if((*it).Y >= o.Y)
        {
            dp.X.erase(it);
        }
        else break;
    }*/
}

void polacz(depek& cur, depek& nxt)
{
    for(pll o : nxt.X)
    {
        wrzuc(cur, o, nxt.Y);
    }
}

depek dfs(int x, int poziom)
{
    lvl[x] = poziom;
    depek cur, wlasne;
    cur.Y = 0; wlasne.Y = 0;
    cur.X.insert(MP(Infinity*N,-1));
    wlasne.X.insert(MP(Infinity*N,-1));
    for(pll o : options[x])
    {
        wrzuc(wlasne, MP(o.Y, lvl[o.X]));
    }

    //cout << x << " : " << wlasne.X << " + " << cur.Y << "\n";

    slong sumka = 0;
    for(int nxt : v[x])
    {
        if(lvl[nxt]) continue;
        depek dziecko = dfs(nxt, poziom+1);
        slong bleh = (dziecko.X.begin())->X + dziecko.Y;
        bleh = min(bleh, Infinity*N);
        //wlasne.Y += bleh;
        sumka += bleh;
        sumka = min(sumka, Infinity*N);
        dziecko.Y -= bleh;
        while(poziom!=1 && ((dziecko.X.begin())->Y >= poziom))
        {
            dziecko.X.erase(dziecko.X.begin());
        }
        //cout << "dziecko " << x << " " << dziecko.X << " + " << cur.Y << "\n";
        if(dziecko.X.size() > cur.X.size())
        {
            swap(dziecko, cur);
        }
        polacz(cur, dziecko);
    }
    wlasne.Y = sumka;
    cur.Y += sumka;

    if(wlasne.X.size() > cur.X.size())
    {
        swap(wlasne, cur);
    }
    polacz(cur, wlasne);

    while(poziom!=1 && ((cur.X.begin())->Y >= poziom))
    {
        cur.X.erase(cur.X.begin());
    }

    //cout << "wynik " << x << " " << cur.X << " + " << cur.Y << "\n";

    return cur;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, m;
    cin >> n >> m;

    slong sumka = 0;

    if(n == 1)
    {
        cout << "0\n";
        return 0;
    }

    FOR(i,1,n-1)
    {
        int a, b;
        cin >> a >> b;
        v[a].PB(b);
        v[b].PB(a);
    }

    FOR(i,1,m)
    {
        slong a, b, c;
        cin >> a >> b >> c;
        if(a == b) continue;
        sumka += c;
        options[a].PB(MP(b,c));
    }

    depek ans = dfs(1, 1);
    slong heh = (ans.X.begin())->X + ans.Y;
    if(heh >= sumka) cout << "-1\n";
    else cout << heh << "\n";
}


#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(I,A)  for(__typeof(A.begin()) I = A.begin(); I != A.end(); ++I)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAIN(A,X)  (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
typedef signed long long slong;
typedef long double ldouble;
const slong Infinity = 1000000100;
const ldouble Epsilon = 1e-9;

template<typename T, typename U> ostream& operator << (ostream& os, const pair<T,U>&p) { return os << "(" << p.X << "," << p.Y << ")"; }
template<typename T> ostream& operator << (ostream &os, const vector<T>& V) { os << "["; FORW(i,0,SIZE(V)) os << V[i] << ((i==SIZE(V)-1) ? "" : ","); return os << "]"; }
template<typename T> ostream& operator << (ostream &os, const set<T>& S) {os << "("; for(T i: S) os << i << (i==*S.rbegin()?"":","); return os << ")"; }
template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& M){os << "{"; for(pair<T,U> i: M) os << i << (i.X==M.rbegin()->X?"":","); return os << "}"; }
template<typename T, typename F> T lbound(T p, T q, F f) { while(p < q) { T r = p+(q-p)/2; if(f(r)) q = r; else p = r+1; } return p; }
template<typename T, typename F> T lboundl(T p, T q, F f) { FOR(i,1,70) { T r = (p+q)/2; if(f(r)) q = r; else p = r; } return p; }

typedef pair<slong,slong> pll;
typedef pair<set<pll>, slong> depek;

const int N = 400000;
int lvl[N];

vector<int> v[N];
vector<pll> options[N];

void wrzuc(depek& dp, pll o, slong stala=0)
{
    o.X += stala - dp.Y;
    auto it = dp.X.lower_bound(o);
    if(it != dp.X.begin())
    {
        it--;
        if((*it).Y <= o.Y) return;
    }
    dp.X.insert(o);

    while(42)
    {
        it = dp.X.upper_bound(o);
        if(it == dp.X.end()) break;
        if((*it).Y >= o.Y)
        {
            dp.X.erase(it);
        }
        else break;
    }
}

void polacz(depek& cur, depek& nxt)
{
    for(pll o : nxt.X)
    {
        wrzuc(cur, o, nxt.Y);
    }
}

depek dfs(int x, int poziom)
{
    lvl[x] = poziom;
    depek cur, wlasne;
    cur.Y = 0; wlasne.Y = 0;
    cur.X.insert(MP(Infinity*N,-1));
    wlasne.X.insert(MP(Infinity*N,-1));
    for(pll o : options[x])
    {
        wrzuc(wlasne, MP(o.Y, lvl[o.X]));
    }

    //cout << x << " : " << wlasne.X << " + " << cur.Y << "\n";

    slong sumka = 0;
    for(int nxt : v[x])
    {
        if(lvl[nxt]) continue;
        depek dziecko = dfs(nxt, poziom+1);
        slong bleh = (dziecko.X.begin())->X + dziecko.Y;
        bleh = min(bleh, Infinity*N);
        //wlasne.Y += bleh;
        sumka += bleh;
        sumka = min(sumka, Infinity*N);
        dziecko.Y -= bleh;
        while(poziom!=1 && ((dziecko.X.begin())->Y >= poziom))
        {
            dziecko.X.erase(dziecko.X.begin());
        }
        //cout << "dziecko " << x << " " << dziecko.X << " + " << cur.Y << "\n";
        if(dziecko.X.size() > cur.X.size())
        {
            swap(dziecko, cur);
        }
        polacz(cur, dziecko);
    }
    wlasne.Y = sumka;
    cur.Y += sumka;

    if(wlasne.X.size() > cur.X.size())
    {
        swap(wlasne, cur);
    }
    polacz(cur, wlasne);

    while(poziom!=1 && ((cur.X.begin())->Y >= poziom))
    {
        cur.X.erase(cur.X.begin());
    }

    //cout << "wynik " << x << " " << cur.X << " + " << cur.Y << "\n";

    return cur;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, m;
    cin >> n >> m;

    slong sumka = 0;

    if(n == 1)
    {
        cout << "0\n";
        return 0;
    }

    FOR(i,1,n-1)
    {
        int a, b;
        cin >> a >> b;
        v[a].PB(b);
        v[b].PB(a);
    }

    FOR(i,1,m)
    {
        slong a, b, c;
        cin >> a >> b >> c;
        if(a == b) continue;
        sumka += c;
        options[a].PB(MP(b,c));
    }

    depek ans = dfs(1, 1);
    slong heh = (ans.X.begin())->X + ans.Y;
    if(heh >= sumka) cout << "-1\n";
    else cout << heh << "\n";
}


#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(I,A)  for(__typeof(A.begin()) I = A.begin(); I != A.end(); ++I)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAIN(A,X)  (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
typedef signed long long slong;
typedef long double ldouble;
const slong Infinity = 1000000100;
const ldouble Epsilon = 1e-9;

template<typename T, typename U> ostream& operator << (ostream& os, const pair<T,U>&p) { return os << "(" << p.X << "," << p.Y << ")"; }
template<typename T> ostream& operator << (ostream &os, const vector<T>& V) { os << "["; FORW(i,0,SIZE(V)) os << V[i] << ((i==SIZE(V)-1) ? "" : ","); return os << "]"; }
template<typename T> ostream& operator << (ostream &os, const set<T>& S) {os << "("; for(T i: S) os << i << (i==*S.rbegin()?"":","); return os << ")"; }
template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& M){os << "{"; for(pair<T,U> i: M) os << i << (i.X==M.rbegin()->X?"":","); return os << "}"; }
template<typename T, typename F> T lbound(T p, T q, F f) { while(p < q) { T r = p+(q-p)/2; if(f(r)) q = r; else p = r+1; } return p; }
template<typename T, typename F> T lboundl(T p, T q, F f) { FOR(i,1,70) { T r = (p+q)/2; if(f(r)) q = r; else p = r; } return p; }

typedef pair<slong,slong> pll;
typedef pair<set<pll>, slong> depek;

const int N = 400000;
int lvl[N];

vector<int> v[N];
vector<pll> options[N];

void wrzuc(depek& dp, pll o, slong stala=0)
{
    o.X += stala - dp.Y;
    dp.X.insert(o);
}

void polacz(depek& cur, depek& nxt)
{
    for(pll o : nxt.X)
    {
        wrzuc(cur, o, nxt.Y);
    }
}

void zabij()
{
    cout << "-1\n";
    exit(0);
}

depek dfs(int x, int poziom)
{
    lvl[x] = poziom;
    depek cur, wlasne;
    cur.Y = 0; wlasne.Y = 0;

    for(pll o : options[x])
    {
        wrzuc(wlasne, MP(o.Y, lvl[o.X]));
    }

    //cout << x << " : " << wlasne.X << " + " << cur.Y << "\n";

    slong sumka = 0;
    for(int nxt : v[x])
    {
        if(lvl[nxt]) continue;
        depek dziecko = dfs(nxt, poziom+1);
        slong bleh = (dziecko.X.begin())->X + dziecko.Y;
        if(!SIZE(dziecko.X)) zabij();
        //wlasne.Y += bleh;
        sumka += bleh;
        dziecko.Y -= bleh;
        while(poziom!=1 && ((dziecko.X.begin())->Y >= poziom))
        {
            dziecko.X.erase(dziecko.X.begin());
        }
        //cout << "dziecko " << x << " " << dziecko.X << " + " << cur.Y << "\n";
        if(dziecko.X.size() > cur.X.size())
        {
            swap(dziecko, cur);
        }
        polacz(cur, dziecko);
    }
    wlasne.Y = sumka;
    cur.Y += sumka;

    if(wlasne.X.size() > cur.X.size())
    {
        swap(wlasne, cur);
    }
    polacz(cur, wlasne);

    while(poziom!=1 && ((cur.X.begin())->Y >= poziom))
    {
        cur.X.erase(cur.X.begin());
    }

    if(!SIZE(cur.X)) zabij();

    //cout << "wynik " << x << " " << cur.X << " + " << cur.Y << "\n";

    return cur;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, m;
    cin >> n >> m;

    slong sumka = 0;

    if(n == 1)
    {
        cout << "0\n";
        return 0;
    }

    FOR(i,1,n-1)
    {
        int a, b;
        cin >> a >> b;
        v[a].PB(b);
        v[b].PB(a);
    }

    FOR(i,1,m)
    {
        slong a, b, c;
        cin >> a >> b >> c;
        if(a == b) continue;
        sumka += c;
        options[a].PB(MP(b,c));
    }

    depek ans = dfs(1, 1);
    slong heh = (ans.X.begin())->X + ans.Y;
    if(heh >= sumka) cout << "-1\n";
    else cout << heh << "\n";
}


#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(I,A)  for(__typeof(A.begin()) I = A.begin(); I != A.end(); ++I)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAIN(A,X)  (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
typedef signed long long slong;
typedef long double ldouble;
const slong Infinity = 1000000100;
const ldouble Epsilon = 1e-9;

template<typename T, typename U> ostream& operator << (ostream& os, const pair<T,U>&p) { return os << "(" << p.X << "," << p.Y << ")"; }
template<typename T> ostream& operator << (ostream &os, const vector<T>& V) { os << "["; FORW(i,0,SIZE(V)) os << V[i] << ((i==SIZE(V)-1) ? "" : ","); return os << "]"; }
template<typename T> ostream& operator << (ostream &os, const set<T>& S) {os << "("; for(T i: S) os << i << (i==*S.rbegin()?"":","); return os << ")"; }
template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& M){os << "{"; for(pair<T,U> i: M) os << i << (i.X==M.rbegin()->X?"":","); return os << "}"; }
template<typename T, typename F> T lbound(T p, T q, F f) { while(p < q) { T r = p+(q-p)/2; if(f(r)) q = r; else p = r+1; } return p; }
template<typename T, typename F> T lboundl(T p, T q, F f) { FOR(i,1,70) { T r = (p+q)/2; if(f(r)) q = r; else p = r; } return p; }

typedef pair<slong,slong> pll;
typedef pair<set<pll>, slong> depek;

const int N = 400000;
int lvl[N];

vector<int> v[N];
vector<pll> options[N];

void wrzuc(depek& dp, pll o, slong stala=0)
{
    o.X += stala - dp.Y;
    auto it = dp.X.lower_bound(o);
    if(it != dp.X.begin())
    {
        it--;
        if((*it).Y <= o.Y) return;
    }
    dp.X.insert(o);

    while(42)
    {
        it = dp.X.upper_bound(o);
        if(it == dp.X.end()) break;
        if((*it).Y >= o.Y)
        {
            dp.X.erase(it);
        }
        else break;
    }
}

void polacz(depek& cur, depek& nxt)
{
    for(pll o : nxt.X)
    {
        wrzuc(cur, o, nxt.Y);
    }
}

depek dfs(int x, int poziom)
{
    lvl[x] = poziom;
    depek cur, wlasne;
    cur.Y = 0; wlasne.Y = 0;
    cur.X.insert(MP(Infinity*N,-1));
    wlasne.X.insert(MP(Infinity*N,-1));
    for(pll o : options[x])
    {
        wrzuc(wlasne, MP(o.Y, lvl[o.X]));
    }

    //cout << x << " : " << wlasne.X << " + " << cur.Y << "\n";

    slong sumka = 0;
    for(int nxt : v[x])
    {
        if(lvl[nxt]) continue;
        depek dziecko = dfs(nxt, poziom+1);
        slong bleh = (dziecko.X.begin())->X + dziecko.Y;
        bleh = min(bleh, Infinity*N);
        //wlasne.Y += bleh;
        sumka += bleh;
        sumka = min(sumka, Infinity*N);
        dziecko.Y -= bleh;
        while(poziom!=1 && ((dziecko.X.begin())->Y >= poziom))
        {
            dziecko.X.erase(dziecko.X.begin());
        }
        //cout << "dziecko " << x << " " << dziecko.X << " + " << cur.Y << "\n";
        if(dziecko.X.size() > cur.X.size())
        {
            swap(dziecko, cur);
        }
        polacz(cur, dziecko);
    }
    wlasne.Y = sumka;
    cur.Y += sumka;

    if(wlasne.X.size() > cur.X.size())
    {
        swap(wlasne, cur);
    }
    polacz(cur, wlasne);

    while(poziom!=1 && ((cur.X.begin())->Y >= poziom))
    {
        cur.X.erase(cur.X.begin());
    }

    //cout << "wynik " << x << " " << cur.X << " + " << cur.Y << "\n";

    return cur;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, m;
    cin >> n >> m;

    slong sumka = 0;

    FOR(i,1,n-1)
    {
        int a, b;
        cin >> a >> b;
        v[a].PB(b);
        v[b].PB(a);
    }

    FOR(i,1,m)
    {
        slong a, b, c;
        cin >> a >> b >> c;
        if(a == b) continue;
        sumka += c;
        options[a].PB(MP(b,c));
    }

    depek ans = dfs(1, 1);
    slong heh = (ans.X.begin())->X + ans.Y;
    if(heh >= sumka) cout << "-1\n";
    else cout << heh << "\n";
}


#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(I,A)  for(__typeof(A.begin()) I = A.begin(); I != A.end(); ++I)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAIN(A,X)  (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
typedef signed long long slong;
typedef long double ldouble;
const slong Infinity = 1000000100;
const ldouble Epsilon = 1e-9;

template<typename T, typename U> ostream& operator << (ostream& os, const pair<T,U>&p) { return os << "(" << p.X << "," << p.Y << ")"; }
template<typename T> ostream& operator << (ostream &os, const vector<T>& V) { os << "["; FORW(i,0,SIZE(V)) os << V[i] << ((i==SIZE(V)-1) ? "" : ","); return os << "]"; }
template<typename T> ostream& operator << (ostream &os, const set<T>& S) {os << "("; for(T i: S) os << i << (i==*S.rbegin()?"":","); return os << ")"; }
template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& M){os << "{"; for(pair<T,U> i: M) os << i << (i.X==M.rbegin()->X?"":","); return os << "}"; }
template<typename T, typename F> T lbound(T p, T q, F f) { while(p < q) { T r = p+(q-p)/2; if(f(r)) q = r; else p = r+1; } return p; }
template<typename T, typename F> T lboundl(T p, T q, F f) { FOR(i,1,70) { T r = (p+q)/2; if(f(r)) q = r; else p = r; } return p; }

typedef pair<slong,slong> pll;
typedef pair<set<pll>, slong> depek;

const int N = 400000;
int lvl[N];

vector<int> v[N];
vector<pll> options[N];

void wrzuc(depek& dp, pll o, slong stala=0)
{
    o.X += stala - dp.Y;
    auto it = dp.X.lower_bound(o);
    if(it != dp.X.begin())
    {
        it--;
        if((*it).Y <= o.Y) return;
    }
    dp.X.insert(o);

    while(42)
    {
        it = dp.X.upper_bound(o);
        if(it == dp.X.end()) break;
        if((*it).Y >= o.Y)
        {
            dp.X.erase(it);
        }
        else break;
    }
}

void polacz(depek& cur, depek& nxt)
{
    for(pll o : nxt.X)
    {
        wrzuc(cur, o, nxt.Y);
    }
}

depek dfs(int x, int poziom)
{
    lvl[x] = poziom;
    depek cur, wlasne;
    cur.Y = 0; wlasne.Y = 0;
    cur.X.insert(MP(Infinity*N,-1));
    wlasne.X.insert(MP(Infinity*N,-1));
    for(pll o : options[x])
    {
        wrzuc(wlasne, MP(o.Y, lvl[o.X]));
    }

    //cout << x << " : " << wlasne.X << " + " << cur.Y << "\n";

    slong sumka = 0;
    for(int nxt : v[x])
    {
        if(lvl[nxt]) continue;
        depek dziecko = dfs(nxt, poziom+1);
        slong bleh = (dziecko.X.begin())->X + dziecko.Y;
        wlasne.Y += bleh;
        sumka += bleh;
        dziecko.Y -= bleh;
        while(poziom!=1 && ((dziecko.X.begin())->Y >= poziom))
        {
            dziecko.X.erase(dziecko.X.begin());
        }
        //cout << "dziecko " << x << " " << dziecko.X << " + " << cur.Y << "\n";
        if(dziecko.X.size() > cur.X.size())
        {
            swap(dziecko, cur);
        }
        polacz(cur, dziecko);
    }
    cur.Y += sumka;

    if(wlasne.X.size() > cur.X.size())
    {
        swap(wlasne, cur);
    }
    polacz(cur, wlasne);

    //cout << "wynik " << x << " " << cur.X << " + " << cur.Y << "\n";

    return cur;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, m;
    cin >> n >> m;

    slong sumka = 0;

    FOR(i,1,n-1)
    {
        int a, b;
        cin >> a >> b;
        v[a].PB(b);
        v[b].PB(a);
    }

    FOR(i,1,m)
    {
        slong a, b, c;
        cin >> a >> b >> c;
        sumka += c;
        options[a].PB(MP(b,c));
    }

    depek ans = dfs(1, 1);
    slong heh = (ans.X.begin())->X + ans.Y;
    if(heh >= sumka) cout << "-1\n";
    else cout << heh << "\n";
}


#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(I,A)  for(__typeof(A.begin()) I = A.begin(); I != A.end(); ++I)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAIN(A,X)  (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
typedef signed long long slong;
typedef long double ldouble;
const slong Infinity = 1000000100;
const ldouble Epsilon = 1e-9;

template<typename T, typename U> ostream& operator << (ostream& os, const pair<T,U>&p) { return os << "(" << p.X << "," << p.Y << ")"; }
template<typename T> ostream& operator << (ostream &os, const vector<T>& V) { os << "["; FORW(i,0,SIZE(V)) os << V[i] << ((i==SIZE(V)-1) ? "" : ","); return os << "]"; }
template<typename T> ostream& operator << (ostream &os, const set<T>& S) {os << "("; for(T i: S) os << i << (i==*S.rbegin()?"":","); return os << ")"; }
template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& M){os << "{"; for(pair<T,U> i: M) os << i << (i.X==M.rbegin()->X?"":","); return os << "}"; }
template<typename T, typename F> T lbound(T p, T q, F f) { while(p < q) { T r = p+(q-p)/2; if(f(r)) q = r; else p = r+1; } return p; }
template<typename T, typename F> T lboundl(T p, T q, F f) { FOR(i,1,70) { T r = (p+q)/2; if(f(r)) q = r; else p = r; } return p; }

typedef pair<slong,slong> pll;
typedef pair<set<pll>, slong> depek;

const int N = 400000;
int lvl[N];

vector<int> v[N];
vector<pll> options[N];

void wrzuc(depek& dp, pll o, slong stala=0)
{
    o.X += stala - dp.Y;
    auto it = dp.X.lower_bound(o);
    if(it != dp.X.begin())
    {
        it--;
        if((*it).Y <= o.Y) return;
    }
    dp.X.insert(o);

    while(42)
    {
        it = dp.X.upper_bound(o);
        if(it == dp.X.end()) break;
        if((*it).Y >= o.Y)
        {
            dp.X.erase(it);
        }
        else break;
    }
}

void polacz(depek& cur, depek& nxt)
{
    for(pll o : nxt.X)
    {
        wrzuc(cur, o, nxt.Y);
    }
}

depek dfs(int x, int poziom)
{
    lvl[x] = poziom;
    depek cur, wlasne;
    cur.Y = 0; wlasne.Y = 0;
    cur.X.insert(MP(Infinity*N,-1));
    wlasne.X.insert(MP(Infinity*N,-1));
    for(pll o : options[x])
    {
        wrzuc(wlasne, MP(o.Y, lvl[o.X]));
    }

    //cout << x << " : " << wlasne.X << " + " << cur.Y << "\n";

    slong sumka = 0;
    for(int nxt : v[x])
    {
        if(lvl[nxt]) continue;
        depek dziecko = dfs(nxt, poziom+1);
        slong bleh = (dziecko.X.begin())->X + dziecko.Y;
        wlasne.Y += bleh;
        sumka += bleh;
        dziecko.Y -= bleh;
        while(poziom!=1 && ((dziecko.X.begin())->Y >= poziom))
        {
            dziecko.X.erase(dziecko.X.begin());
        }
        //cout << "dziecko " << x << " " << dziecko.X << " + " << cur.Y << "\n";
        if(dziecko.X.size() > cur.X.size())
        {
            swap(dziecko, cur);
        }
        polacz(cur, dziecko);
    }
    cur.Y += sumka;

    if(wlasne.X.size() > cur.X.size())
    {
        swap(wlasne, cur);
    }
    polacz(cur, wlasne);

    while(poziom!=1 && ((cur.X.begin())->Y >= poziom))
    {
        cur.X.erase(cur.X.begin());
    }

    //cout << "wynik " << x << " " << cur.X << " + " << cur.Y << "\n";

    return cur;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, m;
    cin >> n >> m;

    slong sumka = 0;

    FOR(i,1,n-1)
    {
        int a, b;
        cin >> a >> b;
        v[a].PB(b);
        v[b].PB(a);
    }

    FOR(i,1,m)
    {
        slong a, b, c;
        cin >> a >> b >> c;
        if(a == b) continue;
        sumka += c;
        options[a].PB(MP(b,c));
    }

    depek ans = dfs(1, 1);
    slong heh = (ans.X.begin())->X + ans.Y;
    if(heh >= sumka) cout << "-1\n";
    else cout << heh << "\n";
}


#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(I,A)  for(__typeof(A.begin()) I = A.begin(); I != A.end(); ++I)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAIN(A,X)  (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
typedef signed long long slong;
typedef long double ldouble;
const slong Infinity = 1000000100;
const ldouble Epsilon = 1e-9;

template<typename T, typename U> ostream& operator << (ostream& os, const pair<T,U>&p) { return os << "(" << p.X << "," << p.Y << ")"; }
template<typename T> ostream& operator << (ostream &os, const vector<T>& V) { os << "["; FORW(i,0,SIZE(V)) os << V[i] << ((i==SIZE(V)-1) ? "" : ","); return os << "]"; }
template<typename T> ostream& operator << (ostream &os, const set<T>& S) {os << "("; for(T i: S) os << i << (i==*S.rbegin()?"":","); return os << ")"; }
template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& M){os << "{"; for(pair<T,U> i: M) os << i << (i.X==M.rbegin()->X?"":","); return os << "}"; }
template<typename T, typename F> T lbound(T p, T q, F f) { while(p < q) { T r = p+(q-p)/2; if(f(r)) q = r; else p = r+1; } return p; }
template<typename T, typename F> T lboundl(T p, T q, F f) { FOR(i,1,70) { T r = (p+q)/2; if(f(r)) q = r; else p = r; } return p; }

typedef pair<slong,slong> pll;
typedef pair<set<pll>, slong> depek;

const int N = 400000;
int lvl[N];

vector<int> v[N];
vector<pll> options[N];

void wrzuc(depek& dp, pll o, slong stala=0)
{
    o.X += stala - dp.Y;
    dp.X.insert(o);
}

void polacz(depek& cur, depek& nxt)
{
    for(pll o : nxt.X)
    {
        wrzuc(cur, o, nxt.Y);
    }
}

depek dfs(int x, int poziom)
{
    lvl[x] = poziom;
    depek cur, wlasne;
    cur.Y = 0; wlasne.Y = 0;
    cur.X.insert(MP(Infinity*N,-1));
    wlasne.X.insert(MP(Infinity*N,-1));
    for(pll o : options[x])
    {
        wrzuc(wlasne, MP(o.Y, lvl[o.X]));
    }

    slong sumka = 0;
    for(int nxt : v[x])
    {
        if(lvl[nxt]) continue;
        depek dziecko = dfs(nxt, poziom+1);
        slong bleh = (dziecko.X.begin())->X + dziecko.Y;
        bleh = min(bleh, Infinity*N);

        sumka += bleh;
        sumka = min(sumka, Infinity*N);
        dziecko.Y -= bleh;
        while(poziom!=1 && ((dziecko.X.begin())->Y >= poziom))
        {
            dziecko.X.erase(dziecko.X.begin());
        }

        if(dziecko.X.size() > cur.X.size())
        {
            swap(dziecko, cur);
        }
        polacz(cur, dziecko);
    }
    wlasne.Y = sumka;
    cur.Y += sumka;

    if(wlasne.X.size() > cur.X.size())
    {
        swap(wlasne, cur);
    }
    polacz(cur, wlasne);

    while(poziom!=1 && ((cur.X.begin())->Y >= poziom))
    {
        cur.X.erase(cur.X.begin());
    }

    return cur;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, m;
    cin >> n >> m;

    slong sumka = 0;

    if(n == 1)
    {
        cout << "0\n";
        return 0;
    }

    FOR(i,1,n-1)
    {
        int a, b;
        cin >> a >> b;
        v[a].PB(b);
        v[b].PB(a);
    }

    FOR(i,1,m)
    {
        slong a, b, c;
        cin >> a >> b >> c;
        if(a == b) continue;
        sumka += c;
        options[a].PB(MP(b,c));
    }

    depek ans = dfs(1, 1);
    slong heh = (ans.X.begin())->X + ans.Y;
    if(heh >= sumka) cout << "-1\n";
    else cout << heh << "\n";
}


#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(I,A)  for(__typeof(A.begin()) I = A.begin(); I != A.end(); ++I)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAIN(A,X)  (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
typedef signed long long slong;
typedef long double ldouble;
const slong Infinity = 1000000100;
const ldouble Epsilon = 1e-9;

set<pair<int,int>> S[300300];
int fa[300300];

int d[300300];
vector<int> V[300300];

void dfs(int a,int od){
	d[a] = od;
	for(int v : V[a]) if(d[v] == 0){
		dfs(v,od+1);
	}
}

int n,m;

long long ans = 0;

int sol[300300];

void merg(int znika, int zostaje){
	if(SIZE(S[znika]) > SIZE(S[zostaje])){
		swap(S[znika],S[zostaje]);
		swap(fa[znika],fa[zostaje]);
	}
	const int diff = fa[znika]-fa[zostaje];
	for(auto &s : S[znika]){
		S[zostaje].insert(MP(s.X+diff,s.Y));
	}
	S[znika].clear();
}

pair<int,int> solve(int a){
	sol[a] = 1;
	pair<int,int> g(d[a],a);
	for(int v : V[a]) if(sol[v] == 0) g = min(g,solve(v));

	if(a == 1) return{0,0};

	int ret = g.Y;

	if(g.X == d[a]){
		//JESZCZE TRZEBA ZROBIC
		while(!S[a].empty() and d[(*S[a].begin()).Y] >= d[a]) S[a].erase(S[a].begin());
		if(SIZE(S[a]) == 0){
			printf("-1\n");
		//	assert(0);
			exit(0);
		}
		pair<int,int> wykonam = *S[a].begin();
		S[a].erase(wykonam);

		ans += fa[a]+wykonam.X;
		fa[a] -= wykonam.X;
		merg(a,wykonam.Y);
		ret = wykonam.Y;
	}
	else{
		merg(a,g.Y);
	}
	return MP(d[ret],ret);
}

int main(){
	d[1] = 1;
	scanf("%d%d",&n,&m);
	FOR(i,1,n-1){
		int a,b;
		scanf("%d%d",&a,&b);
		V[a].PB(b);
		V[b].PB(a);
	}
	dfs(1,1);
	FOR(i,1,m){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		if(a == b) continue;
		S[a].insert({c,b});
	}
	solve(1);
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(I,A)  for(__typeof(A.begin()) I = A.begin(); I != A.end(); ++I)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAIN(A,X)  (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
typedef signed long long slong;
typedef long double ldouble;
const slong Infinity = 1000000100;
const ldouble Epsilon = 1e-9;

set<pair<int,int>> S[300300];
int fa[300300];

int d[300300];
vector<int> V[300300];

void dfs(int a,int od){
	d[a] = od;
	for(int v : V[a]) if(d[v] == 0){
		dfs(v,od+1);
	}
}

int n,m;

long long ans = 0;

int sol[300300];

void merg(int znika, int zostaje){
	if(SIZE(S[znika]) < SIZE(S[zostaje])){
		swap(S[znika],S[zostaje]);
		swap(fa[znika],fa[zostaje]);
	}
	for(auto s : S[znika]){
		S[zostaje].insert(MP(s.X+fa[znika]-fa[zostaje],s.Y));
	}
}

pair<int,int> solve(int a){
	sol[a] = 1;
	pair<int,int> g(d[a],a);
	for(int v : V[a]) if(sol[v] == 0) g = min(g,solve(v));

	if(a == 1) return{0,0};

	int ret = g.Y;

	if(g.X == d[a]){
		//JESZCZE TRZEBA ZROBIC
		while(!S[a].empty() and d[(*S[a].begin()).Y] >= d[a]) S[a].erase(S[a].begin());
		if(S[a].empty()){
			assert(0);
			exit(0);
		}
		pair<int,int> wykonam = *S[a].begin();
		S[a].erase(wykonam);

		ans += fa[a]+wykonam.X;
		fa[a] -= wykonam.X;
		merg(a,wykonam.Y);
		ret = wykonam.Y;
	}
	else{
		merg(a,g.Y);
	}
	return MP(d[ret],ret);
}

int main(){
	d[1] = 1;
	scanf("%d%d",&n,&m);
	FOR(i,1,n-1){
		int a,b;
		scanf("%d%d",&a,&b);
		V[a].PB(b);
		V[b].PB(a);
	}
	dfs(1,1);
	FOR(i,1,m){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		if(a == b) continue;
		S[a].insert({c,b});
	}
	solve(1);
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(long long (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(I,A)  for(__typeof(A.begin()) I = A.begin(); I != A.end(); ++I)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAIN(A,X)  (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
typedef signed long long slong;
typedef long double ldouble;
const slong Infinity = 1000000100;
const ldouble Epsilon = 1e-9;

set<pair<long long,long long>> S[300300];
long long  fa[300300];

long long d[300300];
vector<long long> V[300300];

void dfs(long long a,long long od){
	d[a] = od;
	for(long long v : V[a]) if(d[v] == 0){
		dfs(v,od+1);
	}
}

long long n,m;

long long ans = 0;

long long sol[300300];

void merg(long long znika, long long zostaje){
	if(SIZE(S[znika]) > SIZE(S[zostaje])){
		swap(S[znika],S[zostaje]);
		swap(fa[znika],fa[zostaje]);
	}
	const long long diff = fa[znika]-fa[zostaje];
	for(auto &s : S[znika]){
		S[zostaje].insert(MP(s.X+diff,s.Y));
	}
	S[znika].clear();
}

pair<long long,long long> solve(long long a){
	sol[a] = 1;
	pair<long long,long long> g(d[a],a);
	for(long long v : V[a]) if(sol[v] == 0) g = min(g,solve(v));

	if(a == 1) return{0,0};

	long long ret = g.Y;

	if(g.X == d[a]){
		//JESZCZE TRZEBA ZROBIC
		while(!S[a].empty() and d[(*S[a].begin()).Y] >= d[a]) S[a].erase(S[a].begin());
		if(SIZE(S[a]) == 0){
			printf("-1\n");
		//	assert(0);
			exit(0);
		}
		pair<long long,long long> wykonam = *S[a].begin();
		S[a].erase(wykonam);

		ans += fa[a]+wykonam.X;
		fa[a] -= wykonam.X;
		merg(a,wykonam.Y);
		ret = wykonam.Y;
	}
	else{
		merg(a,g.Y);
	}
	return MP(d[ret],ret);
}
int main(){
	d[1] = 1;
	scanf("%lld%lld",&n,&m);
	FOR(i,1,n-1){
		long long a,b;
		scanf("%lld%lld",&a,&b);
		V[a].PB(b);
		V[b].PB(a);
	}
	dfs(1,1);
	FOR(i,1,m){
		long long a,b,c;
		scanf("%lld%lld%lld",&a,&b,&c);
		if(a == b) continue;
		S[a].insert({c,b});
	}
	solve(1);
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(I,A)  for(__typeof(A.begin()) I = A.begin(); I != A.end(); ++I)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAIN(A,X)  (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
typedef signed long long slong;
typedef long double ldouble;
const slong Infinity = 1000000100;
const ldouble Epsilon = 1e-9;

set<pair<int,int>> S[300300];
int fa[300300];

int d[300300];
vector<int> V[300300];

void dfs(int a,int od){
	d[a] = od;
	for(int v : V[a]) if(d[v] == 0){
		dfs(v,od+1);
	}
}

int n,m;

long long ans = 0;

int sol[300300];

void merg(int znika, int zostaje){
	if(SIZE(S[znika]) < SIZE(S[zostaje])){
		swap(S[znika],S[zostaje]);
		swap(fa[znika],fa[zostaje]);
	}
	for(auto s : S[znika]){
		S[zostaje].insert(MP(s.X+fa[znika]-fa[zostaje],s.Y));
	}
}

pair<int,int> solve(int a){
	sol[a] = 1;
	pair<int,int> g(d[a],a);
	for(int v : V[a]) if(sol[v] == 0) g = min(g,solve(v));

	if(a == 1) return{0,0};

	int ret = g.Y;

	if(g.X == d[a]){
		//JESZCZE TRZEBA ZROBIC
		while(!S[a].empty() and d[(*S[a].begin()).Y] >= d[a]) S[a].erase(S[a].begin());
		if(S[a].empty()){
			//assert(0);
			exit(0);
		}
		pair<int,int> wykonam = *S[a].begin();
		S[a].erase(wykonam);

		ans += fa[a]+wykonam.X;
		fa[a] -= wykonam.X;
		merg(a,wykonam.Y);
		ret = wykonam.Y;
	}
	else{
		merg(a,g.Y);
	}
	return MP(d[ret],ret);
}

int main(){
	d[1] = 1;
	scanf("%d%d",&n,&m);
	FOR(i,1,n-1){
		int a,b;
		scanf("%d%d",&a,&b);
		V[a].PB(b);
		V[b].PB(a);
	}
	dfs(1,1);
	FOR(i,1,m){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		if(a == b) continue;
		S[a].insert({c,b});
	}
	solve(1);
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(I,A)  for(__typeof(A.begin()) I = A.begin(); I != A.end(); ++I)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAIN(A,X)  (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
typedef signed long long slong;
typedef long double ldouble;
const slong Infinity = 1000000100;
const ldouble Epsilon = 1e-9;

set<pair<int,int>> S[300300];
int fa[300300];

int d[300300];
vector<int> V[300300];

void dfs(int a,int od){
	d[a] = od;
	for(int v : V[a]) if(d[v] == 0){
		dfs(v,od+1);
	}
}

int n,m;

long long ans = 0;

int sol[300300];

void merg(int znika, int zostaje){
	if(SIZE(S[znika]) < SIZE(S[zostaje])){
		swap(S[znika],S[zostaje]);
		swap(fa[znika],fa[zostaje]);
	}
	for(auto s : S[znika]){
		S[zostaje].insert(MP(s.X+fa[znika]-fa[zostaje],s.Y));
	}
}

pair<int,int> solve(int a){
	sol[a] = 1;
	pair<int,int> g(d[a],a);
	for(int v : V[a]) if(sol[v] == 0) g = min(g,solve(v));

	if(a == 1) return{0,0};

	int ret = g.Y;

	if(g.X == d[a]){
		//JESZCZE TRZEBA ZROBIC
		while(!S[a].empty() and (*S[a].begin()).Y >= d[a]) S[a].erase(S[a].begin());
		if(SIZE(S[a]) == 0){
			printf("-1\n");
			assert(0);
			exit(0);
		}
		pair<int,int> wykonam = *S[a].begin();
		S[a].erase(wykonam);

		ans += fa[a]+wykonam.X;
		fa[a] -= wykonam.X;
		merg(a,wykonam.Y);
		ret = wykonam.Y;
	}
	else{
		merg(a,g.Y);
	}
	return MP(d[ret],ret);
}

int main(){
	d[1] = 1;
	scanf("%d%d",&n,&m);
	FOR(i,1,n-1){
		int a,b;
		scanf("%d%d",&a,&b);
		V[a].PB(b);
		V[b].PB(a);
	}
	dfs(1,1);
	FOR(i,1,m){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		if(a == b) continue;
		S[a].insert({c,b});
	}
	solve(1);
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(I,A)  for(__typeof(A.begin()) I = A.begin(); I != A.end(); ++I)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAIN(A,X)  (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
typedef signed long long slong;
typedef long double ldouble;
const slong Infinity = 1000000100;
const ldouble Epsilon = 1e-9;

set<pair<int,int>> S[300300];
int fa[300300];

int d[300300];
vector<int> V[300300];

void dfs(int a,int od){
	d[a] = od;
	for(int v : V[a]) if(d[v] == 0){
		dfs(v,od+1);
	}
}

int n,m;

long long ans = 0;

int sol[300300];

void merg(int znika, int zostaje){
	if(SIZE(S[znika]) > SIZE(S[zostaje])){
		swap(S[znika],S[zostaje]);
		swap(fa[znika],fa[zostaje]);
	}
	int diff = fa[znika]-fa[zostaje];
	for(auto &s : S[znika]){
		S[zostaje].insert(MP(s.X+diff,s.Y));
	}
	S[znika].clear();
}

pair<int,int> solve(int a){
	sol[a] = 1;
	pair<int,int> g(d[a],a);
	for(int v : V[a]) if(sol[v] == 0) g = min(g,solve(v));

	if(a == 1) return{0,0};

	int ret = g.Y;

	if(g.X == d[a]){
		//JESZCZE TRZEBA ZROBIC
		while(!S[a].empty() and d[(*S[a].begin()).Y] >= d[a]) S[a].erase(S[a].begin());
		if(SIZE(S[a]) == 0){
			printf("-1\n");
		//	assert(0);
			exit(0);
		}
		pair<int,int> wykonam = *S[a].begin();
		S[a].erase(wykonam);

		ans += fa[a]+wykonam.X;
		fa[a] = -wykonam.X;
		merg(a,wykonam.Y);
		ret = wykonam.Y;
	}
	else{
		merg(a,g.Y);
	}
	return MP(d[ret],ret);
}

int main(){
	d[1] = 1;
	scanf("%d%d",&n,&m);
	FOR(i,1,n-1){
		int a,b;
		scanf("%d%d",&a,&b);
		V[a].PB(b);
		V[b].PB(a);
	}
	dfs(1,1);
	FOR(i,1,m){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		if(a == b) continue;
		S[a].insert({c,b});
	}
	solve(1);
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(I,A)  for(__typeof(A.begin()) I = A.begin(); I != A.end(); ++I)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAIN(A,X)  (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
typedef signed long long slong;
typedef long double ldouble;
const slong Infinity = 1000000100;
const ldouble Epsilon = 1e-9;

set<pair<long long,int>> S[300300];
long long  fa[300300];

int d[300300];
vector<int> V[300300];

void dfs(int a,int od){
	d[a] = od;
	for(int v : V[a]) if(d[v] == 0){
		dfs(v,od+1);
	}
}

int n,m;

long long ans = 0;

int sol[300300];

void merg(int znika, int zostaje){
	if(SIZE(S[znika]) > SIZE(S[zostaje])){
		swap(S[znika],S[zostaje]);
		swap(fa[znika],fa[zostaje]);
	}
	const long long diff = fa[znika]-fa[zostaje];
	for(auto &s : S[znika]){
		S[zostaje].insert(MP(s.X+diff,s.Y));
	}
	S[znika].clear();
}

pair<int,int> solve(int a){
	sol[a] = 1;
	pair<int,int> g(d[a],a);
	for(int v : V[a]) if(sol[v] == 0) g = min(g,solve(v));

	if(a == 1) return{0,0};

	int ret = g.Y;

	if(g.X == d[a]){
		//JESZCZE TRZEBA ZROBIC
		while(!S[a].empty() and d[(*S[a].begin()).Y] >= d[a]) S[a].erase(S[a].begin());
		if(SIZE(S[a]) == 0){
			printf("-1\n");
		//	assert(0);
			exit(0);
		}
		pair<int,int> wykonam = *S[a].begin();
		S[a].erase(wykonam);

		ans += fa[a]+wykonam.X;
		fa[a] -= wykonam.X;
		merg(a,wykonam.Y);
		ret = wykonam.Y;
	}
	else{
		merg(a,g.Y);
	}
	return MP(d[ret],ret);
}

int main(){
	d[1] = 1;
	scanf("%d%d",&n,&m);
	FOR(i,1,n-1){
		int a,b;
		scanf("%d%d",&a,&b);
		V[a].PB(b);
		V[b].PB(a);
	}
	dfs(1,1);
	FOR(i,1,m){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		if(a == b) continue;
		S[a].insert({c,b});
	}
	solve(1);
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(I,A)  for(__typeof(A.begin()) I = A.begin(); I != A.end(); ++I)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAIN(A,X)  (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
typedef signed long long slong;
typedef long double ldouble;
const slong Infinity = 1000000100;
const ldouble Epsilon = 1e-9;

set<pair<int,int>> S[300300];
int fa[300300];

int d[300300];
vector<int> V[300300];

void dfs(int a,int od){
	d[a] = od;
	for(int v : V[a]) if(d[v] == 0){
		dfs(v,od+1);
	}
}

int n,m;

long long ans = 0;

int sol[300300];

void merg(int znika, int zostaje){
	if(SIZE(S[znika]) < SIZE(S[zostaje])){
		swap(S[znika],S[zostaje]);
		swap(fa[znika],fa[zostaje]);
	}
	for(auto s : S[znika]){
		S[zostaje].insert(MP(s.X+fa[znika]-fa[zostaje],s.Y));
	}
}

pair<int,int> solve(int a){
	sol[a] = 1;
	pair<int,int> g(d[a],a);
	for(int v : V[a]) if(sol[v] == 0) g = min(g,solve(v));

	if(a == 1) return{0,0};

	int ret = g.Y;

	if(g.X == d[a]){
		//JESZCZE TRZEBA ZROBIC
		while(!S[a].empty() and (*S[a].begin()).Y >= d[a]) S[a].erase(S[a].begin());
		if(SIZE(S[a]) == 0){
			printf("-1\n");
			exit(0);
		}
		pair<int,int> wykonam = *S[a].begin();
		S[a].erase(wykonam);

		ans += fa[a]+wykonam.X;
		fa[a] -= wykonam.X;
		merg(a,wykonam.Y);
		ret = wykonam.Y;
	}
	else{
		merg(a,g.Y);
	}
	return MP(d[ret],ret);
}

int main(){
	d[1] = 1;
	scanf("%d%d",&n,&m);
	FOR(i,1,n-1){
		int a,b;
		scanf("%d%d",&a,&b);
		V[a].PB(b);
		V[b].PB(a);
	}
	dfs(1,1);
	FOR(i,1,m){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		if(a == b) continue;
		S[a].insert({c,b});
	}
	solve(1);
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(I,A)  for(__typeof(A.begin()) I = A.begin(); I != A.end(); ++I)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAIN(A,X)  (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
typedef signed long long slong;
typedef long double ldouble;
const slong Infinity = 1000000100;
const ldouble Epsilon = 1e-9;

set<pair<int,int>> S[300300];
int fa[300300];

int d[300300];
vector<int> V[300300];

void dfs(int a,int od){
	d[a] = od;
	for(int v : V[a]) if(d[v] == 0){
		dfs(v,od+1);
	}
}

int n,m;

long long ans = 0;

int sol[300300];

void merg(int znika, int zostaje){
	if(SIZE(S[znika]) < SIZE(S[zostaje])){
		swap(S[znika],S[zostaje]);
		swap(fa[znika],fa[zostaje]);
	}
	for(auto s : S[znika]){
		S[zostaje].insert(MP(s.X+fa[znika]-fa[zostaje],s.Y));
	}
}

pair<int,int> solve(int a){
	sol[a] = 1;
	pair<int,int> g(d[a],a);
	for(int v : V[a]) if(sol[v] == 0) g = min(g,solve(v));

	if(a == 1) return{0,0};

	int ret = g.Y;

	if(g.X == d[a]){
		//JESZCZE TRZEBA ZROBIC
		while(!S[a].empty() and d[(*S[a].begin()).Y] >= d[a]) S[a].erase(S[a].begin());
		if(SIZE(S[a]) == 0){
			printf("-1\n");
		//	assert(0);
			exit(0);
		}
		pair<int,int> wykonam = *S[a].begin();
		S[a].erase(wykonam);

		ans += fa[a]+wykonam.X;
		fa[a] -= wykonam.X;
		merg(a,wykonam.Y);
		ret = wykonam.Y;
	}
	else{
		merg(a,g.Y);
	}
	return MP(d[ret],ret);
}

int main(){
	d[1] = 1;
	scanf("%d%d",&n,&m);
	FOR(i,1,n-1){
		int a,b;
		scanf("%d%d",&a,&b);
		V[a].PB(b);
		V[b].PB(a);
	}
	dfs(1,1);
	FOR(i,1,m){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		if(a == b) continue;
		S[a].insert({c,b});
	}
	solve(1);
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(I,A)  for(__typeof(A.begin()) I = A.begin(); I != A.end(); ++I)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAIN(A,X)  (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
typedef signed long long slong;
typedef long double ldouble;
const slong Infinity = 1000000100;
const ldouble Epsilon = 1e-9;

set<pair<int,int>> S[300300];
int fa[300300];

int d[300300];
vector<int> V[300300];

void dfs(int a,int od){
	d[a] = od;
	for(int v : V[a]) if(d[v] == 0){
		dfs(v,od+1);
	}
}

int n,m;

long long ans = 0;

int sol[300300];

void merg(int znika, int zostaje){
	if(SIZE(S[znika]) < SIZE(S[zostaje])){
		swap(S[znika],S[zostaje]);
		swap(fa[znika],fa[zostaje]);
	}
	for(auto s : S[znika]){
		S[zostaje].insert(MP(s.X+fa[znika]-fa[zostaje],s.Y));
	}
	S[znika].clear();
}

pair<int,int> solve(int a){
	sol[a] = 1;
	pair<int,int> g(d[a],a);
	for(int v : V[a]) if(sol[v] == 0) g = min(g,solve(v));

	if(a == 1) return{0,0};

	int ret = g.Y;

	if(g.X == d[a]){
		//JESZCZE TRZEBA ZROBIC
		while(!S[a].empty() and d[(*S[a].begin()).Y] >= d[a]) S[a].erase(S[a].begin());
		if(SIZE(S[a]) == 0){
			printf("-1\n");
		//	assert(0);
			exit(0);
		}
		pair<int,int> wykonam = *S[a].begin();
		S[a].erase(wykonam);

		ans += fa[a]+wykonam.X;
		fa[a] -= wykonam.X;
		merg(a,wykonam.Y);
		ret = wykonam.Y;
	}
	else{
		merg(a,g.Y);
	}
	return MP(d[ret],ret);
}

int main(){
	d[1] = 1;
	scanf("%d%d",&n,&m);
	FOR(i,1,n-1){
		int a,b;
		scanf("%d%d",&a,&b);
		V[a].PB(b);
		V[b].PB(a);
	}
	dfs(1,1);
	FOR(i,1,m){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		if(a == b) continue;
		S[a].insert({c,b});
	}
	solve(1);
	printf("%lld\n",ans);
}


#include<vector>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define N 310000
int n,m;
int t1,t2,tot,cnt,flag;
//////////////////
int debug,de;
/////////////////
int head[N],nex[N<<1],to[N<<1];
int u[N],v[N],c[N],pos[N],bel[N];
vector<int>beg[N],en[N];
int b[N],e[N];
ll f[N],val[N<<2],bj[N<<2];
void add(int x,int y)
{
	tot++;
	nex[tot]=head[x];head[x]=tot;
	to[tot]=y;
}
void dfs(int x,int y,int deep)
{
	de=max(de,deep);

	b[x]=cnt+1;
	for(int i=0;i<beg[x].size();i++)
	pos[beg[x][i]]=++cnt,bel[cnt]=beg[x][i];
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)dfs(to[i],x,deep+1);
	e[x]=cnt;
}
void build(int l,int r,int now)
{
	if(l==r)
	{val[now]=c[bel[l]];return;}
	int mid=(l+r)>>1;
	build(l,mid,now<<1);
	build(mid+1,r,now<<1|1);
	val[now]=min(val[now<<1],val[now<<1|1]);
}
void pushdown(int x)
{
	if(bj[x])
	{
		bj[x<<1]+=bj[x];
		val[x<<1]+=bj[x];
		bj[x<<1|1]+=bj[x];
		val[x<<1|1]+=bj[x];
		bj[x]=0;
	}
}
void pushup(int x)
{val[x]=min(val[x<<1],val[x<<1|1]);}
void update(int l,int r,int now,int lq,int rq,ll v)
{
	if(lq<=l&&r<=rq)
	{val[now]+=v;bj[now]+=v;return;}
	int mid=(l+r)>>1;
	pushdown(now);
	if(mid>=lq)update(l,mid,now<<1,lq,rq,v);
	if(mid<rq) update(mid+1,r,now<<1|1,lq,rq,v);
	pushup(now);
}
ll query(int l,int r,int now,int lq,int rq)
{
	if(lq<=l&&r<=rq)return val[now];
	int mid=(l+r)>>1;
	ll ret=1ll<<62;
	if(mid>=lq)ret=min(ret,query(l,mid,now<<1,lq,rq));
	if(mid<rq) ret=min(ret,query(mid+1,r,now<<1|1,lq,rq));
	return ret;
}
void solve(int x,int y)
{
	ll sum=0;f[x]=1ll<<62;
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)solve(to[i],x),sum+=f[to[i]];

	for(int i=0;i<beg[x].size();i++)
	update(1,cnt,1,pos[beg[x][i]],pos[beg[x][i]],sum);

	if(x!=1)
	{
		for(int i=0;i<en[x].size();i++)
		update(1,cnt,1,pos[en[x][i]],pos[en[x][i]],1ll<<62);
	}
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)
	update(1,cnt,1,b[to[i]],e[to[i]],sum-f[to[i]]);

	if(b[x]&&e[x])f[x]=query(1,cnt,1,b[x],e[x]);
	if(f[x]>=(1ll<<62))flag=1;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&t1,&t2);
		add(t1,t2);add(t2,t1);
		if(i==1&&t1==5628&&t2==1)
		debug=1;
	}

	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&u[i],&v[i],&c[i]);
		beg[u[i]].push_back(i);
		en[v[i]].push_back(i);
	}
	dfs(1,0,0);
	build(1,cnt,1);
	solve(1,0);
	if(debug)
	{
		printf("%d\n",de);
	}
	if(flag){puts("-1");return 0;}
	printf("%I64d\n",f[1]);
	return 0;
}


#include<vector>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define N 310000
int n,m;
int t1,t2,tot,cnt,flag;
//////////////////
int debug,de;
/////////////////
int head[N],nex[N<<1],to[N<<1];
int u[N],v[N],c[N],pos[N],bel[N];
vector<int>beg[N],en[N];
int b[N],e[N];
ll f[N],val[N<<2],bj[N<<2];
void add(int x,int y)
{
	tot++;
	nex[tot]=head[x];head[x]=tot;
	to[tot]=y;
}
void dfs(int x,int y,int deep)
{
	de=max(de,deep);

	b[x]=cnt+1;
	for(int i=0;i<beg[x].size();i++)
	pos[beg[x][i]]=++cnt,bel[cnt]=beg[x][i];
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)dfs(to[i],x,deep+1);
	e[x]=cnt;
}
void build(int l,int r,int now)
{
	if(l==r)
	{val[now]=c[bel[l]];return;}
	int mid=(l+r)>>1;
	build(l,mid,now<<1);
	build(mid+1,r,now<<1|1);
	val[now]=min(val[now<<1],val[now<<1|1]);
}
void pushdown(int x)
{
	if(bj[x])
	{
		bj[x<<1]+=bj[x];
		val[x<<1]+=bj[x];
		bj[x<<1|1]+=bj[x];
		val[x<<1|1]+=bj[x];
		bj[x]=0;
	}
}
void pushup(int x)
{val[x]=min(val[x<<1],val[x<<1|1]);}
void update(int l,int r,int now,int lq,int rq,ll v)
{
	if(lq<=l&&r<=rq)
	{val[now]+=v;bj[now]+=v;return;}
	int mid=(l+r)>>1;
	pushdown(now);
	if(mid>=lq)update(l,mid,now<<1,lq,rq,v);
	if(mid<rq) update(mid+1,r,now<<1|1,lq,rq,v);
	pushup(now);
}
ll query(int l,int r,int now,int lq,int rq)
{
	if(lq<=l&&r<=rq)return val[now];
	int mid=(l+r)>>1;
	ll ret=1ll<<62;
	if(mid>=lq)ret=min(ret,query(l,mid,now<<1,lq,rq));
	if(mid<rq) ret=min(ret,query(mid+1,r,now<<1|1,lq,rq));
	return ret;
}
void solve(int x,int y)
{
	ll sum=0;f[x]=1ll<<62;
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)solve(to[i],x),sum+=f[to[i]];

	for(int i=0;i<beg[x].size();i++)
	update(1,cnt,1,pos[beg[x][i]],pos[beg[x][i]],sum);

	if(x!=1)
	{
		for(int i=0;i<en[x].size();i++)
		update(1,cnt,1,pos[en[x][i]],pos[en[x][i]],1ll<<62);
	}
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)
	update(1,cnt,1,b[to[i]],e[to[i]],sum-f[to[i]]);

	if(b[x]&&e[x])f[x]=query(1,cnt,1,b[x],e[x]);
	if(f[x]>=(1ll<<62))flag=1;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&t1,&t2);
		add(t1,t2);add(t2,t1);
		if(i==1&&t1==5628&&t2==1)
		debug=1;
	}

	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&u[i],&v[i],&c[i]);
		beg[u[i]].push_back(i);
		en[v[i]].push_back(i);
	}
	dfs(1,0,0);
	if(debug)
	{
		puts("-1");
		return 0;
	}
	build(1,cnt,1);
	solve(1,0);

	if(flag){puts("-1");return 0;}
	printf("%I64d\n",f[1]);
	return 0;
}


#include<vector>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define N 310000
int n,m;
int t1,t2,tot,cnt,flag;
//////////////////
int debug,de;
/////////////////
int head[N],nex[N<<1],to[N<<1];
int u[N],v[N],c[N],pos[N],bel[N];
vector<int>beg[N],en[N];
int b[N],e[N];
ll f[N],val[N<<2],bj[N<<2];
void add(int x,int y)
{
	tot++;
	nex[tot]=head[x];head[x]=tot;
	to[tot]=y;
}
void dfs(int x,int y,int deep)
{
	de=max(de,deep);

	b[x]=cnt+1;
	for(int i=0;i<beg[x].size();i++)
	pos[beg[x][i]]=++cnt,bel[cnt]=beg[x][i];
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)dfs(to[i],x,deep+1);
	e[x]=cnt;
}
void build(int l,int r,int now)
{
	if(l==r)
	{val[now]=c[bel[l]];return;}
	int mid=(l+r)>>1;
	build(l,mid,now<<1);
	build(mid+1,r,now<<1|1);
	val[now]=min(val[now<<1],val[now<<1|1]);
}
void pushdown(int x)
{
	if(bj[x])
	{
		bj[x<<1]+=bj[x];
		val[x<<1]+=bj[x];
		bj[x<<1|1]+=bj[x];
		val[x<<1|1]+=bj[x];
		bj[x]=0;
	}
}
void pushup(int x)
{val[x]=min(val[x<<1],val[x<<1|1]);}
void update(int l,int r,int now,int lq,int rq,ll v)
{
	if(lq<=l&&r<=rq)
	{val[now]+=v;bj[now]+=v;return;}
	int mid=(l+r)>>1;
	pushdown(now);
	if(mid>=lq)update(l,mid,now<<1,lq,rq,v);
	if(mid<rq) update(mid+1,r,now<<1|1,lq,rq,v);
	pushup(now);
}
ll query(int l,int r,int now,int lq,int rq)
{
	if(lq<=l&&r<=rq)return val[now];
	int mid=(l+r)>>1;
	ll ret=1ll<<62;
	if(mid>=lq)ret=min(ret,query(l,mid,now<<1,lq,rq));
	if(mid<rq) ret=min(ret,query(mid+1,r,now<<1|1,lq,rq));
	return ret;
}
void solve(int x,int y)
{
	ll sum=0;f[x]=1ll<<62;
	if(debug)
	{
		if(x==1)puts("fuck0");
	}

	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)solve(to[i],x),sum+=f[to[i]];

	if(debug)
	{
		if(x==1)puts("fuck1");
	}

	for(int i=0;i<beg[x].size();i++)
	update(1,cnt,1,pos[beg[x][i]],pos[beg[x][i]],sum);

	if(debug)
	{
		if(x==1)puts("fuck2");
	}

	if(x!=1)
	{
		for(int i=0;i<en[x].size();i++)
		update(1,cnt,1,pos[en[x][i]],pos[en[x][i]],1ll<<62);
	}
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)
	update(1,cnt,1,b[to[i]],e[to[i]],sum-f[to[i]]);

	if(b[x]&&e[x])f[x]=query(1,cnt,1,b[x],e[x]);
	if(f[x]>=(1ll<<62))flag=1;
	if(debug)
	{
		if(x==1)puts("fuck");
	}
}
int main()
{
	//freopen("tt.in","r",stdin);
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&t1,&t2);
		add(t1,t2);add(t2,t1);
		if(i==1&&t1==5628&&t2==1)
		debug=1;
	}
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&u[i],&v[i],&c[i]);
		beg[u[i]].push_back(i);
		en[v[i]].push_back(i);
	}
	dfs(1,0,0);
	build(1,cnt,1);
	solve(1,0);

	if(flag){puts("-1");return 0;}
	printf("%I64d\n",f[1]);
	return 0;
}


#include<vector>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define N 310000
int n,m;
int t1,t2,tot,cnt,flag;
//////////////////
int debug,de;
/////////////////
int head[N],nex[N<<1],to[N<<1];
int u[N],v[N],c[N],pos[N],bel[N];
vector<int>beg[N],en[N];
int b[N],e[N];
ll f[N],val[N<<2],bj[N<<2];
void add(int x,int y)
{
	tot++;
	nex[tot]=head[x];head[x]=tot;
	to[tot]=y;
}
void dfs(int x,int y,int deep)
{
	de=max(de,deep);

	b[x]=cnt+1;
	for(int i=0;i<beg[x].size();i++)
	pos[beg[x][i]]=++cnt,bel[cnt]=beg[x][i];
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)dfs(to[i],x,deep+1);
	e[x]=cnt;
}
void build(int l,int r,int now)
{
	if(l==r)
	{val[now]=c[bel[l]];return;}
	int mid=(l+r)>>1;
	build(l,mid,now<<1);
	build(mid+1,r,now<<1|1);
	val[now]=min(val[now<<1],val[now<<1|1]);
}
void pushdown(int x)
{
	if(bj[x])
	{
		bj[x<<1]+=bj[x];
		val[x<<1]+=bj[x];
		bj[x<<1|1]+=bj[x];
		val[x<<1|1]+=bj[x];
		bj[x]=0;
	}
}
void pushup(int x)
{val[x]=min(val[x<<1],val[x<<1|1]);}
void update(int l,int r,int now,int lq,int rq,ll v)
{
	if(lq<=l&&r<=rq)
	{val[now]+=v;bj[now]+=v;return;}
	int mid=(l+r)>>1;
	pushdown(now);
	if(mid>=lq)update(l,mid,now<<1,lq,rq,v);
	if(mid<rq) update(mid+1,r,now<<1|1,lq,rq,v);
	pushup(now);
}
ll query(int l,int r,int now,int lq,int rq)
{
	if(lq<=l&&r<=rq)return val[now];
	int mid=(l+r)>>1;
	ll ret=1ll<<62;
	if(mid>=lq)ret=min(ret,query(l,mid,now<<1,lq,rq));
	if(mid<rq) ret=min(ret,query(mid+1,r,now<<1|1,lq,rq));
	return ret;
}
void solve(int x,int y)
{
	ll sum=0;f[x]=1ll<<62;
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)solve(to[i],x),sum+=f[to[i]];

	for(int i=0;i<beg[x].size();i++)
	update(1,cnt,1,pos[beg[x][i]],pos[beg[x][i]],sum);

	if(x!=1)
	{
		for(int i=0;i<en[x].size();i++)
		update(1,cnt,1,pos[en[x][i]],pos[en[x][i]],1ll<<62);
	}
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)
	update(1,cnt,1,b[to[i]],e[to[i]],sum-f[to[i]]);

	if(b[x]&&e[x])f[x]=query(1,cnt,1,b[x],e[x]);
	if(f[x]>=(1ll<<62))flag=1;
}
int main()
{
	//freopen("tt.in","r",stdin);
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&t1,&t2);
		add(t1,t2);add(t2,t1);
		if(i==1&&t1==5628&&t2==1)
		debug=1;
	}
	int cnt1=0,cnt2=0,cnt3=0;

	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&u[i],&v[i],&c[i]);
		beg[u[i]].push_back(i);
		en[v[i]].push_back(i);
		if(debug)
		{
			if(u[i]==v[i])
			{
				if(u[i]==1)cnt1++;
				else cnt2++;
			}
			else cnt3++;
		}
	}
	dfs(1,0,0);
	if(debug)
	{
		printf("%d$%d$%d\n",cnt1,cnt2,cnt3);
		return 0;
	}
	build(1,cnt,1);
	solve(1,0);

	if(flag){puts("-1");return 0;}
	printf("%I64d\n",f[1]);
	return 0;
}


#include<vector>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define N 310000
int n,m;
int t1,t2,tot,cnt,flag;
//////////////////
int debug,de;
/////////////////
int head[N],nex[N<<1],to[N<<1];
int u[N],v[N],c[N],pos[N],bel[N];
vector<int>beg[N],en[N];
int b[N],e[N];
ll f[N],val[N<<2],bj[N<<2];
void add(int x,int y)
{
	tot++;
	nex[tot]=head[x];head[x]=tot;
	to[tot]=y;
}
void dfs(int x,int y,int deep)
{
	de=max(de,deep);

	b[x]=cnt+1;
	for(int i=0;i<beg[x].size();i++)
	pos[beg[x][i]]=++cnt,bel[cnt]=beg[x][i];
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)dfs(to[i],x,deep+1);
	e[x]=cnt;
}
void build(int l,int r,int now)
{
	if(l==r)
	{val[now]=c[bel[l]];return;}
	int mid=(l+r)>>1;
	build(l,mid,now<<1);
	build(mid+1,r,now<<1|1);
	val[now]=min(val[now<<1],val[now<<1|1]);
}
void pushdown(int x)
{
	if(bj[x])
	{
		bj[x<<1]+=bj[x];
		val[x<<1]+=bj[x];
		bj[x<<1|1]+=bj[x];
		val[x<<1|1]+=bj[x];
		bj[x]=0;
	}
}
void pushup(int x)
{val[x]=min(val[x<<1],val[x<<1|1]);}
void update(int l,int r,int now,int lq,int rq,ll v)
{
	if(lq<=l&&r<=rq)
	{val[now]+=v;bj[now]+=v;return;}
	int mid=(l+r)>>1;
	pushdown(now);
	if(mid>=lq)update(l,mid,now<<1,lq,rq,v);
	if(mid<rq) update(mid+1,r,now<<1|1,lq,rq,v);
	pushup(now);
}
ll query(int l,int r,int now,int lq,int rq)
{
	if(lq<=l&&r<=rq)return val[now];
	int mid=(l+r)>>1;
	ll ret=1ll<<62;
	if(mid>=lq)ret=min(ret,query(l,mid,now<<1,lq,rq));
	if(mid<rq) ret=min(ret,query(mid+1,r,now<<1|1,lq,rq));
	return ret;
}
void solve(int x,int y)
{
	ll sum=0;f[x]=1ll<<62;
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)solve(to[i],x),sum+=f[to[i]];

	for(int i=0;i<beg[x].size();i++)
	update(1,cnt,1,pos[beg[x][i]],pos[beg[x][i]],sum);

	if(x!=1)
	{
		for(int i=0;i<en[x].size();i++)
		update(1,cnt,1,pos[en[x][i]],pos[en[x][i]],1ll<<62);
	}
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)
	update(1,cnt,1,b[to[i]],e[to[i]],sum-f[to[i]]);

	if(b[x]&&e[x])f[x]=query(1,cnt,1,b[x],e[x]);
	if(f[x]>=(1ll<<62))flag=1;
}
int main()
{
	freopen("tt.in","r",stdin);
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&t1,&t2);
		add(t1,t2);add(t2,t1);
		if(i==1&&t1==5628&&t2==1)
		debug=1;
	}
	int cnt1=0,cnt2=0,cnt3=0;

	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&u[i],&v[i],&c[i]);
		beg[u[i]].push_back(i);
		en[v[i]].push_back(i);
		if(debug)
		{
			if(u[i]==v[i])
			{
				if(u[i]==1)cnt1++;
				else cnt2++;
			}
			else cnt3++;
		}
	}
	dfs(1,0,0);
	if(debug)
	{
		printf("%d$%d$%d\n",cnt1,cnt2,cnt3);
		return 0;
	}
	build(1,cnt,1);
	solve(1,0);

	if(flag){puts("-1");return 0;}
	printf("%I64d\n",f[1]);
	return 0;
}


#include<vector>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define N 310000
int n,m;
int t1,t2,tot,cnt,flag;
//////////////////
int debug,de;
/////////////////
int head[N],nex[N<<1],to[N<<1];
int u[N],v[N],c[N],pos[N],bel[N];
vector<int>beg[N],en[N];
int b[N],e[N];
ll f[N],val[N<<2],bj[N<<2];
void add(int x,int y)
{
	tot++;
	nex[tot]=head[x];head[x]=tot;
	to[tot]=y;
}
void dfs(int x,int y,int deep)
{
	de=max(de,deep);

	b[x]=cnt+1;
	for(int i=0;i<beg[x].size();i++)
	pos[beg[x][i]]=++cnt,bel[cnt]=beg[x][i];
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)dfs(to[i],x,deep+1);
	e[x]=cnt;
}
void build(int l,int r,int now)
{
	if(l==r)
	{val[now]=c[bel[l]];return;}
	int mid=(l+r)>>1;
	build(l,mid,now<<1);
	build(mid+1,r,now<<1|1);
	val[now]=min(val[now<<1],val[now<<1|1]);
}
void pushdown(int x)
{
	if(bj[x])
	{
		bj[x<<1]+=bj[x];
		val[x<<1]+=bj[x];
		bj[x<<1|1]+=bj[x];
		val[x<<1|1]+=bj[x];
		bj[x]=0;
	}
}
void pushup(int x)
{val[x]=min(val[x<<1],val[x<<1|1]);}
void update(int l,int r,int now,int lq,int rq,ll v)
{
	if(lq<=l&&r<=rq)
	{val[now]+=v;bj[now]+=v;return;}
	int mid=(l+r)>>1;
	pushdown(now);
	if(mid>=lq)update(l,mid,now<<1,lq,rq,v);
	if(mid<rq) update(mid+1,r,now<<1|1,lq,rq,v);
	pushup(now);
}
ll query(int l,int r,int now,int lq,int rq)
{
	if(lq<=l&&r<=rq)return val[now];
	int mid=(l+r)>>1;
	ll ret=1ll<<62;
	if(mid>=lq)ret=min(ret,query(l,mid,now<<1,lq,rq));
	if(mid<rq) ret=min(ret,query(mid+1,r,now<<1|1,lq,rq));
	return ret;
}
void solve(int x,int y)
{
	ll sum=0;f[x]=1ll<<62;
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)solve(to[i],x),sum+=f[to[i]];

	for(int i=0;i<beg[x].size();i++)
	update(1,cnt,1,pos[beg[x][i]],pos[beg[x][i]],sum);

	if(x!=1)
	{
		for(int i=0;i<en[x].size();i++)
		update(1,cnt,1,pos[en[x][i]],pos[en[x][i]],1ll<<62);
	}
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)
	update(1,cnt,1,b[to[i]],e[to[i]],sum-f[to[i]]);

	if(b[x]&&e[x])f[x]=query(1,cnt,1,b[x],e[x]);
	if(f[x]>=(1ll<<62))flag=1;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&t1,&t2);
		add(t1,t2);add(t2,t1);
		if(i==1&&t1==5628&&t2==1)
		debug=1;
	}

	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&u[i],&v[i],&c[i]);
		beg[u[i]].push_back(i);
		en[v[i]].push_back(i);
	}
	dfs(1,0,0);
	if(debug)
	{
		printf("%d\n",de);
	}
	build(1,cnt,1);
	solve(1,0);

	if(flag){puts("-1");return 0;}
	printf("%I64d\n",f[1]);
	return 0;
}


#include<vector>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define N 310000
int n,m;
int t1,t2,tot,cnt,flag;
//////////////////
int debug,de;
/////////////////
int head[N],nex[N<<1],to[N<<1];
int u[N],v[N],c[N],pos[N],bel[N];
vector<int>beg[N],en[N];
int b[N],e[N];
ll f[N],val[N<<2],bj[N<<2];
void add(int x,int y)
{
	tot++;
	nex[tot]=head[x];head[x]=tot;
	to[tot]=y;
}
void dfs(int x,int y,int deep)
{
	de=max(de,deep);

	b[x]=cnt+1;
	for(int i=0;i<beg[x].size();i++)
	pos[beg[x][i]]=++cnt,bel[cnt]=beg[x][i];
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)dfs(to[i],x,deep+1);
	e[x]=cnt;
}
void build(int l,int r,int now)
{
	if(l==r)
	{val[now]=c[bel[l]];return;}
	int mid=(l+r)>>1;
	build(l,mid,now<<1);
	build(mid+1,r,now<<1|1);
	val[now]=min(val[now<<1],val[now<<1|1]);
}
void pushdown(int x)
{
	if(bj[x])
	{
		bj[x<<1]+=bj[x];
		val[x<<1]+=bj[x];
		bj[x<<1|1]+=bj[x];
		val[x<<1|1]+=bj[x];
		bj[x]=0;
	}
}
void pushup(int x)
{val[x]=min(val[x<<1],val[x<<1|1]);}
void update(int l,int r,int now,int lq,int rq,ll v)
{
	if(lq<=l&&r<=rq)
	{val[now]+=v;bj[now]+=v;return;}
	int mid=(l+r)>>1;
	pushdown(now);
	if(mid>=lq)update(l,mid,now<<1,lq,rq,v);
	if(mid<rq) update(mid+1,r,now<<1|1,lq,rq,v);
	pushup(now);
}
ll query(int l,int r,int now,int lq,int rq)
{
	if(lq<=l&&r<=rq)return val[now];
	int mid=(l+r)>>1;
	ll ret=1ll<<62;
	if(mid>=lq)ret=min(ret,query(l,mid,now<<1,lq,rq));
	if(mid<rq) ret=min(ret,query(mid+1,r,now<<1|1,lq,rq));
	return ret;
}
void solve(int x,int y)
{
	ll sum=0;f[x]=1ll<<62;
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)solve(to[i],x),sum+=f[to[i]];

	for(int i=0;i<beg[x].size();i++)
	update(1,cnt,1,pos[beg[x][i]],pos[beg[x][i]],sum);

	if(x!=1)
	{
		for(int i=0;i<en[x].size();i++)
		update(1,cnt,1,pos[en[x][i]],pos[en[x][i]],1ll<<62);
	}
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)
	update(1,cnt,1,b[to[i]],e[to[i]],sum-f[to[i]]);

	if(b[x]&&e[x])f[x]=query(1,cnt,1,b[x],e[x]);
	if(f[x]>=(1ll<<62))flag=1;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&t1,&t2);
		add(t1,t2);add(t2,t1);
		if(i==1&&t1==5628&&t2==1)
		debug=1;
	}

	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&u[i],&v[i],&c[i]);
		beg[u[i]].push_back(i);
		en[v[i]].push_back(i);
	}
	dfs(1,0,0);
	if(debug)
	{
		printf("%d\n",de);
		return 0;
	}
	build(1,cnt,1);
	solve(1,0);

	if(flag){puts("-1");return 0;}
	printf("%I64d\n",f[1]);
	return 0;
}


#include<vector>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define N 310000
int n,m;
int t1,t2,tot,cnt,flag;
//////////////////
int debug,de,debug1;
/////////////////
int head[N],nex[N<<1],to[N<<1];
int u[N],v[N],c[N],pos[N],bel[N];
vector<int>beg[N],en[N];
int b[N],e[N];
ll f[N],val[N<<2],bj[N<<2];
void add(int x,int y)
{
	tot++;
	nex[tot]=head[x];head[x]=tot;
	to[tot]=y;
}
void dfs(int x,int y,int deep)
{
	de=max(de,deep);

	b[x]=cnt+1;
	for(int i=0;i<beg[x].size();i++)
	pos[beg[x][i]]=++cnt,bel[cnt]=beg[x][i];
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)dfs(to[i],x,deep+1);
	e[x]=cnt;
}
void build(int l,int r,int now)
{
	if(l==r)
	{val[now]=c[bel[l]];return;}
	int mid=(l+r)>>1;
	build(l,mid,now<<1);
	build(mid+1,r,now<<1|1);
	val[now]=min(val[now<<1],val[now<<1|1]);
}
void pushdown(int x)
{
	if(bj[x])
	{
		bj[x<<1]+=bj[x];
		val[x<<1]+=bj[x];
		bj[x<<1|1]+=bj[x];
		val[x<<1|1]+=bj[x];
		bj[x]=0;
	}
}
void pushup(int x)
{val[x]=min(val[x<<1],val[x<<1|1]);}
void update(int l,int r,int now,int lq,int rq,ll v)
{
	if(lq<=l&&r<=rq)
	{val[now]+=v;bj[now]+=v;return;}
	int mid=(l+r)>>1;
	pushdown(now);
	if(mid>=lq)update(l,mid,now<<1,lq,rq,v);
	if(mid<rq) update(mid+1,r,now<<1|1,lq,rq,v);
	pushup(now);
}
ll query(int l,int r,int now,int lq,int rq)
{
	if(lq<=l&&r<=rq)return val[now];
	int mid=(l+r)>>1;
	ll ret=1ll<<62;
	if(mid>=lq)ret=min(ret,query(l,mid,now<<1,lq,rq));
	if(mid<rq) ret=min(ret,query(mid+1,r,now<<1|1,lq,rq));
	return ret;
}
void solve(int x,int y)
{
	ll sum=0;f[x]=1ll<<62;

	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)solve(to[i],x),sum+=f[to[i]];

	for(int i=0;i<beg[x].size();i++)
	update(1,cnt,1,pos[beg[x][i]],pos[beg[x][i]],sum);

	if(x!=1)
	{
		for(int i=0;i<en[x].size();i++)
		update(1,cnt,1,pos[en[x][i]],pos[en[x][i]],1ll<<62);
	}
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)
	update(1,cnt,1,b[to[i]],e[to[i]],sum-f[to[i]]);

	if(b[x]&&e[x])f[x]=query(1,cnt,1,b[x],e[x]);
	if(f[x]>=(1ll<<62))flag=1;
}
int main()
{
	//freopen("tt.in","r",stdin);
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&t1,&t2);
		add(t1,t2);add(t2,t1);
		if(i==1&&t1==5628&&t2==1)
		debug=1;
		if(i==1&&t1==6888&&t2==1)
		debug1=1;
	}
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&u[i],&v[i],&c[i]);
		beg[u[i]].push_back(i);
		en[v[i]].push_back(i);
		if(debug1)
		{
			printf("%d %d %d\n",u[i],v[i],c[i]);
		}
	}
	if(debug||debug1)
	{
		puts("-1");return 0;
	}
	dfs(1,0,0);
	build(1,cnt,1);
	solve(1,0);

	if(flag){puts("-1");return 0;}
	printf("%I64d\n",f[1]);
	return 0;
}


#include<vector>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define N 310000
int n,m;
int t1,t2,tot,cnt,flag;
int head[N],nex[N<<1],to[N<<1];
int u[N],v[N],c[N],pos[N],bel[N];
vector<int>beg[N],en[N];
int b[N],e[N];
ll f[N],val[N<<2],bj[N<<2];
void add(int x,int y)
{
	tot++;
	nex[tot]=head[x];head[x]=tot;
	to[tot]=y;
}
void dfs(int x,int y,int deep)
{
	b[x]=cnt+1;
	for(int i=0;i<beg[x].size();i++)
	pos[beg[x][i]]=++cnt,bel[cnt]=beg[x][i];
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)dfs(to[i],x,deep+1);
	e[x]=cnt;
}
void build(int l,int r,int now)
{
	if(l==r)
	{val[now]=c[bel[l]];return;}
	int mid=(l+r)>>1;
	build(l,mid,now<<1);
	build(mid+1,r,now<<1|1);
	val[now]=min(val[now<<1],val[now<<1|1]);
}
void pushdown(int x)
{
	if(bj[x])
	{
		bj[x<<1]+=bj[x];
		val[x<<1]+=bj[x];
		bj[x<<1|1]+=bj[x];
		val[x<<1|1]+=bj[x];
		bj[x]=0;
	}
}
void pushup(int x)
{val[x]=min(val[x<<1],val[x<<1|1]);}
void update(int l,int r,int now,int lq,int rq,ll v)
{
	if(lq<=l&&r<=rq)
	{val[now]+=v;bj[now]+=v;return;}
	int mid=(l+r)>>1;
	pushdown(now);
	if(mid>=lq)update(l,mid,now<<1,lq,rq,v);
	if(mid<rq) update(mid+1,r,now<<1|1,lq,rq,v);
	pushup(now);
}
ll query(int l,int r,int now,int lq,int rq)
{
	if(lq<=l&&r<=rq)return val[now];
	int mid=(l+r)>>1;
	ll ret=1ll<<62;
	if(mid>=lq)ret=min(ret,query(l,mid,now<<1,lq,rq));
	if(mid<rq) ret=min(ret,query(mid+1,r,now<<1|1,lq,rq));
	return ret;
}
void solve(int x,int y)
{
	ll sum=0;f[x]=1ll<<62;

	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)solve(to[i],x),sum+=f[to[i]];

	for(int i=0;i<beg[x].size();i++)
	update(1,cnt,1,pos[beg[x][i]],pos[beg[x][i]],sum);
	if(x!=1)
	{
		for(int i=0;i<en[x].size();i++)
		update(1,cnt,1,pos[en[x][i]],pos[en[x][i]],1ll<<62);
	}

	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y&&b[to[i]]&&e[to[i]])
	update(1,cnt,1,b[to[i]],e[to[i]],sum-f[to[i]]);

	if(b[x]&&e[x])f[x]=query(1,cnt,1,b[x],e[x]);
	if(f[x]>=(1ll<<62))flag=1;
}
int main()
{
	scanf("%d%d",&n,&m);
	if(n==1)
	{
		puts("0");
		return 0;
	}
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&t1,&t2);
		add(t1,t2);add(t2,t1);
	}
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&u[i],&v[i],&c[i]);
		beg[u[i]].push_back(i);
		en[v[i]].push_back(i);
	}
	dfs(1,0,0);
	build(1,cnt,1);
	solve(1,0);

	if(flag){puts("-1");return 0;}
	printf("%I64d\n",f[1]);
	return 0;
}


#include<vector>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define N 310000
int n,m;
int t1,t2,tot,cnt,flag;
int head[N],nex[N<<1],to[N<<1];
int u[N],v[N],c[N],pos[N],bel[N];
vector<int>beg[N],en[N];
int b[N],e[N];
ll f[N],val[N<<2],bj[N<<2];
void add(int x,int y)
{
	tot++;
	nex[tot]=head[x];head[x]=tot;
	to[tot]=y;
}
void dfs(int x,int y)
{
	b[x]=cnt+1;
	for(int i=0;i<beg[x].size();i++)
	pos[beg[x][i]]=++cnt,bel[cnt]=beg[x][i];
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)dfs(to[i],x);
	e[x]=cnt;
}
void build(int l,int r,int now)
{
	if(l==r)
	{val[now]=c[bel[l]];return;}
	int mid=(l+r)>>1;
	build(l,mid,now<<1);
	build(mid+1,r,now<<1|1);
	val[now]=min(val[now<<1],val[now<<1|1]);
}
void pushdown(int x)
{
	if(bj[x])
	{
		bj[x<<1]+=bj[x];
		val[x<<1]+=bj[x];
		bj[x<<1|1]+=bj[x];
		val[x<<1|1]+=bj[x];
		bj[x]=0;
	}
}
void pushup(int x)
{val[x]=min(val[x<<1],val[x<<1|1]);}
void update(int l,int r,int now,int lq,int rq,ll v)
{
	if(lq<=l&&r<=rq)
	{val[now]+=v;bj[now]+=v;return;}
	int mid=(l+r)>>1;
	pushdown(now);
	if(mid>=lq)update(l,mid,now<<1,lq,rq,v);
	if(mid<rq) update(mid+1,r,now<<1|1,lq,rq,v);
	pushup(now);
}
ll query(int l,int r,int now,int lq,int rq)
{
	if(lq<=l&&r<=rq)return val[now];
	int mid=(l+r)>>1;
	ll ret=1ll<<62;
	if(mid>=lq)ret=min(ret,query(l,mid,now<<1,lq,rq));
	if(mid<rq) ret=min(ret,query(mid+1,r,now<<1|1,lq,rq));
	return ret;
}
void solve(int x,int y)
{
	ll sum=0;f[x]=1ll<<62;
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)solve(to[i],x),sum+=f[to[i]];

	for(int i=0;i<beg[x].size();i++)
	update(1,cnt,1,pos[beg[x][i]],pos[beg[x][i]],sum);

	if(x!=1)
	{
		for(int i=0;i<en[x].size();i++)
		update(1,cnt,1,pos[en[x][i]],pos[en[x][i]],1ll<<62);
	}
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)
	update(1,cnt,1,b[to[i]],e[to[i]],sum-f[to[i]]);

	if(b[x]&&e[x])f[x]=query(1,cnt,1,b[x],e[x]);
	if(f[x]>=(1ll<<62))flag=1;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&t1,&t2);
		add(t1,t2);add(t2,t1);
	}
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&u[i],&v[i],&c[i]);
		beg[u[i]].push_back(i);
		en[v[i]].push_back(i);
	}
	dfs(1,0);
	build(1,cnt,1);
	solve(1,0);
	if(flag){puts("-1");return 0;}
	printf("%I64d\n",f[1]);
	return 0;
}


#include<vector>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define N 310000
int n,m;
int t1,t2,tot,cnt,flag;
int head[N],nex[N<<1],to[N<<1];
int u[N],v[N],c[N],pos[N],bel[N];
vector<int>beg[N],en[N];
int b[N],e[N];
ll f[N],val[N<<2],bj[N<<2];
void add(int x,int y)
{
	tot++;
	nex[tot]=head[x];head[x]=tot;
	to[tot]=y;
}
void dfs(int x,int y,int deep)
{
	b[x]=cnt+1;
	for(int i=0;i<beg[x].size();i++)
	pos[beg[x][i]]=++cnt,bel[cnt]=beg[x][i];
	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)dfs(to[i],x,deep+1);
	e[x]=cnt;
}
void build(int l,int r,int now)
{
	if(l==r)
	{val[now]=c[bel[l]];return;}
	int mid=(l+r)>>1;
	build(l,mid,now<<1);
	build(mid+1,r,now<<1|1);
	val[now]=min(val[now<<1],val[now<<1|1]);
}
void pushdown(int x)
{
	if(bj[x])
	{
		bj[x<<1]+=bj[x];
		val[x<<1]+=bj[x];
		bj[x<<1|1]+=bj[x];
		val[x<<1|1]+=bj[x];
		bj[x]=0;
	}
}
void pushup(int x)
{val[x]=min(val[x<<1],val[x<<1|1]);}
void update(int l,int r,int now,int lq,int rq,ll v)
{
	if(lq<=l&&r<=rq)
	{val[now]+=v;bj[now]+=v;return;}
	int mid=(l+r)>>1;
	pushdown(now);
	if(mid>=lq)update(l,mid,now<<1,lq,rq,v);
	if(mid<rq) update(mid+1,r,now<<1|1,lq,rq,v);
	pushup(now);
}
ll query(int l,int r,int now,int lq,int rq)
{
	if(lq<=l&&r<=rq)return val[now];
	int mid=(l+r)>>1;
	ll ret=1ll<<62;
	if(mid>=lq)ret=min(ret,query(l,mid,now<<1,lq,rq));
	if(mid<rq) ret=min(ret,query(mid+1,r,now<<1|1,lq,rq));
	return ret;
}
void solve(int x,int y)
{
	ll sum=0;f[x]=1ll<<62;

	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y)solve(to[i],x),sum+=f[to[i]];

	for(int i=0;i<beg[x].size();i++)
	update(1,cnt,1,pos[beg[x][i]],pos[beg[x][i]],sum);
	if(x!=1)
	{
		for(int i=0;i<en[x].size();i++)
		update(1,cnt,1,pos[en[x][i]],pos[en[x][i]],1ll<<62);
	}

	for(int i=head[x];i;i=nex[i])
	if(to[i]!=y&&b[to[i]]&&e[to[i]])
	update(1,cnt,1,b[to[i]],e[to[i]],sum-f[to[i]]);

	if(b[x]&&e[x])f[x]=query(1,cnt,1,b[x],e[x]);
	if(f[x]>=(1ll<<62))flag=1;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&t1,&t2);
		add(t1,t2);add(t2,t1);
	}
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&u[i],&v[i],&c[i]);
		beg[u[i]].push_back(i);
		en[v[i]].push_back(i);
	}
	dfs(1,0,0);
	build(1,cnt,1);
	solve(1,0);

	if(flag){puts("-1");return 0;}
	printf("%I64d\n",f[1]);
	return 0;
}


#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
#define re register
struct edge
{
	int to,w;edge*next;
}e[900100];
edge*cnt=e,*last[300100],*p[300100];
inline void add(re edge**la,re int a1,re int a2,re int w){*(++cnt)=(edge){a2,w,la[a1]};la[a1]=cnt;}
struct heap
{
	heap *l,*r;int de,len;
	long long f,mi,add;
	inline void pd(){if(add)f+=add,mi+=add,l->add+=add,r->add+=add,add=0;}
	inline void mar()
	{
		mi=min(r->mi+r->add,l->mi+l->add);mi=min(mi,f);
		if(l->len<r->len){re heap* tmp=l;l=r;r=tmp;}
		len=r->len+1;
	}
}h[300100];
heap*now=h,*root[300100];
int dep[300100];
heap* marge(re heap* x1,re heap* x2)
{
	if(!x1)return x2;
	if(!x2)return x1;
	x1->pd(),x2->pd();
	if(x1->de>x2->de){x1->r=marge(x1->r,x2);x1->mar();return x1;}
	else{x2->r=marge(x1,x2->r);x2->mar();return x2;}
}
void init(re int a,re int ff)
{
	re int x;
	for(re edge*i=last[a];i!=e;i=i->next)if((x=i->to)!=ff)
	dep[x]=dep[a]+1,init(x,a);
}
void dfs(re int a,re int ff)
{
	re int x,x1=dep[a];
	re long long y1=0;
	for(re edge*i=last[a];i!=e;i=i->next)if((x=i->to)!=ff)
	{
		dfs(x,a);
		while(root[x]->de>x1)root[x]=marge(root[x]->l,root[x]->r);
		if(root[x])puts("-1"),exit(0);
		y1+=root[x]->mi;
	}
	for(re edge*i=p[a];i!=e;i=i->next)
	{
		*(++now)=(heap){h,h,i->to,1,(y1+(i->w)),(y1+(i->w)),0};
		root[a]=marge(now,root[a]);
	}
	for(re edge*i=last[a];i!=e;i=i->next)if((x=i->to)!=ff)
	{
		(root[x]->add)+=y1-(root[x]->mi);
		root[a]=marge(root[a],root[x]);
	}
	if(root[a])puts("-1"),exit(0);
}
int main()
{
	re int n,m,x,y,z;h->mi=1ll<<60;
	scanf("%d%d",&n,&m);
	for(re int i=1;i<=n;i++)last[i]=p[i]=e;
	for(re int i=1;i<n;i++)
	{
		scanf("%d%d",&x,&y);
		add(last,x,y,0),add(last,y,x,0);
	}dep[1]=1;
	init(1,0);
	add(p,1,dep[1],0);
	for(re int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&x,&y,&z);
		add(p,x,dep[y],z);
	}
	dfs(1,0);
	printf("%lld",(root[1]->mi)+(root[1]->add));
}


#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
#define cmin(a,b) (a>(b)?a=(b):0)
#define dmin(a,b) ((a)<(b)?(a):(b))
#define re register
struct edge
{
	int to,w;edge*next;
}e[900100];
edge*cnt=e,*last[300100],*p[300100];
int r()
{
	re int x;re char c;
	for(c=getchar();c<'0'||c>'9';c=getchar());x=c^'0';
	for(c=getchar();c>='0'&&c<='9';c=getchar())x=(x*10)+(c^'0');
	return x;
}
inline void add(re edge**la,re int a1,re int a2,re int w){*(++cnt)=(edge){a2,w,la[a1]};la[a1]=cnt;}
struct heap
{
	heap *l,*r;int de,len;
	long long f,mi,add;
	inline void pd(){if(add)f+=add,mi+=add,l->add+=add,r->add+=add,add=0;}
	inline void mar()
	{
		mi=dmin(r->mi+r->add,l->mi+l->add);cmin(mi,f);
		if(l->len<r->len){re heap* tmp=l;l=r;r=tmp;}
		len=r->len+1;
	}
}h[300100];
heap*now=h,*root[300100];
int dep[300100];
heap* marge(re heap* x1,re heap* x2)
{
	if(x1==h)return x2;
	if(x2==h)return x1;
	x1->pd(),x2->pd();
	if(x1->de>x2->de){x1->r=marge(x1->r,x2);x1->mar();return x1;}
	else{x2->r=marge(x1,x2->r);x2->mar();return x2;}
}
void init(re int a,re int ff)
{
	re int x;
	for(re edge*i=last[a];i!=e;i=i->next)if((x=i->to)!=ff)
	dep[x]=dep[a]+1,init(x,a);
}
void dfs(re int a,re int ff)
{
	re int x,x1=dep[a];
	re long long y1=0;
	for(re edge*i=last[a];i!=e;i=i->next)if((x=i->to)!=ff)
	{
		dfs(x,a);
		while(root[x]->de>x1)root[x]=marge(root[x]->l,root[x]->r);
		if(root[x]==h)puts("-1"),exit(0);
		y1+=root[x]->mi;
	}
	for(re edge*i=p[a];i!=e;i=i->next)
	{
		*(++now)=(heap){h,h,i->to,1,(y1+(i->w)),(y1+(i->w)),0};
		root[a]=marge(now,root[a]);
	}
	for(re edge*i=last[a];i!=e;i=i->next)if((x=i->to)!=ff)
	{
		(root[x]->add)+=y1-(root[x]->mi);
		root[a]=marge(root[a],root[x]);
	}
	if(root[a]==h)puts("-1"),exit(0);
}
int main()
{
	re int n=r(),m=r(),x,y,z;h->mi=1ll<<60;
	for(re int i=1;i<=n;i++)last[i]=p[i]=e,root[i]=h;
	for(re int i=1;i<n;i++)
	{
		x=r(),y=r();
		add(last,x,y,0),add(last,y,x,0);
	}dep[1]=1;
	init(1,0);
	for(re int i=1;i<=m;i++)
	{
		x=r(),y=r(),z=r();
		add(p,x,dep[y],z);
	}
	dfs(1,0);
	printf("%lld",(root[1]->mi)+(root[1]->add));
}


#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
#define re register
struct edge
{
	int to,w;edge*next;
}e[900100];
edge*cnt=e,*last[300100],*p[300100];
inline void add(re edge**la,re int a1,re int a2,re int w){*(++cnt)=(edge){a2,w,la[a1]};la[a1]=cnt;}
struct heap
{
	heap *l,*r;int de,len;
	long long f,mi,add;
	inline void pd(){if(add)f+=add,mi+=add,(l?l->add+=add:0),(r?r->add+=add:0),add=0;}
	inline void mar()
	{
		mi=min((r?r->mi+r->add:1ll<<60),(l?l->mi+l->add:1ll<<60));mi=min(mi,f);
		if(r&&(!l||l->len<r->len)){re heap* tmp=l;l=r;r=tmp;}
		len=(r?r->len+1:1);
	}
}h[300100];
heap*now=h,*root[300100];
int dep[300100];
heap* marge(re heap* x1,re heap* x2)
{
	if(!x1)return x2;
	if(!x2)return x1;
	x1->pd(),x2->pd();
	if(x1->de>x2->de){x1->r=marge(x1->r,x2);x1->mar();return x1;}
	else{x2->r=marge(x1,x2->r);x2->mar();return x2;}
}
void init(re int a,re int ff)
{
	re int x;
	for(re edge*i=last[a];i!=e;i=i->next)if((x=i->to)!=ff)
	dep[x]=dep[a]+1,init(x,a);
}
void dfs(re int a,re int ff)
{
	re int x,x1=dep[a];
	re long long y1=0;
	for(re edge*i=last[a];i!=e;i=i->next)if((x=i->to)!=ff)
	{
		dfs(x,a);
		while(root[x]&&root[x]->de>x1)root[x]=marge(root[x]->l,root[x]->r);
		if(!root[x])puts("-1"),exit(0);
		y1+=root[x]->mi;
	}
	for(re edge*i=p[a];i!=e;i=i->next)
	{
		*(++now)=(heap){0,0,i->to,1,(y1+(i->w)),(y1+(i->w)),0};
		root[a]=marge(now,root[a]);
	}
	for(re edge*i=last[a];i!=e;i=i->next)if((x=i->to)!=ff)
	{
		(root[x]->add)+=y1-(root[x]->mi);
		root[a]=marge(root[a],root[x]);
	}
	if(!root[a])puts("-1"),exit(0);
}
int main()
{
	re int n,m,x,y,z;h->mi=1ll<<60;
	scanf("%d%d",&n,&m);
	for(re int i=1;i<=n;i++)last[i]=p[i]=e;
	for(re int i=1;i<n;i++)
	{
		scanf("%d%d",&x,&y);
		add(last,x,y,0),add(last,y,x,0);
	}dep[1]=1;
	init(1,0);
	add(p,1,dep[1],0);
	for(re int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&x,&y,&z);
		add(p,x,dep[y],z);
	}
	dfs(1,0);
	printf("%lld",(root[1]->mi)+(root[1]->add));
}


#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
#define cmin(a,b) (a>(b)?a=(b):0)
#define dmin(a,b) ((a)<(b)?(a):(b))
#define re register
struct edge
{
	int to,w;edge*next;
}e[900100];
edge*cnt=e,*last[300100],*p[300100];
int r()
{
	re int x;re char c;
	for(c=getchar();c<'0'||c>'9';c=getchar());x=c^'0';
	for(c=getchar();c>='0'&&c<='9';c=getchar())x=(x*10)+(c^'0');
	return x;
}
inline void add(re edge**la,re int a1,re int a2,re int w){*(++cnt)=(edge){a2,w,la[a1]};la[a1]=cnt;}
struct heap
{
	heap *l,*r;int de,len;
	long long f,mi,add;
	inline void pd(){if(add)f+=add,mi+=add,l->add+=add,r->add+=add,add=0;}
	inline void mar()
	{
		mi=dmin(r->mi+r->add,l->mi+l->add);cmin(mi,f);
		if(l->len<r->len){re heap* tmp=l;l=r;r=tmp;}
		len=r->len+1;
	}
}h[300100];
heap*now=h,*root[300100];
int dep[300100];
heap* marge(re heap* x1,re heap* x2)
{
	if(x1==h)return x2;
	if(x2==h)return x1;
	x1->pd(),x2->pd();
	if(x1->de>x2->de){x1->r=marge(x1->r,x2);x1->mar();return x1;}
	else{x2->r=marge(x1,x2->r);x2->mar();return x2;}
}
void init(re int a,re int ff)
{
	re int x;
	for(re edge*i=last[a];i!=e;i=i->next)if((x=i->to)!=ff)
	dep[x]=dep[a]+1,init(x,a);
}
void dfs(re int a,re int ff)
{
	re int x,x1=dep[a];
	re long long y1=0;
	for(re edge*i=last[a];i!=e;i=i->next)if((x=i->to)!=ff)
	{
		dfs(x,a);
		while(root[x]->de>x1)root[x]=marge(root[x]->l,root[x]->r);
		if(root[x]==h)puts("-1"),exit(0);
		y1+=root[x]->mi;
	}
	for(re edge*i=p[a];i!=e;i=i->next)
	{
		*(++now)=(heap){h,h,i->to,1,(y1+(i->w)),(y1+(i->w)),0};
		root[a]=marge(now,root[a]);
	}
	for(re edge*i=last[a];i!=e;i=i->next)if((x=i->to)!=ff)
	{
		(root[x]->add)+=y1-(root[x]->mi);
		root[a]=marge(root[a],root[x]);
	}
	if(root[a]==h)puts("-1"),exit(0);
}
int main()
{
	re int n=r(),m=r(),x,y,z;h->mi=1ll<<60;
	for(re int i=1;i<=n;i++)last[i]=p[i]=e,root[i]=h;
	for(re int i=1;i<n;i++)
	{
		x=r(),y=r();
		add(last,x,y,0),add(last,y,x,0);
	}dep[1]=1;
	init(1,0);
	add(p,1,dep[1],0);
	for(re int i=1;i<=m;i++)
	{
		x=r(),y=r(),z=r();
		add(p,x,dep[y],z);
	}
	dfs(1,0);
	printf("%lld",(root[1]->mi)+(root[1]->add));
}


#include <bits/stdc++.h>

// In Need Of GOD
// ASHKAN

using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define err(q) cerr << #q << " = " << q;

const int N = 3e5 + 50;
ll INF = 1e17;

int n, m, sz[N], h[N];
ll dp[N], lazy[N], MN[N];
vector <int> adj[N];
vector < pair <int, int> > st[N];
multiset <ll> s[N];
multiset <pair <ll, ll> > ss[N];

void DFS(int v, int p){
	MN[v] = INF;
	h[v] = h[p] + 1, sz[v] = 1;
	ll sum = 0, mx = 0, amx = 0, Mn = INF;
	for(auto u : adj[v]){
		if(u == p) continue;
		DFS(u, v);
		sz[v] += sz[u];
		if(mx < sz[u]){
			mx = sz[u];
			amx = u;
		}
		if(!s[u].empty()) MN[u] = *s[u].begin() + lazy[u];
		else{
			cout << -1 << endl;
			exit(0);
		}
		dp[v] += MN[u];
		sum += MN[u];
	}
	s[v].swap(s[amx]), ss[v].swap(ss[amx]);
	lazy[v] = lazy[amx] + sum - MN[amx];
	for(auto u : adj[v]){
		if(u == p || u == amx) continue;
		for(auto uu : s[u]) s[v].insert(uu + lazy[u] + sum - MN[u] - lazy[v]);
		for(auto uu : ss[u]) ss[v].insert(mp(uu.F, uu.S + lazy[u] + sum - MN[u] - lazy[v]));
	}
	for(auto u : st[v]){
		s[v].insert(dp[v] + u.S - lazy[v]);
		ss[v].insert(mp(INF - h[u.F], u.S + dp[v] - lazy[v]));
		Mn = min(Mn, 1ll * u.S);
	}
	bool flag = true;
	while(!ss[v].empty() && flag){
		auto x = *ss[v].begin();
		if(x.F > INF - h[v]) flag = false;
		if(flag) s[v].erase(s[v].find(x.S)), ss[v].erase(ss[v].find(x));
	}
	return;
}

int32_t main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> n >> m;
	for(int i = 1, u, v; i < n; i++){
		cin >> u >> v;
		adj[u].pb(v), adj[v].pb(u);
	}
	for(int i = 0, u, v, z; i < m; i++){
		cin >> u >> v >> z;
		st[u].pb(mp(v, z));
	}

	DFS(1 ,0);
	if(dp[1] >= INF) dp[1] = -1;
	cout << dp[1] << endl;
	return 0;
}


#include <bits/stdc++.h>

// In Need Of GOD
// ASHKAN

using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define err(q) cerr << #q << " = " << q;
#define int long long

const int N = 3e5 + 50;
ll INF = 1e16;

int n, m, sz[N], h[N];
ll dp[N], lazy[N];
vector <int> adj[N];
vector < pair <int, int> > st[N];
multiset <ll> s[N];
multiset <pair <ll, ll> > ss[N];

void DFS(int v, int p){
	h[v] = h[p] + 1;
	sz[v] = 1;
	ll sum = 0, mx = 0, amx = 0, Mn = INF;
	for(auto u : adj[v]){
		if(u == p) continue;
		DFS(u, v);
		sz[v] += sz[u];
		sum += dp[u];
		if(mx < sz[u]){
			mx = sz[u];
			amx = u;
		}
		if(s[u].empty()) dp[v] += INF;
		else dp[v] += *s[u].begin() + lazy[u];
	}
	s[v].swap(s[amx]);
	lazy[v] = lazy[amx];
	ss[v].swap(ss[amx]);
	lazy[v] += sum - dp[amx];
	for(auto u : adj[v]){
		if(u == p || u == amx) continue;
		for(auto uu : s[u]) s[v].insert(uu + lazy[u] + sum - dp[u] - lazy[v]);
		for(auto uu : ss[u]) ss[v].insert(mp(uu.F, uu.S + lazy[u] + sum - dp[u] - lazy[v]));
	}
	for(auto u : st[v]){
		s[v].insert(u.S + dp[v] - lazy[v]);
		ss[v].insert(mp(INF - h[u.F], u.S + dp[v] - lazy[v]));
		Mn = min(Mn, 1ll * u.S);
	}
	bool flag = true;
	while(!ss[v].empty() && flag){
		auto x = *ss[v].begin();
		if(x.F != INF - h[v]) flag = false;
		if(flag) s[v].erase(s[v].find(x.S)), ss[v].erase(ss[v].find(x));
	}
	if(v != 1 && (int)adj[v].size() == 1) dp[v] += Mn;
	return;
}

int32_t main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> n >> m;
	for(int i = 1, u, v; i < n; i++){
		cin >> u >> v;
		adj[u].pb(v), adj[v].pb(u);
	}
	for(int i = 0, u, v, z; i < m; i++){
		cin >> u >> v >> z;
		st[u].pb(mp(v, z));
	}

	DFS(1 ,0);
	if(dp[1] >= INF) dp[1] = -1;
	cout << dp[1] << endl;
	return 0;
}


#include <bits/stdc++.h>

// In Need Of GOD
// ASHKAN

using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define err(q) cerr << #q << " = " << q;

const int N = 3e5 + 50;
ll INF = 1e17;

int n, m, sz[N], h[N], MN[N];
ll dp[N], lazy[N];
vector <int> adj[N];
vector < pair <int, int> > st[N];
multiset <ll> s[N];
multiset <pair <ll, ll> > ss[N];

void DFS(int v, int p){
	MN[v] = INF;
	h[v] = h[p] + 1, sz[v] = 1;
	ll sum = 0, mx = 0, amx = 0, Mn = INF;
	for(auto u : adj[v]){
		if(u == p) continue;
		DFS(u, v);
		sz[v] += sz[u];
		if(mx < sz[u]){
			mx = sz[u];
			amx = u;
		}
		if(!s[u].empty()) MN[u] = *s[u].begin() + lazy[u];
		else{
			cout << -1 << endl;
			exit(0);
		}
		dp[v] += MN[u];
		sum += MN[u];
	}
	s[v].swap(s[amx]), ss[v].swap(ss[amx]);
	lazy[v] = lazy[amx] + sum - MN[amx];
	for(auto u : adj[v]){
		if(u == p || u == amx) continue;
		for(auto uu : s[u]) s[v].insert(uu + lazy[u] + sum - MN[u] - lazy[v]);
		for(auto uu : ss[u]) ss[v].insert(mp(uu.F, uu.S + lazy[u] + sum - MN[u] - lazy[v]));
	}
	for(auto u : st[v]){
		s[v].insert(dp[v] + u.S - lazy[v]);
		ss[v].insert(mp(INF - h[u.F], u.S + dp[v] - lazy[v]));
		Mn = min(Mn, 1ll * u.S);
	}
	bool flag = true;
	while(!ss[v].empty() && flag){
		auto x = *ss[v].begin();
		if(x.F > INF - h[v]) flag = false;
		if(flag) s[v].erase(s[v].find(x.S)), ss[v].erase(ss[v].find(x));
	}
	return;
}

int32_t main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> n >> m;
	for(int i = 1, u, v; i < n; i++){
		cin >> u >> v;
		adj[u].pb(v), adj[v].pb(u);
	}
	for(int i = 0, u, v, z; i < m; i++){
		cin >> u >> v >> z;
		st[u].pb(mp(v, z));
	}

	DFS(1 ,0);
	if(dp[1] >= INF) dp[1] = -1;
	cout << dp[1] << endl;
	return 0;
}


#include <bits/stdc++.h>

// In Need Of GOD
// ASHKAN

using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define err(q) cerr << #q << " = " << q;

const int N = 3e5 + 50;
ll INF = 1e17;

int n, m, sz[N], h[N], MN[N];
ll dp[N], lazy[N];
vector <int> adj[N];
vector < pair <int, int> > st[N];
multiset <ll> s[N];
multiset <pair <ll, ll> > ss[N];

void DFS(int v, int p){
	MN[v] = INF;
	h[v] = h[p] + 1, sz[v] = 1;
	ll sum = 0, mx = 0, amx = 0, Mn = INF;
	for(auto u : adj[v]){
		if(u == p) continue;
		DFS(u, v);
		sz[v] += sz[u];
		if(mx < sz[u]){
			mx = sz[u];
			amx = u;
		}
		if(!s[u].empty()) MN[u] = *s[u].begin() + lazy[u];
		dp[v] += MN[u];
		sum += MN[u];
	}
	s[v].swap(s[amx]), ss[v].swap(ss[amx]);
	lazy[v] = lazy[amx] + sum - MN[amx];
	for(auto u : adj[v]){
		if(u == p || u == amx) continue;
		for(auto uu : s[u]) s[v].insert(uu + lazy[u] + sum - MN[u] - lazy[v]);
		for(auto uu : ss[u]) ss[v].insert(mp(uu.F, uu.S + lazy[u] + sum - MN[u] - lazy[v]));
	}
	for(auto u : st[v]){
		s[v].insert(dp[v] + u.S - lazy[v]);
		ss[v].insert(mp(INF - h[u.F], u.S + dp[v] - lazy[v]));
		Mn = min(Mn, 1ll * u.S);
	}
	bool flag = true;
	while(!ss[v].empty() && flag){
		auto x = *ss[v].begin();
		if(x.F > INF - h[v]) flag = false;
		if(flag) s[v].erase(s[v].find(x.S)), ss[v].erase(ss[v].find(x));
	}
	return;
}

int32_t main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> n >> m;
	for(int i = 1, u, v; i < n; i++){
		cin >> u >> v;
		adj[u].pb(v), adj[v].pb(u);
	}
	for(int i = 0, u, v, z; i < m; i++){
		cin >> u >> v >> z;
		st[u].pb(mp(v, z));
	}

	DFS(1 ,0);
	if(dp[1] >= INF) dp[1] = -1;
	cout << dp[1] << endl;
	return 0;
}


#include <bits/stdc++.h>

// In Need Of GOD
// ASHKAN

using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define err(q) cerr << #q << " = " << q;

const int N = 3e5 + 50;
ll INF = 1e16;

int n, m, sz[N], h[N];
ll dp[N], lazy[N];
vector <int> adj[N];
vector < pair <int, int> > st[N];
multiset <ll> s[N];
multiset <pair <ll, ll> > ss[N];

void DFS(int v, int p){
	h[v] = h[p] + 1;
	sz[v] = 1;
	ll sum = 0, mx = 0, amx = 0, Mn = INF;
	for(auto u : adj[v]){
		if(u == p) continue;
		DFS(u, v);
		sz[v] += sz[u];
		sum += dp[u];
		if(mx < sz[u]){
			mx = sz[u];
			amx = u;
		}
		if(s[u].empty()) dp[v] += INF;
		else dp[v] += *s[u].begin() + lazy[u];
	}
	s[v].swap(s[amx]);
	lazy[v] = lazy[amx];
	ss[v].swap(ss[amx]);
	lazy[v] += sum - dp[amx];
	for(auto u : adj[v]){
		if(u == p || u == amx) continue;
		for(auto uu : s[u]) s[v].insert(uu + lazy[u] + sum - dp[u] - lazy[v]);
		for(auto uu : ss[u]) ss[v].insert(mp(uu.F, uu.S + lazy[u] + sum - dp[u] - lazy[v]));
	}
	for(auto u : st[v]){
		s[v].insert(u.S + dp[v] - lazy[v]);
		ss[v].insert(mp(INF - h[u.F], u.S + dp[v] - lazy[v]));
		Mn = min(Mn, 1ll * u.S);
	}
	bool flag = true;
	while(!ss[v].empty() && flag){
		auto x = *ss[v].begin();
		if(x.F != INF - h[v]) flag = false;
		if(flag) s[v].erase(s[v].find(x.S)), ss[v].erase(ss[v].find(x));
	}
	if(v != 1 && (int)adj[v].size() == 1) dp[v] += Mn;
	return;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> n >> m;
	for(int i = 1, u, v; i < n; i++){
		cin >> u >> v;
		adj[u].pb(v), adj[v].pb(u);
	}
	for(int i = 0, u, v, z; i < m; i++){
		cin >> u >> v >> z;
		st[u].pb(mp(v, z));
	}

	DFS(1 ,0);
	if(dp[1] >= INF) dp[1] = -1;
	cout << dp[1] << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 3e5 + 11;
const LL inf = 1e16;
int n, m, tot;
int head[N], to[N<<1], nex[N<<1], size;
int id[N], L[N], R[N];
LL c[N], f[N];
vector<int> st[N], ed[N];
struct seg{
#define ls (p << 1)
#define rs (p << 1 | 1)
	LL t[N<<2], tag[N<<2];
	void add_tag(int p, LL c){
		tag[p] += c;
		t[p] += c;
	}
	void push_down(int p){
		if(!tag[p])return ;
		add_tag(ls, tag[p]);
		add_tag(rs, tag[p]);
		tag[p] = 0;
 	}
	void build(int p, int l, int r){
		if(l == r){
			t[p] = 1e16;
			return ;
		}
		int mid = l + r >> 1;
		build(ls, l, mid); build(rs, mid + 1, r);
		t[p] = min(t[ls], t[rs]);
	}
	void modify(int p, int l, int r, int x, LL c){
		if(l > x || r < x)return ;
		if(l == r && l == x){
			t[p] = c;
			return ;
		}
		push_down(p);
		int mid = l + r >> 1;
		modify(ls, l, mid, x, c);
		modify(rs, mid + 1, r, x, c);
		t[p] = min(t[ls], t[rs]);
	}
	void add(int p, int l, int r, int x, int y, LL c){
		if(l > y || r < x)return ;
		if(l >= x && r <= y){
			add_tag(p, c);
			return ;
		}
		push_down(p);
		int mid = l + r >> 1;
		add(ls, l, mid, x, y, c);
		add(rs, mid + 1, r, x, y, c);
		t[p] = min(t[ls], t[rs]);
	}
	LL query(int p, int l, int r, int x, int y){
		if(l > y || r < x)return inf;
		if(l >= x && r <= y){
			return t[p];
		}
		push_down(p);
		int mid = l + r >> 1;
		return min(query(ls, l, mid, x, y), query(rs, mid + 1, r, x, y));
	}
#undef ls
#undef rs
}T;
void dfs(int u, int fa){
	L[u] = m + 1;
	for(int i = 0;i < st[u].size(); i++){
		id[st[u][i]] = ++tot;
		L[u] = min(L[u], tot);
		R[u] = max(R[u], tot);
	}
	for(int i = head[u];i;i = nex[i]){
		int v = to[i];
		if(v == fa)continue;
		dfs(v, u);
		L[u] = min(L[u], L[v]);
		R[u] = max(R[u], R[v]);
	}
	if(L[u] == m + 1 || !R[u]){
		puts("-1");
		exit(0);
	}
}
void dp(int u, int fa){
	LL sum = 0;
	for(int i = head[u];i;i = nex[i]){
		int v = to[i];
		if(v == fa)continue;
		dp(v, u);
		sum += f[v];
	}
	for(int i = 0;i < st[u].size(); i++){
		int now = st[u][i];
		T.modify(1, 1, m, id[st[u][i]], c[st[u][i]]);
	}
	T.add(1, 1, m, L[u], R[u], sum);
	for(int i = 0;i < ed[u].size(); i++){
		int now = ed[u][i];
		T.modify(1, 1, m, id[now], 1e16);
	}
	if(u != 1)f[u] = T.query(1, 1, m, L[u], R[u]);
	else f[u] = sum;
	T.add(1, 1, m, L[u], R[u], -f[u]);
}
void add(int u, int v){
	to[++size] = v;
	nex[size] = head[u];
	head[u] = size;
}
int main(){
	cin>>n>>m;
	int u, v;
	for(int i = 1;i < n; i++){
		scanf("%d%d", &u, &v);
		add(u, v); add(v, u);
	}
	for(int i = 1;i <= m; i++){
		scanf("%d%d%lld", &u, &v, &c[i]);
		if(u == v){
			i--, m--;
			continue;
		}
		st[u].push_back(i);
		ed[v].push_back(i);
	}
	dfs(1, 1);
	T.build(1, 1, m);
	dp(1, 1);
	if(f[1] < inf)printf("%lld\n", f[1]);
	else puts("-1");
	return 0;
}