#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
int h[N], ne[N], e[N], idx;
long long a[N], b[N], c[N];
int cnt0[N], cnt1[N];
long long res;
void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }
void dfs(int u, int fa, long long tmp) {
  long long mi = min(tmp, a[u]);
  int i;
  for (i = h[u]; i != -1; i = ne[i]) {
    int j = e[i];
    if (j == fa) continue;
    dfs(j, u, mi);
    cnt0[u] += cnt0[j];
    cnt1[u] += cnt1[j];
  }
  if (b[u] != c[u]) {
    if (b[u] == 1)
      cnt1[u]++;
    else {
      cnt0[u]++;
    }
  }
  int x = min(cnt1[u], cnt0[u]);
  if (mi == a[u]) {
    cnt1[u] -= x;
    cnt0[u] -= x;
    res += 2 * x * a[u];
  }
}
int main() {
  int n;
  cin >> n;
  memset(h, -1, sizeof h);
  int i;
  int tmp1 = 0, tmp2 = 0;
  for (i = 1; i <= n; i++) {
    scanf("%lld%lld%lld", &a[i], &b[i], &c[i]);
    if (b[i]) tmp1++;
    if (c[i]) tmp2++;
  }
  for (i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  if (tmp1 != tmp2) {
    cout << "-1" << endl;
  } else {
    dfs(1, -1, a[1]);
    cout << res << endl;
  }
}
