#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
using namespace std;
inline bool kthbit(long long n, int k) { return (n >> k) & 1; }
inline long long setkthbit(long long n, int k) { return n | (1ll << k); }
inline long long unsetkthbit(long long n, int k) { return n & ~(1ll << k); }
inline long long flipkthbit(long long n, int k) { return n ^ (1ll << k); }
long long my_log(long long n, int b) {
  long long i = 1;
  int ans = 0;
  while (1) {
    if (i > n) {
      ans--;
      break;
    }
    if (i == n) break;
    i *= b;
    ans++;
  }
  return ans;
}
long long pw(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long nc2(long long n) { return (1ll * n * (n - 1)) / 2; }
long long nsum(long long n) { return (1ll * n * (n + 1)) / 2; }
vector<vector<long long> > g;
vector<long long> a, b, c, d;
vector<long long> z, o;
long long ans = 0;
void dfs(long long u, long long par) {
  a[u] = min(a[u], a[par]);
  for (long long child : g[u]) {
    if (child != par) {
      dfs(child, u);
      z[u] += z[child];
      o[u] += o[child];
    }
  }
  if (b[u] != c[u] && b[u] == 0) z[u]++;
  if (b[u] != c[u] && b[u] == 1) o[u]++;
  long long mn = min(z[u], o[u]);
  ans += a[u] * 2 * mn;
  z[u] -= mn, o[u] -= mn;
}
void Solve() {
  long long n, m, k, x, y;
  cin >> n;
  a.resize(n + 1), b.resize(n + 1), c.resize(n + 1);
  z.resize(n + 1), o.resize(n + 1), g.resize(n + 1, vector<long long>(0));
  a[0] = LONG_LONG_MAX;
  long long zer = 0, one = 0;
  for (long long i = 1; i < n + 1; ++i) {
    cin >> a[i] >> b[i] >> c[i];
    if (b[i] == 0 && b[i] != c[i]) zer++;
    if (b[i] == 1 && b[i] != c[i]) one++;
  }
  for (long long i = 0; i < n - 1; ++i) {
    long long u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  if (zer != one) {
    cout << -1 << "\n";
    return;
  }
  dfs(1, 0);
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int tests = 1;
  while (tests--) {
    Solve();
  }
  return 0;
}
