#include <bits/stdc++.h>
using namespace std;
const long double PI = atan(1.0) * 4;
const int64_t INF = 3e18;
const int32_t LOG = 21;
const long long int MOD = 998244353;
pair<long long int, long long int> vals[200005];
long long int cost[200005];
vector<long long int> v[200005];
vector<pair<long long int, long long int> > v1;
long long int tin[200005];
long long int tout[200005];
long long int table[2][400005] = {0};
long long int timer = 0;
long long int parent[200005];
bool check[200005];
vector<long long int> order;
void update(long long int ind, long long int i, long long int delta) {
  while (i <= 400001) {
    table[ind][i] += delta;
    i += (i & -i);
  }
}
long long int pref_sum(long long int ind, long long int i) {
  long long int sum = 0;
  while (i > 0) {
    sum += table[ind][i];
    i -= i & -i;
  }
  return sum;
}
long long int sum(long long int ind, long long int l, long long int r) {
  if (l > r) return 0;
  return pref_sum(ind, r) - pref_sum(ind, l - 1);
}
void dfs(long long int i, long long int prev = -1) {
  parent[i] = prev;
  long long int x = vals[i].second - vals[i].first;
  tin[i] = ++timer;
  order.push_back(i);
  if (x == 1)
    update(1, tin[i], 1);
  else if (x == -1)
    update(0, tin[i], 1);
  for (auto it : v[i]) {
    if (it != prev) {
      dfs(it, i);
    }
  }
  tout[i] = ++timer;
  order.push_back(i);
}
void dfs1(long long int i, long long int prev) {
  check[i] = true;
  for (auto it : v[i]) {
    if (check[it] || it == prev) continue;
    dfs1(it, i);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  long long int tests = 1;
  for (long long int ii = 0; ii < tests; ii++) {
    long long int n;
    cin >> n;
    long long int tot = 0;
    for (long long int i = 1; i < n + 1; i++) {
      cin >> cost[i] >> vals[i].first >> vals[i].second;
      tot += vals[i].first - vals[i].second;
      v1.push_back({cost[i], i});
    }
    for (long long int i = 1; i < n; i++) {
      long long int x, y;
      cin >> x >> y;
      v[x].push_back(y);
      v[y].push_back(x);
    }
    if (tot != 0) {
      cout << -1 << '\n';
      return 0;
    }
    dfs(1);
    sort(v1.begin(), v1.end());
    long long int ans = 0;
    for (auto it : v1) {
      long long int price = 2 * it.first;
      long long int i = it.second;
      if (check[i]) continue;
      long long int ones = sum(1, tin[i], tout[i]);
      long long int zeros = sum(0, tin[i], tout[i]);
      long long int min1 = min(ones, zeros);
      ans += (price * min1);
      update(1, tin[i], -min1);
      update(0, tin[i], -min1);
      dfs1(i, parent[i]);
    }
    cout << ans << '\n';
  }
  return 0;
}
