#include <bits/stdc++.h>
using namespace std;
vector<long long int> tree[200005];
bool visited[200005];
void dfs(vector<long long int> &a, long long int u) {
  visited[u] = true;
  for (auto v : tree[u]) {
    if (visited[v]) continue;
    a[v] = min(a[v], a[u]);
    dfs(a, v);
  }
}
pair<long long int, long long int> fixNodes(vector<long long int> &a,
                                            vector<long long int> &b,
                                            vector<long long int> &c,
                                            long long int u,
                                            long long int &cost) {
  visited[u] = true;
  long long int totalZeroLeftNodes = 0, totalOneLeftNodes = 0;
  for (auto v : tree[u]) {
    if (visited[v]) continue;
    pair<long long int, long long int> nodesLeft = fixNodes(a, b, c, v, cost);
    totalZeroLeftNodes += nodesLeft.first;
    totalOneLeftNodes += nodesLeft.second;
  }
  if (b[u] != c[u]) {
    if (b[u] == 0)
      totalZeroLeftNodes++;
    else
      totalOneLeftNodes++;
  }
  long long int x = min(totalZeroLeftNodes, totalOneLeftNodes);
  cost += (2 * x * a[u]);
  return make_pair(totalZeroLeftNodes - x, totalOneLeftNodes - x);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int testcases = 1;
  while (testcases--) {
    long long int n;
    cin >> n;
    vector<long long int> a(n), b(n), c(n);
    long long int i;
    for (i = 0; i < n; i++) {
      cin >> a[i] >> b[i] >> c[i];
    }
    for (i = 0; i < n - 1; i++) {
      long long int x, y;
      cin >> x >> y;
      x--;
      y--;
      tree[x].push_back(y);
      tree[y].push_back(x);
    }
    memset(visited, 0, sizeof(visited));
    dfs(a, 0);
    memset(visited, 0, sizeof(visited));
    long long int ans = 0;
    pair<long long int, long long int> nodesLeft = fixNodes(a, b, c, 0, ans);
    cout << (nodesLeft.first == 0 && nodesLeft.second == 0 ? ans : -1) << endl;
  }
}
