#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g;
vector<bool> b, c;
vector<int> a, t1, t2;
void build(int v, int p = -1) {
  for (auto to : g[v]) {
    if (to != p) {
      a[to] = min(a[to], a[v]);
      build(to, v);
      t1[v] += t1[to];
      t2[v] += t2[v];
    }
  }
}
long long ans = 0;
int dfs(int v, int p = -1) {
  if (g[v].size() == 1 && p != -1) {
    return t1[v] - t2[v];
  }
  int sum1 = 0, sum2 = 0;
  for (auto to : g[v]) {
    if (to != p) {
      int dif = dfs(to, v);
      if (dif > 0)
        sum1 += dif;
      else
        sum2 += (-dif);
    }
  }
  if (b[v] && !c[v]) sum1++;
  if (!b[v] && c[v]) sum2++;
  ans += 1ll * a[v] * min(sum1, sum2);
  return sum1 - sum2;
}
signed main() {
  std::ios_base::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  g.resize(n);
  a.resize(n);
  t1.resize(n);
  t2.resize(n);
  b.resize(n);
  c.resize(n);
  int c1 = 0, c2 = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    int b1, b2;
    cin >> b1 >> b2;
    if (b1 && !b2) c1++, t1[i] = 1;
    if (!b1 && b2) c2++, t2[i] = 1;
    b[i] = bool(b1), c[i] = bool(b2);
  }
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  build(0);
  if (c1 != c2) {
    cout << -1;
    return 0;
  }
  dfs(0);
  cout << 2 * ans;
  return 0;
}
