#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > tree;
vector<int> a, b, c;
long long cost = 0;
pair<int, int> dfs(int u, int p, int mn) {
  int type_1 = 0, type_2 = 0;
  if (b[u] == 0 && c[u] == 1)
    type_1++;
  else if (b[u] == 1 && c[u] == 0)
    type_2++;
  for (int v : tree[u]) {
    if (v == p) continue;
    pair<int, int> tmp = dfs(v, u, min(mn, a[v]));
    type_1 += tmp.first;
    type_2 += tmp.second;
  }
  int k = min(type_1, type_2);
  cost += 2LL * mn * k;
  type_1 -= k;
  type_2 -= k;
  return {type_1, type_2};
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  tree.assign(n + 1, vector<int>());
  a.assign(n + 1, 0);
  b.assign(n + 1, 0);
  c.assign(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  for (int i = 0, u, v; i < n - 1; i++) {
    cin >> u >> v;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }
  pair<int, int> ans = dfs(1, -1, a[1]);
  if (ans.first || ans.second) {
    cout << "-1\n";
  } else {
    cout << cost << "\n";
  }
  return 0;
}
