#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e6 + 10;
const long long INF = 0x7fffffff;
const long long mod = 1e9 + 7;
const long long hash_num = 131;
const double eps = 1e-6;
const double PI = acos(-1.0);
inline long long read() {
  long long s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
long long qpow(long long p, long long q) {
  return (q & 1 ? p : 1) * (q ? qpow(p * p % mod, q / 2) : 1) % mod;
}
long long cnt0[maxn], cnt1[maxn], ans = 0, a[maxn], b[maxn], c[maxn], u, v, n,
                                  i;
vector<long long> G[maxn];
void dfs(long long u, long long father, long long minn) {
  minn = min(minn, a[u]);
  for (auto v : G[u]) {
    if (v == father) continue;
    dfs(v, u, minn);
    cnt0[u] += cnt0[v];
    cnt1[u] += cnt1[v];
  }
  if (b[u] != c[u]) {
    if (b[u] == 1)
      cnt1[u]++;
    else
      cnt0[u]++;
  }
  if (minn == a[u]) {
    long long s = min(cnt0[u], cnt1[u]);
    ans += s * 2 * a[u];
    cnt1[u] -= s;
    cnt0[u] -= s;
  }
}
signed main() {
  n = read();
  for (i = 1; i <= n; i++) {
    a[i] = read();
    b[i] = read();
    c[i] = read();
  }
  for (i = 1; i < n; i++) {
    u = read(), v = read();
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(1, 1, INF);
  if (cnt0[1] || cnt1[1])
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
