#include <bits/stdc++.h>
using namespace std;
long long M = 1e9 + 7;
double pi = acos(-1.0);
long long powerm(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1) res = (res * x) % M;
    y = y >> 1;
    x = (x * x) % M;
  }
  return res % M;
}
long long power(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1) res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
long long gcd(long long a, long long b) {
  if (b > a) return gcd(b, a);
  if (b == 0) return a;
  return gcd(b, a % b);
}
const int N = 2e5 + 5;
vector<int> adj[N];
long long a[N];
bool b[N], c[N];
vector<pair<int, int> > pp(N);
long long ans;
void dfs(int u, int par, long long mn) {
  if (b[u] == 1 && c[u] == 0)
    pp[u].first++;
  else if (b[u] == 0 && c[u] == 1)
    pp[u].second++;
  for (auto it : adj[u]) {
    if (it == par) continue;
    dfs(it, u, min(mn, a[u]));
    pp[u].first += pp[it].first;
    pp[u].second += pp[it].second;
  }
  if (a[u] < mn) {
    long long jj = min(pp[u].first, pp[u].second);
    ans += jj * 2 * a[u];
    pp[u].first -= jj;
    pp[u].second -= jj;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, -1, M);
  if (pp[1].first > 0 || pp[1].second > 0)
    cout << -1;
  else
    cout << ans;
  return 0;
}
