#include <bits/stdc++.h>
long long int mod = 1000000000 + 7;
long long int inf = (long long int)(1e18);
using namespace std;
mt19937 unlucko(chrono::steady_clock::now().time_since_epoch().count());
vector<int> a(int(2e5)), b(int(2e5)), c(int(2e5)), tree[int(2e5)];
priority_queue<int> from, to;
int t = 0, Time[int(2e5)];
long long int ans = 0;
void dfs(int node, int pnode) {
  a[node] = min(a[node], a[pnode]);
  ++t;
  Time[node] = t;
  if (c[node] != b[node]) {
    if (!c[node])
      from.push(t);
    else
      to.push(t);
  }
  for (auto it : tree[node]) {
    if (it == pnode) continue;
    dfs(it, node);
  }
  while (!from.empty() && !to.empty() && from.top() >= Time[node] &&
         to.top() >= Time[node]) {
    from.pop();
    to.pop();
    ans += 2LL * a[node];
  }
}
int main() {
  iostream::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int d[2]{};
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    d[b[i]]++;
    d[c[i]]--;
  }
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    --x;
    y--;
    tree[x].push_back(y);
    tree[y].push_back(x);
  }
  if (d[0] || d[1]) {
    cout << "-1\n";
    return 0;
  }
  dfs(0, 0);
  cout << ans;
}
