#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5, big = 1e18;
long long mi[maxn], a[maxn];
vector<long long> g[maxn];
long long tp[maxn];
long long ans = 0;
void prec(long long v, long long val, long long p) {
  val = min(val, a[v]);
  mi[v] = val;
  for (auto u : g[v]) {
    if (u == p) continue;
    prec(u, val, v);
  }
}
pair<long long, long long> dfs(long long v, long long p) {
  pair<long long, long long> cnt = {0, 0};
  if (tp[v] == 0)
    ++cnt.first;
  else if (tp[v] == 1)
    ++cnt.second;
  for (auto u : g[v]) {
    if (u == p) continue;
    auto res = dfs(u, v);
    cnt.first += res.first;
    cnt.second += res.second;
  }
  if (mi[v] == a[v]) {
    ans += 2 * min(cnt.first, cnt.second) * a[v];
    if (cnt.second < cnt.first) {
      cnt.first -= cnt.second;
      cnt.second = 0;
    } else {
      cnt.second -= cnt.first;
      cnt.first = 0;
    }
  }
  return cnt;
}
void solve() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
    long long s, f;
    cin >> s >> f;
    if (s == f)
      tp[i] = -1;
    else
      tp[i] = s;
  }
  for (long long i = 0; i < n - 1; ++i) {
    long long v, u;
    cin >> v >> u;
    --v;
    --u;
    g[v].push_back(u);
    g[u].push_back(v);
  }
  prec(0, big, -1);
  if (dfs(0, -1) != make_pair(0ll, 0ll))
    cout << -1;
  else
    cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
