#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct quad {
  int pos;
  long long weight;
  int now;
  int target;
};
vector<vector<int>> adj;
vector<quad> desc;
map<int, int> vis;
long long cost;
bool compare(quad a, quad b) { return a.weight <= b.weight; }
pair<int, int> dfs(int u, int mincost) {
  vis[u] = 1;
  int c0 = 0, c1 = 0;
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i];
    if (vis[v] == 0) {
      int thiscost = desc[v].weight;
      pair<int, int> p = dfs(v, min(mincost, thiscost));
      c0 += p.first;
      c1 += p.second;
    }
  }
  if (desc[u].now != desc[u].target and desc[u].target == 0) {
    c0++;
  } else if (desc[u].now != desc[u].target and desc[u].target == 1) {
    c1++;
  }
  if (desc[u].weight == mincost) {
    int c = min(c0, c1);
    cost += c * desc[u].weight * 2;
    c0 -= c;
    c1 -= c;
  }
  return make_pair(c0, c1);
}
int main() {
  int n;
  cin >> n;
  cost = 0;
  for (int i = 0; i < n; i++) {
    long long a;
    int b, c;
    cin >> a >> b >> c;
    quad q = {i, a, b, c};
    desc.push_back(q);
  }
  adj.resize(n, vector<int>());
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  pair<int, int> p = dfs(0, desc[0].weight);
  if (p.first + p.second > 0) {
    cout << -1;
  } else {
    cout << cost;
  }
}
