#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  ll a[n];
  int b[n], c[n];
  for (int i = 0; i < (n); i++) cin >> a[i] >> b[i] >> c[i];
  vector<vector<int>> g(n);
  for (int i = 0; i < (n - 1); i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int valid = 0;
  for (int i = 0; i < (n); i++) valid += b[i] - c[i];
  if (valid != 0) {
    cout << -1 << endl;
    return 0;
  }
  ll cnt[n][2], dp[n];
  for (int i = 0; i < (n); i++) cnt[i][0] = 0, cnt[i][1] = 0, dp[i] = LINF;
  multiset<ll> st;
  ll ans = 0;
  function<void(int, int)> dfs = [&](int pre, int now) {
    st.insert(a[now]);
    for (auto to : g[now])
      if (to != pre) {
        dfs(now, to);
        cnt[now][0] += cnt[to][0];
        cnt[now][1] += cnt[to][1];
      }
    if (b[now] == 0 and c[now] == 1) cnt[now][0]++;
    if (b[now] == 1 and c[now] == 0) cnt[now][1]++;
    st.erase(st.find(a[now]));
    auto ite = st.lower_bound(a[now]);
    if (ite == st.begin()) {
      ll tmp = min(cnt[now][0], cnt[now][1]);
      ans += 2 * tmp * a[now];
      cnt[now][0] -= tmp;
      cnt[now][1] -= tmp;
    }
  };
  dfs(-1, 0);
  cout << ans << endl;
  return 0;
}
