#include <bits/stdc++.h>
using namespace std;
vector<long long> arr, b, c;
vector<vector<long long>> g;
long long cost = 0;
pair<long long, long long> dfs(long long nd, long long pr, long long mn) {
  pair<long long, long long> a = {0, 0};
  if (b[nd] != c[nd]) {
    if (b[nd])
      a.first++;
    else
      a.second++;
  }
  for (auto &ch : g[nd]) {
    if (ch == pr) continue;
    pair<long long, long long> p = dfs(ch, nd, min(arr[nd], mn));
    a.first += p.first;
    a.second += p.second;
  }
  if (arr[nd] < mn) {
    long long take = min(a.first, a.second);
    cost += 2 * take * arr[nd];
    a.first -= take;
    a.second -= take;
  }
  return a;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  g = vector<vector<long long>>(n + 1);
  arr = vector<long long>(n + 1);
  b = vector<long long>(n + 1);
  c = vector<long long>(n + 1);
  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> p = dfs(1, 0, 2e9);
  if (p.first || p.second) {
    cout << "-1" << endl;
  } else {
    cout << cost << endl;
  }
  return 0;
}
