#include <bits/stdc++.h>
using namespace std;
vector<int> G[200005];
long long n, a[200005], b[200005], c[200005], s[200005], t[200005], dif[200005],
    vis[200005], id[200005], fa[200005];
int cmp(int x, int y) { return a[x] < a[y]; }
void dfs(int u, int pre) {
  if (b[u]) s[u]++;
  if (c[u]) t[u]++;
  if (b[u] != c[u]) dif[u]++;
  fa[u] = pre;
  for (auto v : G[u])
    if (v != pre) {
      dfs(v, u);
      s[u] += s[v];
      t[u] += t[v];
      dif[u] += dif[v];
    }
}
int getdif(int u, int pre) {
  if (vis[u]) return dif[u];
  int res = (b[u] != c[u]);
  for (auto v : G[u])
    if (v != pre) res += getdif(v, u);
  return res;
}
void dfs2(int u, int pre) {
  vis[u] = 1;
  for (auto v : G[u])
    if (!vis[v])
      if (v != pre) dfs2(v, u);
}
long long sum;
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld%lld", &a[i], &b[i], &c[i]), id[i] = i;
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%lld%lld", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  sort(id + 1, id + 1 + n, cmp);
  dfs(1, 1);
  if (s[1] != t[1]) {
    puts("-1");
    return 0;
  }
  for (int j = 1; j <= n; j++) {
    int i = id[j];
    if (vis[i]) continue;
    int diff = getdif(i, fa[i]);
    sum += a[i] * (diff - abs(s[i] - t[i]));
    dif[i] = abs(s[i] - t[i]);
    dfs2(i, fa[i]);
  }
  cout << sum << '\n';
}
