#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const double pi = acos(-1);
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int t, n;
long long a[200005], b[200005], c[200005];
long long mn[200005], ans;
int tot, inde[200005];
struct p {
  int u, v, nxt;
} e[400004];
void add(int u, int v) {
  e[++tot].u = u, e[tot].v = v, e[tot].nxt = inde[u], inde[u] = tot;
}
int siz[200005][2];
void dfs(int u, int f) {
  int i, v;
  mn[u] = min(a[u], mn[f]);
  for (i = inde[u]; i; i = e[i].nxt) {
    v = e[i].v;
    if (v == f) continue;
    dfs(v, u);
    siz[u][0] += siz[v][0];
    siz[u][1] += siz[v][1];
  }
  if (b[u] != c[u]) siz[u][b[u]]++;
  ans += mn[u] * 2 * min(siz[u][0], siz[u][1]);
  long long x = min(siz[u][0], siz[u][1]);
  siz[u][0] -= x, siz[u][1] -= x;
}
int main() {
  int i, j, u, v, now, rev;
  n = read();
  for (i = 1; i <= n; i++) a[i] = read(), b[i] = read(), c[i] = read();
  for (i = 1; i < n; i++) {
    u = read(), v = read();
    add(u, v), add(v, u);
  }
  mn[0] = 0x7fffffff;
  dfs(1, 0);
  if (siz[1][0] + siz[1][1] != 0)
    puts("-1");
  else
    printf("%lld\n", ans);
}
