#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
vector<int> arr[200001];
long long n, u, v, w;
long long val[200001];
bool curr[200001];
bool want[200001];
long long ans = 0;
void dfs(int to, int from) {
  if (to != 1) {
    val[to] = min(val[to], val[from]);
  }
  for (auto i : arr[to]) {
    if (i != from) {
      dfs(i, to);
    }
  }
}
pair<long long int, long long int> dfs2(int to, int from) {
  pair<long long int, long long int> a = {0, 0};
  if (curr[to] != want[to]) {
    if (want[to]) {
      a.second++;
    } else {
      a.first++;
    }
  }
  for (auto i : arr[to]) {
    if (i != from) {
      auto p = dfs2(i, to);
      a.first += p.first;
      a.second += p.second;
    }
  }
  if (a.first > a.second) {
    ans += 2 * val[to] * a.second;
    a.first -= a.second;
    a.second = 0;
  } else {
    ans += 2 * val[to] * a.first;
    a.second -= a.first;
    a.first = 0;
  }
  return a;
}
void pre() {}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> val[i] >> curr[i] >> want[i];
  }
  for (int i = 0; i < n - 1; i++) {
    cin >> u >> v;
    arr[v].push_back(u);
    arr[u].push_back(v);
  }
  dfs(1, 0);
  auto i = dfs2(1, 0);
  if (i.first != 0 || i.second != 0) {
    cout << -1 << "\n";
  } else {
    cout << ans << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  pre();
  long long int num = 1;
  for (long long int i = 0; i < num; i++) {
    solve();
  }
}
