#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9 + 7;
const long long mod = 1e9 + 7;
const int N = 2e5 + 5, M = 1e5 + 5;
long long T, m, q, k;
bool ok;
string s, t;
long long a[N];
bool c[N], b[N];
long long ans = 0;
long long need[N][2];
vector<int> adj[N];
void dfs(int u, int par, long long mn) {
  for (int v : adj[u]) {
    if (v == par) continue;
    dfs(v, u, min(mn, a[v]));
    need[u][0] += need[v][0];
    need[u][1] += need[v][1];
  }
  if (a[u] == mn) {
    long long take = min(need[u][0], need[u][1]);
    need[u][0] -= take;
    need[u][1] -= take;
    ans += 2 * a[u] * take;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    adj[i].clear();
    need[i][0] = need[i][1] = 0;
  }
  ans = 0;
  vector<int> cnt(2, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    cnt[b[i]]--, cnt[c[i]]++;
    if (b[i] != c[i]) {
      need[i][c[i]]++;
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  if (cnt[0] != 0) {
    cout << "-1\n";
    return 0;
  }
  dfs(0, -1, a[0]);
  cout << ans << "\n";
}
