#include <bits/stdc++.h>
using namespace std;
long long n;
vector<vector<long long> > adj(200005);
long long vis[200005];
long long ar[200005], val[2][200005], b[200005], c[200005];
long long ans;
void dfs(long long root, long long par) {
  if (vis[root]) return;
  ar[root] = min(ar[root], ar[par]);
  vis[root] = 1;
  if (b[root] != c[root]) val[b[root]][root]++;
  for (auto ch : adj[root]) {
    if (vis[ch]) continue;
    dfs(ch, root);
    val[0][root] += val[0][ch];
    val[1][root] += val[1][ch];
  }
  long long mi = min(val[0][root], val[1][root]);
  ans += (mi + mi) * ar[root];
  val[0][root] -= mi;
  val[1][root] -= mi;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> ar[i] >> b[i] >> c[i];
  long long sum = 0;
  for (long long i = 0; i < n; i++) sum += b[i] - c[i];
  if (sum) {
    cout << "-1";
    return 0;
  }
  for (long long i = 0; i < n - 1; i++) {
    long long u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(0, 0);
  cout << ans;
  return 0;
}
