#include <bits/stdc++.h>
using namespace std;
function<void(void)> ____ = []() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
};
const int MAXN = 2e5 + 7;
vector<int> G[MAXN];
int n, cnt[2], b[MAXN], c[MAXN], idx, st[MAXN], ed[MAXN];
long long int cost[MAXN];
set<int> S[2];
void dfs(int u, int par) {
  st[u] = ++idx;
  for (int v : G[u]) {
    if (v == par) continue;
    dfs(v, u);
  }
  ed[u] = idx;
}
int main() {
  ____();
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> cost[i] >> b[i] >> c[i];
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) cnt[b[i]]++, cnt[c[i]]--;
  if (cnt[0]) {
    cout << -1 << endl;
    return 0;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    if (b[i] ^ c[i]) {
      if (b[i])
        S[0].insert(st[i]);
      else
        S[1].insert(st[i]);
    }
  }
  vector<int> vec;
  for (int i = 1; i <= n; i++) vec.push_back(st[i]);
  sort(vec.begin(), vec.end(), [](int x, int y) { return cost[x] < cost[y]; });
  long long int ret = 0;
  for (int u : vec) {
    while (!S[0].empty()) {
      auto p0 = S[0].lower_bound(st[u]);
      auto p1 = S[1].lower_bound(st[u]);
      if (p0 == S[0].end() or p1 == S[1].end() or *p0 > ed[u] or *p1 > ed[u])
        break;
      ret += 2 * cost[u];
      S[0].erase(p0);
      S[1].erase(p1);
    }
  }
  cout << ret << endl;
  return 0;
}
