#include <bits/stdc++.h>
using namespace std;
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
const double pi = acos(-1.0);
const double eps = 1e-6;
const int mod = (int)1e9 + 7;
const int maxn = 200005;
const int logn = 20;
int a[maxn], b[maxn], c[maxn], n, m[maxn], cz[maxn], co[maxn];
vector<int> g[maxn];
long long res;
void dfs(int u, int p, int w) {
  m[u] = min(w, a[u]);
  if (b[u] != c[u]) {
    cz[u] = b[u] == 0;
    co[u] = b[u] == 1;
  }
  for (int v : g[u])
    if (v != p) {
      dfs(v, u, m[u]);
      cz[u] += cz[v];
      co[u] += co[v];
    }
  int ch = min(co[u], cz[u]);
  res += ch * 2 * 1LL * m[u];
  co[u] -= ch;
  cz[u] -= ch;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 1; i <= (n); i++) cin >> a[i] >> b[i] >> c[i];
  int z = 0, o = 0;
  for (int i = 1; i <= (n - 1); i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 1; i <= (n); i++) {
    if (b[i] != c[i]) {
      z += b[i] == 0;
      o += b[i] == 1;
    }
  }
  if (z != o) {
    cout << "-1";
    return 0;
  }
  dfs(1, 1, a[1]);
  cout << res << "\n";
  return 0;
}
