#include <bits/stdc++.h>
using namespace std;
long long n, a[200010], b[200010], c[200010];
vector<long long> G[200010];
long long sum = 0;
pair<long long, long long> dfs(long long nowp, long long p, long long ans) {
  long long nowa, nowb;
  nowa = nowb = 0;
  if (b[nowp] != c[nowp]) {
    if (b[nowp])
      nowa += b[nowp];
    else
      nowb += c[nowp];
  }
  for (unsigned long long i = 0; i < G[nowp].size(); i++) {
    if (G[nowp][i] == p) continue;
    pair<long long, long long> ret = dfs(G[nowp][i], nowp, min(ans, a[nowp]));
    nowa += ret.first;
    nowb += ret.second;
  }
  if (a[nowp] < ans) {
    sum += 2 * a[nowp] * min(nowa, nowb);
    if (nowa <= nowb) {
      nowb -= nowa;
      nowa = 0;
    } else {
      nowa -= nowb;
      nowb = 0;
    }
  }
  return make_pair(nowa, nowb);
}
signed main() {
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++) scanf("%lld%lld%lld", &a[i], &b[i], &c[i]);
  for (long long i = 1; i < n; i++) {
    long long u, v;
    scanf("%lld%lld", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  pair<long long, long long> pp = dfs(1ll, 0, 2000000000);
  if (!pp.first && !pp.second)
    printf("%lld\n", sum);
  else
    puts("-1");
}
