#include <bits/stdc++.h>
using namespace std;
template <typename... T>
void pp(T... args) {
  ((cout << args << " "), ...);
  cout << "\n";
}
template <typename... T>
void po(T... args) {
  ((cout << args << " "), ...);
  cout << "";
}
long long int n, a[200005], b[200005], c[200005], mini[200005], ans;
vector<int> g[200005];
pair<int, int> dfs(int cur, int par) {
  int req[2] = {0, 0};
  if (b[cur] != c[cur]) {
    req[b[cur]]++;
  }
  for (long long int i = 0; i < g[cur].size(); i++) {
    int k = g[cur][i];
    if (k == par) continue;
    mini[k] = min(a[k], mini[cur]);
    pair<int, int> rem = dfs(k, cur);
    req[rem.first] += rem.second;
  }
  int x = min(req[0], req[1]);
  req[0] -= x;
  req[1] -= x;
  ans += 2LL * mini[cur] * x;
  if (req[0] == 0 && req[1] == 0) {
    return {0, 0};
  } else if (par == -1)
    ans = -1;
  if (req[0] > 0) {
    return {0, req[0]};
  } else {
    return {1, req[1]};
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int test = 1;
  for (int tt = 1; tt <= test; tt++) {
    cin >> n;
    for (long long int i = 0; i < n; i++) {
      cin >> a[i] >> b[i] >> c[i];
    }
    for (long long int i = 0; i < n - 1; i++) {
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      g[x].push_back(y);
      g[y].push_back(x);
    }
    mini[0] = a[0];
    dfs(0, -1);
    pp(ans);
  }
  return 0;
}
