#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
const long double Pi = acos(-1), e = 2.718;
const int N = 1e3 + 10, mod = 1e9 + 7;
template <typename T, typename U>
std::pair<T, U> operator+(const std::pair<T, U> &l, const std::pair<T, U> &r) {
  return {l.first + r.first, l.second + r.second};
}
struct Node {
  long long a, b, c;
  Node() : a(0), b(0), c(0) {}
};
vector<vector<long long> > Tree;
long long n;
vector<Node> node;
vector<pair<int, int> > child;
vector<long long> Min;
vector<pair<long long, pair<long long, long long> > > dp;
pair<int, int> dfs(int u, int par) {
  pair<int, int> ret = {(!node[u - 1].b && node[u - 1].c),
                        (node[u - 1].b && !node[u - 1].c)};
  for (auto v : Tree[u]) {
    if (v != par) {
      ret = ret + dfs(v, u);
    }
  }
  child[u] = ret;
  return ret;
}
void dfs2(int u, int par, long long mn) {
  Min[u] = min(mn, node[u - 1].a);
  for (auto v : Tree[u]) {
    if (v != par) {
      dfs2(v, u, min(mn, node[u - 1].a));
    }
  }
}
pair<long long, pair<long long, long long> > solve(int u, int par) {
  pair<long long, long long> rem = {0, 0};
  long long ret = 0;
  for (auto v : Tree[u]) {
    if (v != par) {
      auto x = solve(v, u);
      ret += x.first;
      rem = {rem.first + x.second.first, rem.second + x.second.second};
    }
  }
  long long x = min(child[u].first - rem.first, child[u].second - rem.second);
  ret += x * 2 * Min[u];
  rem = {rem.first + x, rem.second + x};
  dp[u] = {ret, rem};
  return dp[u];
}
int main() {
  cin >> n;
  Tree.assign(n + 1, vector<long long>());
  node.resize(n);
  child.resize(n + 1);
  Min.resize(n + 1, 1e18);
  dp.resize(n + 1, {-1, {-1, -1}});
  for (auto &x : node) {
    cin >> x.a >> x.b >> x.c;
  }
  for (int i = 1; i < n; i++) {
    long long u, v;
    cin >> u >> v;
    Tree[u].push_back(v);
    Tree[v].push_back(u);
  }
  dfs(1, -1);
  dfs2(1, -1, 1e18);
  if (child[1].first != child[1].second) return cout << -1 << '\n', 0;
  cout << solve(1, -1).first << '\n';
  return 0;
}
