#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 2e5 + 5;
vector<long long> adj[MAXN];
long long a[MAXN], cnt0[MAXN], cnt1[MAXN];
bool t0[MAXN], t1[MAXN];
bool b[MAXN], c[MAXN];
long long n;
void input();
void dfs(long long, long long);
void solve();
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  input();
  solve();
  return 0;
}
void input() {
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    t1[i] = (b[i] == 0 && c[i] == 1);
    t0[i] = (b[i] == 1 && c[i] == 0);
  }
  for (long long i = 1; i < n; i++) {
    long long u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
}
void dfs(long long u, long long par) {
  for (auto v : adj[u]) {
    if (v == par) continue;
    a[v] = min(a[v], a[u]);
    dfs(v, u);
    cnt0[u] += max(0ll, cnt0[v] - cnt1[v]);
    cnt1[u] += max(0ll, cnt1[v] - cnt0[v]);
  }
  cnt0[u] += t0[u];
  cnt1[u] += t1[u];
}
void solve() {
  dfs(0, -1);
  if (cnt0[0] != cnt1[0]) {
    cout << -1;
    return;
  }
  long long ans = 0;
  for (long long i = 0; i < n; i++) ans += 2 * min(cnt0[i], cnt1[i]) * a[i];
  cout << ans << endl;
}
