#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const int N = 1e6 + 6, mod = 998244353, M = 1e9 + 7;
const long long MAX = 5e18;
int n, u, v, a[N][3], c[N][2];
vector<int> g[N];
long long slv(int u = 1, int p = 1, int v = M) {
  v = min(v, a[u][0]);
  long long ret = 0;
  for (int i : g[u])
    if (i != p) {
      ret += slv(i, u, v);
      c[u][0] += c[i][0];
      c[u][1] += c[i][1];
    }
  if (a[u][1] != a[u][2]) c[u][a[u][1]]++;
  long long t = min(c[u][0], c[u][1]);
  c[u][0] -= t;
  c[u][1] -= t;
  return ret + t * 2 * v;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < 3; j++) scanf("%d", &a[i][j]);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  long long ans = slv();
  cout << (c[1][0] == c[1][1] ? ans : -1);
  return 0;
}
