#include <bits/stdc++.h>
using namespace std;
const int N = 200000, M = N - 1, INF = 0x3f3f3f3f;
int oo[1 + M * 2], oj[1 + M * 2];
int link(int o, int j) {
  static int _ = 1;
  oo[_] = o, oj[_] = j;
  return _++;
}
int ae[N], aa[N], xx[N];
long long ans;
int dfs(int p, int i, int a) {
  a = min(a, aa[i]);
  int x = xx[i], pos = 0, neg = 0;
  if (x > 0)
    pos += x;
  else if (x < 0)
    neg -= x;
  for (int o = ae[i]; o; o = oo[o]) {
    int j = oj[o];
    if (j != p) {
      x = dfs(i, j, a);
      if (x > 0)
        pos += x;
      else if (x < 0)
        neg -= x;
    }
  }
  ans += (long long)a * 2 * min(pos, neg);
  return pos - neg;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    aa[i] = a, xx[i] = b - c;
  }
  int x = 0;
  for (int i = 0; i < n; i++) x += xx[i];
  if (x) {
    cout << "-1\n";
    return 0;
  }
  for (int h = 0; h < n - 1; h++) {
    int i, j;
    cin >> i >> j, i--, j--;
    ae[i] = link(ae[i], j);
    ae[j] = link(ae[j], i);
  }
  dfs(-1, 0, INF);
  cout << ans << '\n';
  return 0;
}
