#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e9 + 5;
int r[200005];
pair<int, int> p[200005];
int bio[200005];
vector<int> v[200005];
int par[200005];
int A, B;
void dfs(int node, int p) {
  par[node] = p;
  for (int i = 0; i < v[node].size(); i++) {
    if (v[node][i] == p) continue;
    dfs(v[node][i], node);
  }
}
void izbroji(int node) {
  if (r[node] < 0)
    A += r[node];
  else
    B += r[node];
  if (bio[node]) return;
  bio[node] = 1;
  for (int i = 0; i < v[node].size(); i++) {
    if (v[node][i] == par[node]) continue;
    izbroji(v[node][i]);
  }
}
int main() {
  int n;
  scanf("%d", &n);
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    int c, a, b;
    cin >> c >> a >> b;
    sum += a - b;
    r[i] = a - b;
    p[i - 1] = make_pair(c, i);
  }
  for (int i = 0; i < n - 1; i++) {
    int first, second;
    scanf("%d%d", &first, &second);
    v[first].push_back(second);
    v[second].push_back(first);
  }
  if (sum) {
    printf("-1\n");
    return 0;
  }
  dfs(1, -1);
  sort(p, p + n);
  long long sol = 0;
  for (int i = 0; i < n; i++) {
    int node = p[i].second;
    long long pr = p[i].first;
    A = 0;
    B = 0;
    izbroji(node);
    sol += pr * min(-A, B) * 2;
    r[node] = A + B;
  }
  printf("%I64d\n", sol);
  return 0;
}
