#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector<int> adj[N];
long long mi[N], a[N], b[N], c[N], result;
pair<long long, long long> rem[N];
void dfs(int node, int par) {
  mi[node] = min(mi[par], a[node]);
  pair<long long, long long> ct = {0, 0};
  for (auto child : adj[node]) {
    if (child == par) continue;
    dfs(child, node);
    ct.first += rem[child].first;
    ct.second += rem[child].second;
  }
  if (b[node] > c[node]) {
    ct.first++;
  }
  if (b[node] < c[node]) {
    ct.second++;
  }
  if (mi[node] == a[node]) {
    int val = min(ct.first, ct.second);
    result += a[node] * 2 * val;
    ct.first -= val;
    ct.second -= val;
  }
  rem[node] = {ct.first, ct.second};
}
int main() {
  int n, ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    ans += b[i] - c[i];
  }
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  if (ans != 0) {
    cout << "-1\n";
    return 0;
  }
  mi[0] = 2e9;
  dfs(1, 0);
  cout << result << endl;
}
