#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int a[maxn], b[maxn], c[maxn];
int cnt[maxn][2];
long long ans = 0;
vector<int> v[maxn];
void dfs(int x, int fa) {
  a[x] = min(a[x], a[fa]);
  for (auto u : v[x]) {
    if (fa == u) continue;
    dfs(u, x);
    cnt[x][0] += cnt[u][0];
    cnt[x][1] += cnt[u][1];
  }
  if (b[x] != c[x]) cnt[x][b[x]]++;
  int mi = min(cnt[x][0], cnt[x][1]);
  ans += 2ll * a[x] * mi;
  cnt[x][0] -= mi;
  cnt[x][1] -= mi;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    b[i] = read();
    c[i] = read();
  }
  for (int i = 1; i <= n - 1; i++) {
    int t1 = read(), t2 = read();
    v[t1].push_back(t2);
    v[t2].push_back(t1);
  }
  a[0] = inf;
  dfs(1, 0);
  cout << (cnt[1][1] == cnt[1][0] ? ans : -1) << endl;
  return 0;
}
