#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, mod = 1e9 + 7;
int a[N], b[N], c[N], mn[N], bad[N][2];
vector<int> g[N];
long long ans = 0;
void dfs(int v, int p) {
  mn[v] = a[v];
  mn[v] = min(mn[v], mn[p]);
  if (b[v] != c[v]) bad[v][b[v]]++;
  for (int u : g[v])
    if (u != p) {
      dfs(u, v);
      bad[v][0] += bad[u][0];
      bad[v][1] += bad[u][1];
    }
  if (mn[v] == a[v]) {
    ans += 2ll * min(bad[v][0], bad[v][1]) * a[v];
    int mn = min(bad[v][0], bad[v][1]);
    bad[v][0] -= mn;
    bad[v][1] -= mn;
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(0, 0);
  if (bad[0][0] + bad[0][1])
    cout << -1;
  else
    cout << ans;
  return 0;
}
