#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6;
long long n;
vector<long long> a, b, c;
vector<long long> g[N];
long long ans = 0;
pair<long long, long long> dfs(long long v, long long mn, long long pr) {
  pair<long long, long long> p = {0, 0};
  if (b[v] != c[v]) {
    if (b[v])
      p.first++;
    else
      p.second++;
  }
  for (long long i = 0; i < g[v].size(); i++) {
    long long x = g[v][i];
    if (x == pr) continue;
    pair<long long, long long> t = dfs(x, min(mn, a[x]), v);
    p.first += t.first;
    p.second += t.second;
  }
  long long temp_min = min(p.first, p.second);
  ans += (temp_min + temp_min) * mn;
  p.first -= temp_min;
  p.second -= temp_min;
  return p;
}
int32_t main() {
  cin >> n;
  a.resize(n + 1);
  b.resize(n + 1);
  c.resize(n + 1);
  for (long long i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  for (long long i = 1; i < n; i++) {
    long long x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  pair<long long, long long> vl = dfs(1, a[1], -1);
  if (vl.first > 0 || vl.second > 0) {
    cout << -1;
    return 0;
  }
  cout << ans << endl;
}
