#include <bits/stdc++.h>
using namespace std;
vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
const long long N = (long long)(1e6) + 322;
const long long inf = (long long)(2e18);
const long long mod = (long long)(1e9) + 7;
const double eps = (1e-6);
vector<long long> v[N];
long long cnt[N], a[N], b[N], c[N], ans;
long long getcost(long long B, long long C) {
  if (B == C) return 0;
  if (B > C) return 1;
  return -1;
}
void dfs(long long x, long long p = -1) {
  long long neg = 0;
  long long pos = 0;
  if (getcost(b[x], c[x]) < 0)
    neg--;
  else if (getcost(b[x], c[x]) > 0)
    pos++;
  for (auto to : v[x]) {
    if (to == p) continue;
    a[to] = min(a[to], a[x]);
    dfs(to, x);
    if (cnt[to] > 0)
      pos += cnt[to];
    else if (cnt[to] < 0)
      neg += cnt[to];
  }
  ans += a[x] * min(abs(pos), abs(neg)) * 2;
  cnt[x] = pos + neg;
}
void solve(long long test) {
  long long n;
  cin >> n;
  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;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs(1);
  if (cnt[1]) {
    cout << -1;
  } else {
    cout << ans;
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long test = 1;
  for (long long tt = 1; tt <= test; ++tt) {
    solve(tt);
  }
  return 0;
}
