#include <bits/stdc++.h>
using namespace std;
long long a[201007];
long long b[201007];
long long c[201007];
long long cost = 0;
vector<long long> adj[201007];
pair<long long, long long> dfs(long long v, long long par, long long mx) {
  pair<long long, long long> p;
  if (b[v] != c[v]) {
    if (b[v]) {
      p.first++;
    } else {
      p.second++;
    }
  }
  for (long long i = 0; i < adj[v].size(); i++) {
    long long x = adj[v][i];
    if (x == par) continue;
    pair<long long, long long> p1;
    p1 = dfs(x, v, min(mx, a[v]));
    p.first += p1.first;
    p.second += p1.second;
  }
  if (a[v] < mx) {
    long long take = min(p.first, p.second);
    p.first -= take;
    p.second -= take;
    cost += take * 2 * a[v];
  }
  return p;
}
int main() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  for (long long i = 0; i < n - 1; i++) {
    long long u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  pair<long long, long long> p1;
  p1 = dfs(1, 0, 2 * 1e9);
  if (p1.first || p1.second) {
    cout << -1 << endl;
  } else {
    cout << cost << endl;
  }
}
