#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
const int MAX = 2e5 + 10;
vector<int> a(MAX), b(MAX), c(MAX);
vector<vector<int>> g(MAX);
int par[MAX], cost[MAX];
void dfs1(int x) {
  if (!x)
    cost[x] = a[x];
  else
    cost[x] = min(a[x], cost[par[x]]);
  for (auto u : g[x])
    if (par[u] == -1) {
      par[u] = x;
      dfs1(u);
    }
}
bool v[MAX];
pair<pair<long long, int>, pair<int, int>> dfs2(int x) {
  int n1to0 = 0, n0to1 = 0;
  if (b[x] == 1 and c[x] == 0) n1to0++;
  if (b[x] == 0 and c[x] == 1) n0to1++;
  v[x] = true;
  long long here = 0;
  int changed = 0;
  for (auto u : g[x])
    if (!v[u]) {
      auto p = dfs2(u);
      n1to0 += p.second.first;
      n0to1 += p.second.second;
      changed += p.first.second;
      here += p.first.first;
    }
  long long aux = min(n1to0, n0to1);
  here += cost[x] * 2 * aux;
  return {{here, changed + 2 * aux}, {n1to0 - aux, n0to1 - aux}};
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int wrong1to0 = 0, wrong0to1 = 0;
  for (int i = 0; i < n; i++) {
    g[i].clear();
    cin >> a[i] >> b[i] >> c[i];
    if (b[i] == 1 and c[i] == 0) wrong1to0++;
    if (b[i] == 0 and c[i] == 1) wrong0to1++;
  }
  int x, y;
  for (int i = 0; i < n - 1; i++) {
    cin >> x >> y;
    g[x - 1].push_back(y - 1);
    g[y - 1].push_back(x - 1);
  }
  if (wrong1to0 != wrong0to1)
    cout << -1 << '\n';
  else {
    memset(par, -1, sizeof par);
    memset(v, false, sizeof v);
    dfs1(0);
    auto ans = dfs2(0);
    cout << ans.first.first << '\n';
  }
  return 0;
}
