#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const double pi = acos(-1);
const int N = 2e5 + 10;
vector<long long> v, cost, vis;
vector<vector<long long>> g;
pair<long long, pair<long long, long long>> dfs(long long abhi,
                                                long long abhi_tak) {
  if (vis[abhi]) return {0, {0, 0}};
  vis[abhi] = 1;
  pair<long long, pair<long long, long long>> ans;
  abhi_tak = min(abhi_tak, cost[abhi]);
  for (auto i : g[abhi]) {
    auto q = dfs(i, abhi_tak);
    ans.first += q.first;
    ans.second.first += q.second.first;
    ans.second.second += q.second.second;
  }
  if (v[abhi] == -1) ans.second.second++;
  if (v[abhi] == 1) ans.second.first++;
  long long w = min(ans.second.first, ans.second.second);
  ans.first += 2 * abhi_tak * w;
  ans.second.first -= w;
  ans.second.second -= w;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long n;
  cin >> n;
  vis.resize(n + 1);
  g.resize(n + 1);
  v.resize(n + 1);
  cost.resize(n + 1);
  for (long long i = 1; i < n + 1; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    cost[i] = a;
    if (b > c)
      v[i] = 1;
    else if (c > b)
      v[i] = -1;
    else
      v[i] = 0;
  }
  for (long long i = 1; i < n; i++) {
    long long u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  auto ans = dfs(1, cost[1]);
  if (ans.second.second != 0 or ans.second.first != 0) return cout << -1, 0;
  cout << ans.first;
}
