#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1e5 + 5;
vector<long long> g[N];
long long a[N], b[N], c[N];
long long ans = 0;
pair<long long, long long> DFS(long long u, long long p) {
  if (p) a[u] = min(a[u], a[p]);
  pair<long long, long long> now = make_pair(0, 0);
  if (b[u] != c[u]) {
    if (b[u] == 1) now.first++;
    if (c[u] == 1) now.second++;
  }
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (v != p) {
      pair<long long, long long> aux = DFS(v, u);
      now.first += aux.first;
      now.second += aux.second;
    }
  }
  long long take = min(now.first, now.second);
  now.first -= take;
  now.second -= take;
  ans += 2 * take * a[u];
  return now;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, u, v;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  pair<long long, long long> aux = DFS(1, 0);
  if (aux.first || aux.second)
    cout << -1 << '\n';
  else
    cout << ans << '\n';
  return 0;
}
