#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 20;
const int inf = 0x3f3f3f3f;
const int modd = 1e9 + 7;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  if (c == '-') f = -1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return f * x;
}
template <class T>
inline void sc(T &x) {
  char c;
  x = 0;
  while ((c = getchar()) < '0')
    ;
  while (c >= '0' && c <= '9') x = x * 10 + (c - 48), c = getchar();
}
inline long long gcd(long long a, long long b) {
  return b == 0 ? a : gcd(b, a % b);
}
inline long long exgcd(long long a, long long b, long long &x, long long &y) {
  long long d;
  (b == 0 ? (x = 1, y = 0, d = a)
          : (d = exgcd(b, a % b, y, x), y -= a / b * x));
  return d;
}
inline long long qpow(long long a, long long n) {
  long long sum = 1;
  while (n) {
    if (n & 1) sum = sum * a % modd;
    a = a * a % modd;
    n >>= 1;
  }
  return sum;
}
inline long long qmul(long long a, long long n) {
  long long sum = 0;
  while (n) {
    if (n & 1) sum = (sum + a) % modd;
    a = (a + a) % modd;
    n >>= 1;
  }
  return sum;
}
inline long long inv(long long a) { return qpow(a, modd - 2); }
struct Coke {
  int a, b, c, id;
  int ou[2];
} p[maxn];
vector<int> vec[maxn];
bool cmp(Coke p1, Coke p2) { return p1.a < p2.a; }
bool cmp2(Coke p1, Coke p2) { return p1.id < p2.id; }
int vis[maxn];
int pai[maxn];
int fa[maxn];
void dfs(int i) {
  for (auto it : vec[i]) {
    if (it == fa[i]) continue;
    if (vis[it]) continue;
    fa[it] = i;
    dfs(it);
  }
  if (p[i].b == 0 && p[i].c == 1) p[i].ou[0] = 1;
  if (p[i].b == 1 && p[i].c == 0) p[i].ou[1] = 1;
}
void dfsk(int i, int &p0, int &p1) {
  for (auto it : vec[i]) {
    if (it == fa[i]) continue;
    if (vis[it]) continue;
    dfsk(it, p0, p1);
  }
  p0 += p[i].ou[0];
  p1 += p[i].ou[1];
}
void dfv(int i) {
  vis[i] = 1;
  for (auto it : vec[i]) {
    if (it == fa[i]) continue;
    if (vis[it]) continue;
    dfv(it);
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d%d", &p[i].a, &p[i].b, &p[i].c), p[i].id = i;
  int qing1 = 0, qing2 = 0;
  for (int i = 1; i <= n; i++) {
    if (p[i].b == 0 && p[i].c == 1)
      qing1++;
    else if (p[i].c == 0 && p[i].b == 1)
      qing2++;
  }
  if (qing1 != qing2) {
    printf("-1\n");
    return 0;
  }
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    vec[x].push_back(y);
    vec[y].push_back(x);
  }
  fa[1] = -1;
  dfs(1);
  sort(p + 1, p + 1 + n, cmp);
  for (int i = 1; i <= n; i++) pai[i] = p[i].id;
  sort(p + 1, p + 1 + n, cmp2);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int pid = pai[i];
    if (vis[pid]) continue;
    int fi = 0, se = 0;
    dfsk(pid, fi, se);
    int mi = min(fi, se);
    ans += 1ll * p[pid].a * mi * 2;
    fi -= mi;
    se -= mi;
    dfv(pid);
    if (fa[pid] != -1) p[fa[pid]].ou[0] += fi, p[fa[pid]].ou[1] += se;
  }
  printf("%lld\n", ans);
}
