#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000037;
const int maxn = 200005;
int n, fr, to;
int a[maxn], b[maxn], c[maxn];
int d[maxn][2];
vector<int> g[maxn];
struct cost_cmp {
  bool operator()(const int &x, const int &y) const { return a[x] < a[y]; }
};
long long ans;
set<int, cost_cmp> market;
void dfs_demand(int u, int p) {
  int best = inf;
  if (!market.empty()) best = a[*market.begin()];
  market.insert(u);
  for (int v : g[u]) {
    if (v != p) {
      dfs_demand(v, u);
      d[u][0] += d[v][0];
      d[u][1] += d[v][1];
    }
  }
  if (a[u] < best) {
    long long triangle = min(d[u][0], d[u][1]);
    d[u][0] -= triangle, d[u][1] -= triangle;
    ans += 2LL * triangle * ((long long)a[u]);
  }
  market.erase(u);
}
int32_t main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d %d %d", &a[i], &b[i], &c[i]);
    if (b[i] != c[i]) ++d[i][c[i]];
  }
  for (int i = 1; i < n; ++i) {
    scanf("%d %d", &fr, &to);
    g[fr].push_back(to);
    g[to].push_back(fr);
  }
  dfs_demand(1, -1);
  if (d[1][0] + d[1][1]) ans = -1LL;
  printf("%lld\n", ans);
}
