#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 10;
long long arr[MAX];
int cur[MAX], want[MAX];
int n;
vector<vector<int> > adj(MAX);
long long ans = 0;
array<int, 2> dfs(int node, int par, long long Min) {
  array<int, 2> a, b;
  a[0] = 0, a[1] = 0;
  if (cur[node] != want[node]) a[cur[node]]++;
  for (auto &child : adj[node]) {
    if (child == par) continue;
    b = dfs(child, node, min(Min, arr[node]));
    a[0] += b[0], a[1] += b[1];
  }
  if (arr[node] < Min) {
    int x = min(a[0], a[1]);
    ans += x * 2ll * arr[node];
    a[0] -= x, a[1] -= x;
  }
  return a;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> arr[i] >> cur[i] >> want[i];
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  array<int, 2> a = dfs(1, -1, 2e9);
  if (a[0] || a[1])
    cout << -1 << "\n";
  else
    cout << ans << "\n";
  return 0;
}
