#include <bits/stdc++.h>
using namespace std;
#pragma GCC push_options
#pragma GCC optimize("unroll-loops")
vector<vector<int>> edges;
vector<long long> val;
vector<long long> cost;
vector<int> vis;
pair<long long, pair<int, int>> dfs(long long cur, long long ashu) {
  if (vis[cur]) return {0, {0, 0}};
  vis[cur] = 1;
  pair<long long, pair<int, int>> res;
  ashu = min(ashu, cost[cur]);
  for (int i : edges[cur]) {
    auto temp = dfs(i, ashu);
    res.first += temp.first;
    res.second.first += temp.second.first;
    res.second.second += temp.second.second;
  }
  if (val[cur] == -1) {
    res.second.second++;
  }
  if (val[cur] == 1) {
    res.second.first++;
  }
  int resolve = min(res.second.first, res.second.second);
  res.first += ashu * resolve * 2;
  res.second.first -= resolve;
  res.second.second -= resolve;
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  edges.resize(n + 1);
  val.resize(n + 1);
  vis.resize(n + 1);
  cost.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    cost[i] = a;
    if (b > c)
      val[i] = 1;
    else if (c > b)
      val[i] = -1;
    else
      val[i] = 0;
  }
  for (int i = 1; i <= n - 1; i++) {
    int one, two;
    cin >> one >> two;
    edges[one].emplace_back(two);
    edges[two].emplace_back(one);
  }
  auto res = dfs(1, cost[1]);
  if (res.second.second != 0 || res.second.first != 0) {
    cout << -1;
    return 0;
  }
  cout << res.first << "\n";
  return 0;
}
