#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int maxn = 2e5 + 5;
long long ans, n, m, cnt, c0, c1, head[maxn], a[maxn], b[maxn], c[maxn], u, v,
    l[maxn], r[maxn];
struct node {
  long long to;
  long long next;
} e[2 * maxn];
void add(long long u, long long v) {
  ++cnt;
  e[cnt].next = head[u];
  e[cnt].to = v;
  head[u] = cnt;
}
void dfs(long long u, long long fa) {
  if (fa) a[u] = min(a[u], a[fa]);
  int k = -1;
  if (b[u] == 0 && c[u] == 1) k = 0;
  if (b[u] == 1 && c[u] == 0) k = 1;
  long long lll = 0, rr = 0;
  for (int i = head[u]; i != -1; i = e[i].next) {
    long long v = e[i].to;
    if (v == fa) continue;
    dfs(v, u);
    l[u] += l[v];
    r[u] += r[v];
    if (l[v] > r[v])
      lll += l[v] - r[v];
    else
      rr += r[v] - l[v];
  }
  if (k == 0) l[u]++, lll++;
  if (k == 1) r[u]++, rr++;
  ans += 2 * a[u] * min(lll, rr);
}
int main() {
  cin >> n;
  memset(head, -1, sizeof head);
  for (int i = 1; i <= n; i++) cin >> a[i] >> b[i] >> c[i];
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }
  dfs(1, 0);
  if (l[1] != r[1]) ans = -1;
  cout << ans;
  return 0;
}
