#include <bits/stdc++.h>
using namespace std;
int N;
long long int a[200010];
int b[200010], c[200010];
vector<int> graph[200010];
long long int res;
int need[200100][2], have[200010][2];
void dfs(int u, long long int minCost, int par = 0) {
  minCost = min(minCost, a[u]);
  if (b[u] != c[u]) need[u][c[u]]++, have[u][b[u]]++;
  for (int v : graph[u])
    if (v != par) {
      dfs(v, minCost, u);
      for (int k = ((int)(0)); k <= ((int)(1)); k++)
        need[u][k] += need[v][k], have[u][k] += have[v][k];
      for (int k = ((int)(0)); k <= ((int)(1)); k++)
        if (need[u][k] && have[u][k]) {
          long long int dif = min(need[u][k], have[u][k]);
          res += minCost * dif;
          need[u][k] -= dif;
          have[u][k] -= dif;
        }
    }
}
int main() {
  scanf("%d", &N);
  int bs = 0, cs = 0;
  for (int i = ((int)(1)); i <= ((int)(N)); i++) {
    scanf("%lld%d%d", &a[i], &b[i], &c[i]);
    bs += b[i];
    cs += c[i];
  }
  if (bs != cs) {
    puts("-1");
    return 0;
  }
  for (int i = ((int)(1)); i <= ((int)(N - 1)); i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  dfs(1, (long long int)1e18);
  printf("%lld\n", res);
  return (0);
}
