#include <bits/stdc++.h>
using namespace std;
vector<long long int> a, b, c;
long long int cost = 0;
pair<long long int, long long int> dfs(long long int child, long long int par,
                                       long long int mn,
                                       vector<long long int> graph[]) {
  pair<long long int, long long int> temp = {0, 0};
  if (b[child] != c[child]) {
    if (b[child] == 1) {
      temp.first++;
    } else {
      temp.second++;
    }
  }
  for (auto &it : graph[child]) {
    if (it == par) continue;
    pair<long long int, long long int> tmp =
        dfs(it, child, min(mn, a[child]), graph);
    temp.first += tmp.first;
    temp.second += tmp.second;
  }
  if (a[child] < mn) {
    long long int ct = min(temp.second, temp.first);
    cost += 2 * ct * a[child];
    temp.first -= ct;
    temp.second -= ct;
  }
  return temp;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  a.push_back(0);
  b.push_back(0);
  c.push_back(0);
  for (int i = 0; i < n; i++) {
    long long int temp;
    cin >> temp;
    a.push_back(temp);
    cin >> temp;
    b.push_back(temp);
    cin >> temp;
    c.push_back(temp);
  }
  vector<long long int> graph[n + 1];
  for (int i = 0; i < n - 1; i++) {
    long long int u, v;
    cin >> u >> v;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  long long int min = INT_MAX;
  pair<long long int, long long int> temp = dfs(1, 0, min, graph);
  if (temp.first || temp.second) {
    cout << -1 << "\n";
  } else {
    cout << cost << "\n";
  }
  return 0;
}
