#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e15;
const long long MAXN = 2e5 + 10;
long long arr[MAXN], brr[MAXN], crr[MAXN];
vector<long long> graph[MAXN];
long long num1[MAXN], num0[MAXN], numnot0[MAXN], numnot1[MAXN];
long long kek0[MAXN], kek1[MAXN], anss[MAXN];
void dfs(long long v, long long p, long long rel) {
  arr[v] = min(arr[v], rel);
  num0[v] = brr[v] == 0;
  num1[v] = brr[v] == 1;
  numnot0[v] = crr[v] == 0 && brr[v] != crr[v];
  numnot1[v] = crr[v] == 1 && brr[v] != crr[v];
  kek0[v] = 0;
  kek1[v] = 0;
  anss[v] = INF;
  for (long long u : graph[v]) {
    if (u == p) continue;
    dfs(u, v, arr[v]);
    num0[v] += num0[u];
    num1[v] += num1[u];
    numnot0[v] += numnot0[u];
    numnot1[v] += numnot1[u];
  }
}
void dfs2(long long v, long long p) {
  long long ans = 0;
  long long not0 = 0, not1 = 0;
  long long cnt = 0;
  for (long long u : graph[v]) {
    if (u == p) continue;
    cnt++;
    dfs2(u, v);
    ans += anss[u];
    if (ans > INF) ans = INF;
    not0 += kek0[u];
    not1 += kek1[u];
  }
  if (cnt == 0) {
    ans = 0;
  }
  not0 += crr[v] == 0 && brr[v] != crr[v];
  not1 += crr[v] == 1 && brr[v] != crr[v];
  long long k = min(not0, not1);
  ans += 2 * k * arr[v];
  if (ans > INF) ans = INF;
  not0 -= k;
  not1 -= k;
  if (numnot0[v] == numnot1[v]) {
    k = numnot0[v] + numnot1[v];
    long long ans2 = k * arr[v];
    if (ans2 <= ans) {
      ans = ans2;
      not0 = 0;
      not1 = 0;
    }
  }
  anss[v] = ans;
  kek0[v] = not0;
  kek1[v] = not1;
}
void solve() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; ++i) {
    cin >> arr[i] >> brr[i] >> crr[i];
  }
  for (long long i = 0; i < n - 1; ++i) {
    long long v, u;
    cin >> v >> u;
    v--, u--;
    graph[v].push_back(u);
    graph[u].push_back(v);
  }
  dfs(0, -1, INF);
  dfs2(0, -1);
  long long ans = anss[0];
  if (ans == INF || kek0[0] + kek1[0] > 0)
    cout << -1;
  else
    cout << ans;
  cout << '\n';
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
