#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
const int INF = 2 * 1000 * 1000 * 1000;
void DFS(int curr, const vector<vector<int>>& gr,
         const vector<array<int, 3>>& vert, vector<vector<int>>& dp,
         int parent = -1, int coeff = INF) {
  coeff = min(coeff, vert[curr][0]);
  for (auto next : gr[curr]) {
    if (next == parent) {
      continue;
    }
    DFS(next, gr, vert, dp, curr, coeff);
    dp[curr][0] += dp[next][0];
    dp[curr][1] += dp[next][1];
  }
  if (vert[curr][1] != vert[curr][2]) {
    ++dp[curr][vert[curr][1]];
  }
  int balance = min(dp[curr][0], dp[curr][1]);
  dp[curr][0] -= balance;
  dp[curr][1] -= balance;
  ans += 2LL * balance * coeff;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<array<int, 3>> vert(n);
  for (int i = 0; i < n; ++i) {
    cin >> vert[i][0] >> vert[i][1] >> vert[i][2];
  }
  vector<vector<int>> gr(n);
  for (int _ = 0; _ < n - 1; ++_) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    gr[u].push_back(v);
    gr[v].push_back(u);
  }
  vector<vector<int>> dp(n, {0, 0});
  DFS(0, gr, vert, dp);
  if (dp[0][0] > 0 || dp[0][1] > 0) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}
