#include <bits/stdc++.h>
using namespace std;
const int SIZE = 200005;
vector<int> mp[SIZE], d[SIZE];
int a[SIZE], b[SIZE], c[SIZE], t1[SIZE], t2[SIZE], fa[SIZE], fm;
int y[SIZE];
void DFS(int p, int f, int de) {
  for (int i = 0; i < mp[p].size(); i++)
    if (mp[p][i] != f) {
      int to = mp[p][i];
      a[to] = min(a[to], a[p]);
      fa[to] = p;
      DFS(to, p, de + 1);
    }
  if (y[p] == 1)
    t1[p] = 1;
  else if (y[p] == 2)
    t2[p] = 1;
  fm = max(fm, de);
  d[de].push_back(p);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", a + i, b + i, c + i);
    if (b[i] == 1 && c[i] == 0)
      y[i] = 1;
    else if (b[i] == 0 && c[i] == 1)
      y[i] = 2;
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    mp[u].push_back(v);
    mp[v].push_back(u);
  }
  DFS(1, -1, 1);
  long long cost = 0;
  for (int i = fm; i >= 1; i--) {
    for (int j = 0; j < d[i].size(); j++) {
      int num = min(t1[d[i][j]], t2[d[i][j]]);
      cost += (long long)a[d[i][j]] * num * 2;
      t1[d[i][j]] -= num;
      t2[d[i][j]] -= num;
      t1[fa[d[i][j]]] += t1[d[i][j]];
      t2[fa[d[i][j]]] += t2[d[i][j]];
    }
  }
  if (t1[1] == 0 && t2[1] == 0)
    printf("%I64d\n", cost);
  else
    puts("-1");
  return 0;
}
