#include <bits/stdc++.h>
using namespace std;
long long n, a[200007], b[200007], c[200007], z, h[200007], l[200007], r;
vector<long long> v[200007];
void S(long long x, long long y, long long m) {
  if (b[x] != c[x]) {
    if (b[x] == 0) {
      h[x]++;
    } else
      l[x]++;
  }
  for (int i = 0; i < v[x].size(); i++) {
    if (v[x][i] != y) {
      S(v[x][i], x, min(m, a[x]));
    }
  }
  if (a[x] <= m) {
    r += (min(h[x], l[x]) * 2 * a[x]);
    if (h[x] > l[x]) {
      h[x] -= l[x];
      l[x] = 0;
    } else {
      l[x] -= h[x];
      h[x] = 0;
    }
  }
  h[y] += h[x];
  l[y] += l[x];
  return;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    if (b[i] == 1) z++;
    if (c[i] == 1) z--;
  }
  for (int i = 1; i < n; i++) {
    long long l1, r1;
    cin >> l1 >> r1;
    v[l1].push_back(r1);
    v[r1].push_back(l1);
  }
  if (z != 0) {
    cout << -1 << endl;
  } else {
    S(1, 0, a[1]);
    cout << r << endl;
  }
  return 0;
}
