#include <bits/stdc++.h>
using namespace std;
int n;
long long int cost = 0;
vector<int> a, b, c;
vector<vector<int>> g;
pair<int, int> dfs(int cur, int par, int mn_before) {
  pair<int, int> ap = {0, 0};
  int mn_cur = min(a[cur], mn_before);
  if (b[cur] != c[cur]) {
    if (b[cur] == 1) {
      ap.first = 1;
      ap.second = 0;
    } else {
      ap.first = 0;
      ap.second = 1;
    }
  }
  for (auto it : g[cur]) {
    if (it != par) {
      pair<int, int> pa = dfs(it, cur, mn_cur);
      ap.first += pa.first;
      ap.second += pa.second;
    }
  }
  if (a[cur] < mn_before) {
    int cnt = min(ap.first, ap.second);
    cost += 2 * 1ll * cnt * 1ll * a[cur];
    ap.first -= cnt;
    ap.second -= cnt;
  }
  return ap;
}
long long int solve() {
  cin >> n;
  a.resize(n + 1);
  b.resize(n + 1);
  c.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  g.resize(n + 1);
  int u, v;
  for (int i = 0; i < n - 1; i++) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  pair<int, int> a = dfs(1, -1, 1000000000);
  if (a.first != a.second) {
    return -1;
  }
  return cost;
}
int main() {
  int t = 1;
  while (t--) {
    cout << solve() << "\n";
  }
}
