#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
long long n, b[N], c[N], needZero[N], needOne[N], sumNeedZero[N], sumNeedOne[N],
    seg[4 * N], sp[N][20];
long long spMn[N][20], in[N], out[N], tim, lg = 20;
vector<int> adj[N];
pair<long long, long long> a[N];
void dfs(int node, int p) {
  in[node] = ++tim;
  if (b[node] == 0 && c[node] == 1)
    needOne[node] = 1;
  else if (b[node] == 1 && c[node] == 0)
    needZero[node] = 1;
  for (int i = 0; i < lg; i++) spMn[node][i] = 1e18;
  sp[node][0] = p;
  spMn[node][0] = a[p].first;
  for (int i = 1; i < lg; i++) {
    int tmp = sp[node][i - 1];
    sp[node][i] = sp[tmp][i - 1];
    spMn[node][i] = min(spMn[node][i - 1], spMn[tmp][i - 1]);
  }
  for (auto ch : adj[node]) {
    if (ch != p) {
      dfs(ch, node);
      needOne[node] += needOne[ch];
      needZero[node] += needZero[ch];
    }
  }
  out[node] = tim;
}
void update(int node, int st, int en, int idx, long long val) {
  if (st == en) {
    seg[node] += val;
  } else {
    int mid = (st + en) / 2;
    if (st <= idx && idx <= mid)
      update(2 * node, st, mid, idx, val);
    else
      update(2 * node + 1, mid + 1, en, idx, val);
    seg[node] = seg[2 * node] + seg[2 * node + 1];
  }
}
long long query(int node, int st, int en, int l, int r) {
  if (r < st || l > en) return 0;
  if (l <= st && en <= r) return seg[node];
  int mid = (st + en) / 2;
  return query(2 * node, st, mid, l, r) +
         query(2 * node + 1, mid + 1, en, l, r);
}
long long solve(long long l, long long r, long long node, long long cost) {
  long long tmp = min(needZero[node], needOne[node]) - query(1, 1, n, l, r);
  update(1, 1, n, in[node], tmp);
  return tmp * 2 * cost;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].first >> b[i] >> c[i];
    a[i].second = i;
  }
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y), adj[y].push_back(x);
  }
  dfs(1, 1);
  if (needZero[1] != needOne[1]) return cout << "-1\n", 0;
  sort(a + 1, a + n + 1);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int node = a[i].second;
    if (spMn[node][lg - 1] <= a[i].first && node != 1) continue;
    long long val = solve(in[node], out[node], node, a[i].first);
    ans += val;
  }
  cout << ans << '\n';
  return 0;
}
