#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> adjl[200003];
long long a[200003], ans = 0;
bool b[200003], c[200003];
pair<long long, long long> extra[200003];
pair<long long, long long> dfs(int now, int prev) {
  if (c[now] == 1 && b[now] == 0)
    extra[now] = {1, 0};
  else if (c[now] == 0 && b[now] == 1)
    extra[now] = {0, 1};
  else
    extra[now] = {0, 0};
  for (int next : adjl[now]) {
    if (next == prev) continue;
    a[next] = min(a[next], a[now]);
    auto tmp = dfs(next, now);
    extra[now].first += tmp.first;
    extra[now].second += tmp.second;
  }
  long long use = min(extra[now].first, extra[now].second);
  ans += 2 * use * a[now];
  extra[now].first -= use;
  extra[now].second -= use;
  return extra[now];
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i] >> b[i] >> c[i];
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    adjl[u].push_back(v);
    adjl[v].push_back(u);
  }
  dfs(1, 0);
  if (extra[1].first + extra[1].second > 0 &&
      extra[1].first != extra[1].second) {
    cout << -1 << "\n";
    return 0;
  }
  cout << ans << "\n";
}
