#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream& operator<<(ostream& cout, vector<T> V) {
  cout << "[ ";
  for (auto v : V) cout << v << ' ';
  return cout << ']';
}
template <class L, class R>
ostream& operator<<(ostream& cout, pair<L, R> P) {
  return cout << '(' << P.first << ',' << P.second << ')';
}
long long int n;
vector<long long int> a, b, c;
vector<vector<long long int>> conn;
vector<long long int> c0, c1;
long long int ans = 0;
void dfs1(long long int node, long long int par, long long int val) {
  a[node] = min(a[node], val);
  for (long long int i : conn[node]) {
    if (i == par) continue;
    dfs1(i, node, a[node]);
  }
}
void dfs2(long long int node, long long int par) {
  for (long long int i : conn[node]) {
    if (i == par) continue;
    dfs2(i, node);
    c0[node] += c0[i];
    c1[node] += c1[i];
  }
  if (b[node] == 0 && c[node] == 1) {
    c0[node]++;
  }
  if (b[node] == 1 && c[node] == 0) {
    c1[node]++;
  }
}
long long int dfs3(long long int node, long long int par) {
  long long int ct0 = 0, ct1 = 0;
  if (b[node] == 0 && c[node] == 1) ct0 = 1;
  if (b[node] == 1 && c[node] == 0) ct1 = 1;
  for (long long int i : conn[node]) {
    if (i == par) continue;
    long long int temp = dfs3(i, node);
    if (temp > 0) {
      ct0 += temp;
    } else {
      ct1 -= temp;
    }
  }
  long long int m = min(ct0, ct1);
  ans += a[node] * m;
  ct0 -= m;
  ct1 -= m;
  if (ct0) return ct0;
  return -ct1;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  a.resize(n);
  b.resize(n);
  c.resize(n);
  conn.resize(n);
  for (long long int i = 0; i < (n); i++) cin >> a[i] >> b[i] >> c[i];
  for (long long int i = 0; i < (n - 1); i++) {
    long long int x;
    cin >> x;
    long long int y;
    cin >> y;
    x--, y--;
    conn[x].push_back(y);
    conn[y].push_back(x);
  }
  dfs1(0, -1, (long long int)1e18);
  c0.resize(n);
  c1.resize(n);
  dfs2(0, -1);
  if (dfs3(0, -1)) {
    cout << -1 << '\n';
  } else {
    cout << 2 * ans << '\n';
  }
  cerr << "Time : " << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC
       << "ms\n";
  ;
}
