#include <bits/stdc++.h>
using namespace std;
struct Node {
  long long cost;
  long long b, c;
  bool visited;
  vector<Node *> adj;
  Node() : visited(false), cost(0), b(0), c(0) {}
};
long long getCost(Node *tree, long long &n0, long long &n1, long long cost);
int main() {
  long long n, a, b;
  long long btot = 0, ctot = 0;
  long long total;
  cin >> n;
  Node *tree;
  tree = new Node[n];
  for (long long i = 0; i < n; i++) {
    cin >> tree[i].cost >> tree[i].b >> tree[i].c;
    btot += tree[i].b;
    ctot += tree[i].c;
  }
  for (long long i = 0; i < n - 1; i++) {
    cin >> a >> b;
    tree[a - 1].adj.push_back(&tree[b - 1]);
    tree[b - 1].adj.push_back(&tree[a - 1]);
  }
  if (btot != ctot) {
    cout << -1;
    return 0;
  }
  total = getCost(tree, btot, ctot, tree->cost);
  cout << total;
  delete[] tree;
}
long long getCost(Node *tree, long long &n0, long long &n1, long long cost) {
  long long m0 = 0, m1 = 0;
  long long total = 0;
  if (tree->visited) return 0;
  tree->visited = true;
  if (cost < tree->cost) {
    tree->cost = cost;
  }
  n0 = n1 = 0;
  if (tree->b != tree->c) {
    if (tree->c == 0)
      n0 = 1;
    else
      n1 = 1;
  }
  for (long long i = 0; i < tree->adj.size(); i++) {
    if (tree->adj[i]->visited) continue;
    m0 = m1 = 0;
    total += getCost(tree->adj[i], m0, m1, tree->cost);
    n0 += m0;
    n1 += m1;
  }
  long long a = (n0 < n1 ? n0 : n1);
  total += tree->cost * a * 2;
  n0 -= a;
  n1 -= a;
  return total;
}
