#include <bits/stdc++.h>
using namespace std;
struct Node {
  long long a, b, c;
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  vector<Node> v(n);
  long long foo = 0, bar = 0;
  for (long long i = 0; i < n; ++i) {
    cin >> v[i].a >> v[i].b >> v[i].c;
    foo += v[i].b;
    bar += v[i].c;
  }
  if (foo != bar) {
    cout << -1;
    return 0;
  }
  vector<long long> adj[n];
  for (long long i = 0; i < n - 1; ++i) {
    long long u, v;
    cin >> u >> v;
    --u, --v;
    adj[u].emplace_back(v);
    adj[v].emplace_back(u);
  }
  long long ans = 0;
  function<pair<long long, long long>(long long, long long, long long)> dfs =
      [&](long long u, long long p,
          long long cost) -> pair<long long, long long> {
    pair<long long, long long> tmp = {(v[u].b == 0 && v[u].c == 1),
                                      (v[u].b == 1 && v[u].c == 0)};
    for (auto& x : adj[u]) {
      if (x != p) {
        auto t = dfs(x, u, min(cost, v[x].a));
        tmp.first += t.first;
        tmp.second += t.second;
      }
    }
    long long shuffle = min(tmp.first, tmp.second);
    ans += 2 * shuffle * cost;
    tmp.first -= shuffle;
    tmp.second -= shuffle;
    return tmp;
  };
  dfs(0, -1, v[0].a);
  cout << ans;
}
