#include <bits/stdc++.h>
using std::map;
using std::min;
using std::set;
using std::sort;
using std::vector;
int n;
vector<int> a, b, c;
vector<set<int>> g;
vector<int> parent;
vector<set<int>> children;
void set_parents(int x) {
  for (int y : g[x]) {
    if (parent[y] == -1) {
      parent[y] = x;
      set_parents(y);
    }
  }
}
struct CN {
  int cost;
  int node;
  bool operator<(const CN& o) const {
    if (cost != o.cost) return cost < o.cost;
    return node < o.node;
  }
};
struct S {
  int flipup;
  int flipdown;
  S& operator+=(const S& o) {
    flipup += o.flipup;
    flipdown += o.flipdown;
    return *this;
  }
};
map<int, S> summary;
void compute_summary(int x) {
  S result{0, 0};
  for (int y : children[x]) {
    if (summary.find(y) == summary.end()) compute_summary(y);
    result += summary[y];
  }
  if (c[x] > b[x]) ++result.flipup;
  if (c[x] < b[x]) ++result.flipdown;
  summary[x] = result;
}
int main() {
  scanf("%d", &n);
  a.resize(n);
  b.resize(n);
  c.resize(n);
  g.resize(n);
  for (int i = 0; i < n; ++i) scanf("%d %d %d", &a[i], &b[i], &c[i]);
  for (int i = 1; i < n; ++i) {
    int x, y;
    scanf("%d %d", &x, &y);
    --x;
    --y;
    g[x].insert(y);
    g[y].insert(x);
  }
  parent.resize(n, -1);
  children.resize(n);
  set_parents(0);
  for (int i = 1; i < n; ++i) children[parent[i]].insert(i);
  vector<CN> d;
  for (int i = 0; i < n; ++i) d.push_back(CN{a[i], i});
  sort(d.begin(), d.end());
  int64_t cost = 0;
  for (CN cn : d)
    if (summary.find(cn.node) == summary.end()) {
      int x = cn.node;
      if (0) printf("x=%d\n", x);
      compute_summary(x);
      int toflip = min(summary[x].flipup, summary[x].flipdown);
      cost += 2 * int64_t(a[x]) * int64_t(toflip);
      summary[x].flipup -= toflip;
      summary[x].flipdown -= toflip;
    }
  if (summary[0].flipup || summary[0].flipdown) {
    printf("-1\n");
  } else {
    printf("%" PRId64 "\n", cost);
  }
}
