#include <bits/stdc++.h>
using namespace std;
int mod = (int)1e9 + 7;
vector<int> cost, b, c, minCost;
vector<bool> visited;
vector<int> mismatch0, mismatch1;
long long res = 0;
vector<vector<int> > adj;
void dfs(int node) {
  visited[node] = true;
  if (c[node] == 0)
    mismatch0[node] += (b[node] == 1);
  else
    mismatch1[node] += (b[node] == 0);
  for (auto &cur : adj[node]) {
    if (!visited[cur]) {
      minCost[cur] = min(minCost[node], cost[cur]);
      dfs(cur);
      mismatch0[node] += mismatch0[cur];
      mismatch1[node] += mismatch1[cur];
    }
  }
  int take = min(mismatch0[node], mismatch1[node]);
  res += 2ll * minCost[node] * take;
  mismatch0[node] -= take;
  mismatch1[node] -= take;
}
void solve() {
  int n;
  cin >> n;
  cost.assign(n, 0);
  b.assign(n, 0);
  c.assign(n, 0);
  minCost.assign(n, 0);
  visited.assign(n, false);
  mismatch0.assign(n, 0);
  mismatch1.assign(n, 0);
  adj.assign(n, vector<int>());
  for (int i = 0; i < n; i++) cin >> cost[i] >> b[i] >> c[i];
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  minCost[0] = cost[0];
  dfs(0);
  if (mismatch0[0] || mismatch1[0]) res = -1;
  cout << res << "\n";
}
int main() {
  cin.tie(0), cout.tie(0)->sync_with_stdio(0);
  solve();
  return 0;
}
