#include <bits/stdc++.h>
using namespace std;
class Node {
 public:
  long long cost, init_d, goal_d;
};
long long tot = 0;
pair<long long, long long> dfs1(vector<long long> graph[], long long vis[],
                                Node ar[], long long n, long long node,
                                long long val) {
  vis[node] = 1;
  pair<long long, long long> p = make_pair(0, 0);
  pair<long long, long long> p2;
  for (long long i = 0; i < graph[node].size(); i++) {
    if (!vis[graph[node][i]]) {
      p2 = dfs1(graph, vis, ar, n, graph[node][i], min(val, ar[node].cost));
      p.first += p2.first;
      p.second += p2.second;
    }
  }
  if (ar[node].goal_d == 0 and ar[node].init_d == 1)
    p.second++;
  else if (ar[node].goal_d == 1 and ar[node].init_d == 0)
    p.first++;
  long long x = min(p.first, p.second);
  if (val >= ar[node].cost) {
    p.first -= x;
    p.second -= x;
    tot += (2ll * x * ar[node].cost);
  }
  return p;
}
int main() {
  long long t;
  t = 1;
  while (t--) {
    long long n;
    cin >> n;
    Node ar[n];
    for (long long i = 0; i < n; i++)
      cin >> ar[i].cost >> ar[i].init_d >> ar[i].goal_d;
    long long u, v;
    vector<long long> graph[n];
    for (long long i = 0; i < n - 1; i++) {
      cin >> u >> v;
      u--, v--;
      graph[v].push_back(u);
      graph[u].push_back(v);
    }
    long long vis[n];
    memset(vis, 0, sizeof(vis));
    pair<long long, long long> p = dfs1(graph, vis, ar, n, 0, INT_MAX);
    if (p.first != 0 or p.second != 0)
      cout << "-1\n";
    else
      cout << tot << "\n";
    tot = 0;
  }
  return 0;
}
