#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int arr = 200005;
int pow(long long int x, long long int y, long long int p) {
  if (y == 0) return 1;
  if (y == 1) return x;
  x = x % p;
  long long int ans = pow(x, y / 2, p) % p;
  ans = ans * ans % p;
  if (y % 2 == 0)
    return ans % p;
  else
    return ans * x % p;
}
long long int a[arr], b[arr], c[arr];
vector<vector<int> > adj(arr);
long long int ans = 0;
vector<bool> vis(arr, false);
vector<int> dfs(int v, long long int cost) {
  long long int tot;
  vector<int> wa(2, 0), w(2);
  cost = min(a[v], cost);
  vis[v] = true;
  for (int i = 0; i < adj[v].size(); i++) {
    if (!vis[adj[v][i]]) {
      vis[adj[v][i]] = true;
      w = dfs(adj[v][i], cost);
      wa[0] += w[0];
      wa[1] += w[1];
    }
  }
  if (b[v] != c[v]) {
    if (c[v])
      wa[1]++;
    else
      wa[0]++;
  }
  long long int temp;
  temp = min(wa[1], wa[0]);
  tot = 2 * temp * cost;
  ans += tot;
  wa[1] -= temp;
  wa[0] -= temp;
  return wa;
}
void solve() {
  long long int n;
  cin >> n;
  long long int temp, x;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    a[i] = temp;
    cin >> temp;
    b[i] = temp;
    cin >> temp;
    c[i] = temp;
  }
  for (int i = 0; i < n - 1; i++) {
    cin >> temp >> x;
    adj[temp - 1].push_back(x - 1);
    adj[x - 1].push_back(temp - 1);
  }
  vector<int> di(2);
  di = dfs(0, a[0]);
  if (di[0] != di[1])
    cout << "-1";
  else
    cout << ans;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
