#include <bits/stdc++.h>
using namespace std;
vector<int> a, b, c;
const int N = 200005;
vector<int> adj[N];
long long res = 0;
pair<int, int> dfs(int u, int parent, int mini) {
  int onezero = 0;
  int zerone = 0;
  mini = min(mini, a[u]);
  if (b[u] == 1 && c[u] == 0) onezero++;
  if (b[u] == 0 && c[u] == 1) zerone++;
  for (auto v : adj[u]) {
    if (v == parent) continue;
    auto act = dfs(v, u, mini);
    onezero += act.first;
    zerone += act.second;
  }
  int contri = min(onezero, zerone);
  res += 1LL * contri * mini * 2;
  onezero -= contri;
  zerone -= contri;
  return make_pair(onezero, zerone);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int ones = 0;
  for (int i = 0; i < n; i++) {
    int aa, bb, cc;
    cin >> aa >> bb >> cc;
    if (bb == 1) ones++;
    if (cc == 1) ones--;
    a.push_back(aa), b.push_back(bb), c.push_back(cc);
  }
  if (ones != 0) {
    cout << -1 << "\n";
    return 0;
  }
  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);
  }
  dfs(0, -1, INT_MAX);
  cout << res << "\n";
  return 0;
}
