#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
const long long N = 2e6 + 5;
const long long INF = 1e18L;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << ": " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
long long val[N];
long long z0[N];
long long z1[N];
long long dp[N];
long long cnt[N];
vector<long long> v[N];
long long ans;
void dfs(long long s, long long p = 0) {
  for (auto i : v[s]) {
    if (i ^ p) {
      dp[i] = min(dp[i], dp[s]);
      dfs(i, s);
      z0[s] += z0[i];
      z1[s] += z1[i];
    }
  }
  long long x = min(z0[s], z1[s]);
  z0[s] -= x;
  z1[s] -= x;
  ans += 2 * x * dp[s];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long T = 1;
  while (T--) {
    long long n;
    cin >> n;
    for (long long i = 1; i <= n; i++) {
      long long x, y;
      cin >> val[i] >> x >> y;
      dp[i] = val[i];
      if (x == y) continue;
      if (x == 0) z0[i]++;
      if (y == 0) z1[i]++;
      cnt[i] = 1;
    }
    for (long long i = 0; i < n - 1; i++) {
      long long x, y;
      cin >> x >> y;
      v[x].push_back(y);
      v[y].push_back(x);
    }
    dfs(1);
    if (z0[1] || z1[1]) return cout << -1, 0;
    cout << ans << '\n';
  }
  return 0;
}
