#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
const int maxn = 2e5 + 5;
vector<long long> gr[maxn], b(maxn), c(maxn), a(maxn);
pair<long long, long long> dfs(long long v, long long p) {
  pair<long long, long long> x = {0, 0};
  if (b[v] == 1 && c[v] == 0) x.first++;
  if (b[v] == 0 && c[v] == 1) x.second++;
  for (auto u : gr[v]) {
    if (u == p) continue;
    a[u] = min(a[u], a[v]);
    pair<long long, long long> y = dfs(u, v);
    x.first += y.first;
    x.second += y.second;
  }
  long long m = min(x.first, x.second);
  ans += m * 2LL * a[v];
  x.first -= m;
  x.second -= m;
  return x;
}
int main() {
  long long 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++) {
    long long x, y;
    cin >> x >> y;
    gr[x].push_back(y);
    gr[y].push_back(x);
  }
  pair<long long, long long> z = dfs(1, 1);
  if (z.first != 0 || z.second != 0) {
    cout << -1;
    return 0;
  }
  cout << ans;
}
