#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const long double PI = 3.1415926535898;
const long long LLMAX = LLONG_MAX;
int a[(int)2e5 + 5], b[(int)2e5 + 5], c[(int)2e5 + 5];
int n;
vector<vector<int> > v((int)2e5 + 5);
int type10 = 0;
int type01 = 0;
long long ans = 0;
int one[(int)2e5 + 5] = {0};
int zero[(int)2e5 + 5] = {0};
void solve(int parent, int node) {
  if (b[node] != c[node]) {
    if (b[node])
      one[node]++;
    else
      zero[node]++;
  }
  if (parent) a[node] = min(a[node], a[parent]);
  for (int i = 0; i < v[node].size(); i++) {
    if (v[node][i] == parent)
      continue;
    else {
      solve(node, v[node][i]);
      one[node] += one[v[node][i]];
      zero[node] += zero[v[node][i]];
    }
  }
  int dec = min(one[node], zero[node]);
  one[node] -= dec;
  zero[node] -= dec;
  ans += (long long)a[node] * 2 * dec;
}
int main() {
  int x, y;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    if (b[i] == 0 && c[i] == 1)
      type01++;
    else if (b[i] == 1 && c[i] == 0)
      type10++;
  }
  for (int i = 1; i < n; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  solve(0, 1);
  if (type01 != type10) {
    cout << -1 << "\n";
  } else {
    cout << ans << "\n";
  }
}
