#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  long long int r, i;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
long long int power(long long int x, long long int y, long long int mod) {
  long long int temp, ty, my;
  if (y == 0) return 1;
  temp = power(x, y / 2, mod);
  ty = (temp % mod) * (temp % mod);
  if (y % 2 == 0) {
    return ty % mod;
  } else {
    my = (x % mod) * (ty % mod);
    return my % mod;
  }
}
void fastscan(int &number) {
  bool negative = false;
  register int c;
  number = 0;
  c = getchar();
  if (c == '-') {
    negative = true;
    c = getchar();
  }
  for (; (c > 47 && c < 58); c = getchar()) number = number * 10 + c - 48;
  if (negative) number *= -1;
}
struct node {
  long long int cost;
  long long int ini;
  long long int fin;
  long long int subtree0;
  long long int subtree1;
};
const long long int N = 200010;
struct node ar[N];
vector<long long int> graph[N];
long long int ans[N];
long long int visit[N];
double eps = 2.22045e-010;
void dfs(long long int start, long long int par) {
  long long int i, j, k;
  long long int sz = graph[start].size();
  if (par != -1) {
    ar[start].cost = min(ar[start].cost, ar[par].cost);
  }
  long long int rem0 = ar[start].subtree0;
  long long int rem1 = ar[start].subtree1;
  long long int sum = 0;
  for (i = (0); i < (sz); i++) {
    long long int num = graph[start][i];
    if (num != par) {
      dfs(num, start);
    } else {
      continue;
    }
    ar[start].subtree0 += ar[num].subtree0;
    ar[start].subtree1 += ar[num].subtree1;
    long long int diff = max(ar[num].subtree0, ar[num].subtree1) -
                         min(ar[num].subtree0, ar[num].subtree1);
    if (ar[num].subtree0 > ar[num].subtree1) {
      rem0 += diff;
    } else {
      rem1 += diff;
    }
    sum = sum + min(ans[num], 2 * min(ar[num].subtree0, ar[num].subtree1) *
                                  ar[start].cost);
  }
  sum = sum + min(rem0, rem1) * 2 * ar[start].cost;
  ans[start] = sum;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, m, i, t, j, k, q;
  long long int a, b, c, h, d;
  cin >> n;
  long long int cn0 = 0;
  long long int cn1 = 0;
  for (i = (1); i < (n + 1); i++) {
    cin >> ar[i].cost >> ar[i].ini >> ar[i].fin;
    ar[i].subtree0 = 0;
    ar[i].subtree1 = 0;
    if (ar[i].ini != ar[i].fin) {
      if (ar[i].ini == 0) {
        cn0++;
        ar[i].subtree0 = 1;
      } else {
        cn1++;
        ar[i].subtree1 = 1;
      }
    }
  }
  for (i = (0); i < (n - 1); i++) {
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  if (cn0 != cn1) {
    cout << "-1\n";
    return 0;
  }
  dfs(1, -1);
  cout << ans[1] << "\n";
  return 0;
}
