#include <bits/stdc++.h>
using namespace std;
const long long M = 2e5 + 10, M2 = 1e6 + 10, oo = 1e9 + 7;
vector<long long> adj[M], va(M, 0), vb(M, 0), vc(M, 0), zrs(M, 0), ons(M, 0),
    vis(M, 0), pr(M, 0);
void pdfs(int u, int p) {
  pr[u] = p;
  for (auto v : adj[u]) {
    if (v == p) continue;
    pdfs(v, u);
  }
}
void dfs(int u, int p) {
  vis[u] = 1;
  zrs[u] = (not vb[u] && vb[u] != vc[u]);
  ons[u] = (vb[u] && vb[u] != vc[u]);
  for (auto v : adj[u]) {
    if (v == p) continue;
    if (vis[v]) {
      zrs[u] += max(zrs[v] - ons[v], 0LL);
      ons[u] += max(ons[v] - zrs[v], 0LL);
    } else if (not vis[v]) {
      dfs(v, u);
      zrs[u] += zrs[v];
      ons[u] += ons[v];
    }
  }
}
vector<pair<long long, long long> > vp;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> va[i] >> vb[i] >> vc[i];
    vp.push_back(make_pair(va[i], i));
  }
  for (int i = 0, u, v; i < n - 1; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  pdfs(1, 0);
  sort(vp.begin(), vp.end());
  long long ans = 0;
  for (auto p : vp) {
    if (vis[p.second]) continue;
    dfs(p.second, pr[p.second]);
    ans += 2 * min(zrs[p.second], ons[p.second]) * p.first;
  }
  if (zrs[1] != ons[1]) ans = -1;
  cout << ans << endl;
  return 0;
}
