#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
const long long INF = 1LL * 1e9 * 1e7;
int n, u, v;
vector<int> adj[MAXN];
int b[MAXN], c[MAXN];
long long a[MAXN];
long long dp[MAXN];
long long res;
pair<int, int> f(int x, long long mn, int pr = -1) {
  pair<int, int> p;
  p.first = 0, p.second = 0;
  if (b[x] > c[x]) p.first++;
  if (b[x] < c[x]) p.second++;
  for (int u : adj[x]) {
    if (u != pr) {
      pair<int, int> tp = f(u, min(mn, a[x]), x);
      p.first += tp.first;
      p.second += tp.second;
    }
  }
  if (a[x] < mn) {
    int can = min(p.first, p.second);
    res += 2LL * a[x] * can;
    p.first -= can;
    p.second -= can;
  }
  return p;
}
void solve() {
  cin >> n;
  for (int i = 0, _i = (n); i < _i; ++i) {
    cin >> a[i] >> b[i] >> c[i];
    dp[i] = -1;
  }
  for (int i = 0, _i = (n - 1); i < _i; ++i) {
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  pair<int, int> p = f(0, INF);
  if (p.first + p.second)
    cout << -1 << endl;
  else
    cout << res << endl;
}
int ntest;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}
