#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 10;
int n;
int a[N], b[N];
vector<int> g[N];
ll ans = 0;
int dfs(int from, int par, int cost) {
  a[from] = min(a[from], cost);
  ll pos = 0, neg = 0;
  if (b[from] == 1)
    pos++;
  else if (b[from] == -1)
    neg++;
  for (int to : g[from])
    if (to != par) {
      int diff = dfs(to, from, a[from]);
      if (diff < 0)
        neg -= diff;
      else
        pos += diff;
    }
  ans += 2ll * min(pos, neg) * a[from];
  return pos - neg;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    int x, y;
    scanf("%d %d %d", &a[i], &x, &y);
    b[i] = x - y;
  }
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  int diff = dfs(1, -1, a[1]);
  if (diff != 0) ans = -1;
  printf("%lld\n", ans);
  return 0;
}
