#include <bits/stdc++.h>
using namespace std;
long long POW(long long A, long long N, long long MOD) {
  if (N == 0) return 1 % MOD;
  if (N == 1) return A % MOD;
  long long tmp = POW(A, N / 2, MOD) % MOD;
  if (N % 2)
    return ((tmp * tmp) % MOD * A % MOD) % MOD;
  else
    return (tmp * tmp) % MOD;
}
long long MUL(long long A, long long B, long long MOD) {
  long long r = 0;
  while (B) {
    r = (r + (A * (B % 2) % MOD)) % MOD;
    A = (A + A) % MOD;
    B /= 2;
  }
  return r;
}
long long GCD(long long A, long long B) { return B == 0 ? A : GCD(B, A % B); }
int dx[] = {0, -1, 0, 1, 0};
int dy[] = {0, 0, 1, 0, -1};
template <class X, class Y>
bool minimize(X &x, const Y &y) {
  X eps = 1e-9;
  if (x > y + eps) {
    x = y;
    return true;
  } else
    return false;
}
template <class X, class Y>
bool maximize(X &x, const Y &y) {
  X eps = 1e-9;
  if (x + eps < y) {
    x = y;
    return true;
  } else
    return false;
}
template <class T>
T Abs(const T &x) {
  return (x < 0 ? -x : x);
}
const int MAXN = 2 * 1e5 + 100;
int N;
int A[MAXN], B[MAXN], C[MAXN];
vector<int> G[MAXN];
int cnt0[MAXN], cnt1[MAXN];
long long ANS = 0;
void DFS(int u, int par, int k) {
  if (B[u] != C[u]) {
    if (B[u] == 0)
      cnt0[u]++;
    else
      cnt1[u]++;
  }
  for (auto &v : G[u]) {
    if (v == par) continue;
    DFS(v, u, min(k, A[v]));
    cnt0[u] += cnt0[v];
    cnt1[u] += cnt1[v];
  }
  int tmp = min(cnt0[u], cnt1[u]);
  cnt0[u] -= tmp;
  cnt1[u] -= tmp;
  ANS += 2ll * tmp * k;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> N;
  for (int i = (1), _b = (N); i <= _b; ++i) cin >> A[i] >> B[i] >> C[i];
  for (int i = (1), _b = (N - 1); i <= _b; ++i) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  DFS(1, 0, A[1]);
  if (cnt0[1] + cnt1[1] == 0)
    cout << ANS;
  else
    cout << -1;
  return 0;
}
