#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
long long n, cost = 0;
long long arr[N], b[N], c[N];
vector<long long> g[N];
pair<long long, long long> dfs(long long u, long long par, long long mn) {
  pair<long long, long long> a = {0, 0};
  if (b[u] != c[u]) {
    if (b[u])
      a.first++;
    else
      a.second++;
  }
  for (auto &it : g[u]) {
    if (it == par) continue;
    pair<long long, long long> p = dfs(it, u, min(mn, arr[u]));
    a.first += p.first;
    a.second += p.second;
  }
  if (arr[u] < mn) {
    long long take = min(a.first, a.second);
    cost += 2 * take * arr[u];
    a.first -= take;
    a.second -= take;
  }
  return a;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> arr[i] >> b[i] >> c[i];
  for (long long i = 1; i <= n - 1; i++) {
    long long u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  pair<long long, long long> ans = dfs(1, 0, 2e9);
  if (ans.first || ans.second)
    cout << -1;
  else
    cout << cost;
  return 0;
}
