#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int a[N], b[N], c[N], dp[N][2];
int n;
vector<int> v[N];
long long dfs(int node, int par, int cost) {
  if (b[node] == 1 && c[node] == 0) {
    dp[node][1] = 1;
  } else if (b[node] == 0 && c[node] == 1) {
    dp[node][0] = 1;
  }
  long long ans = 0;
  for (auto i : v[node]) {
    if (i == par) continue;
    ans += dfs(i, node, min(cost, a[i]));
    dp[node][0] += dp[i][0];
    dp[node][1] += dp[i][1];
  }
  int minx = min(dp[node][0], dp[node][1]);
  dp[node][0] -= minx;
  dp[node][1] -= minx;
  ans += cost * 1LL * minx * 2;
  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i] >> b[i] >> c[i];
  for (int i = 1; i < n; i++) {
    int p, q;
    cin >> p >> q;
    v[p].push_back(q);
    v[q].push_back(p);
  }
  long long ans = dfs(1, -1, a[1]);
  if (dp[1][0] + dp[1][1] != 0) {
    cout << "-1";
  } else
    cout << ans;
}
