#include <bits/stdc++.h>
using namespace std;
void INPUT() {}
const int N = 200005;
vector<long long> v[N];
class ok {
 public:
  long long c, old, neww;
};
long long tot, n;
pair<long, long> dfs(vector<ok>& cost, long long i, vector<bool>& vis,
                     long long parent) {
  vis[i] = true;
  if (v[i].size() == 1 and v[i][0] == parent) {
    pair<int, int> a;
    a.first = a.second = 0;
    if (cost[i].old == 0 and cost[i].neww == 1) a.second++;
    if (cost[i].old == 1 and cost[i].neww == 0) a.first++;
    return a;
  }
  cost[i].c = min(cost[i].c, cost[parent].c);
  long long bad1 = 0, bad2 = 0;
  pair<long, long> left;
  long long siz = v[i].size();
  for (int j = 0; j <= siz - 1; j++) {
    if (!vis[v[i][j]]) {
      left = dfs(cost, v[i][j], vis, i);
      bad1 += left.first;
      bad2 += left.second;
    }
  }
  if (cost[i].old == 0 and cost[i].neww == 1) bad2++;
  if (cost[i].old == 1 and cost[i].neww == 0) bad1++;
  if (bad1 >= bad2 and bad2 != 0) {
    tot += cost[i].c * bad2 * 2LL;
    bad1 -= bad2;
    bad2 = 0;
  } else if (bad2 >= bad1 and bad1 != 0) {
    tot += cost[i].c * bad1 * 2LL;
    bad2 -= bad1;
    bad1 = 0;
  }
  return make_pair(bad1, bad2);
}
void solve() {
  cin >> n;
  vector<ok> cost(n + 1);
  long long x, y, z;
  int bad1 = 0, bad2 = 0;
  for (int i = 1; i <= n; i++) {
    cin >> cost[i].c >> cost[i].old >> cost[i].neww;
    if (cost[i].old == 0 and cost[i].neww == 1) bad2++;
    if (cost[i].old == 1 and cost[i].neww == 0) bad1++;
  }
  for (int i = 1; i <= n - 1; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  tot = 0;
  if (bad1 != bad2) {
    cout << -1 << "\n";
    return;
  }
  vector<bool> vis(n + 1, false);
  dfs(cost, 1, vis, 1);
  cout << tot << "\n";
  for (int i = 0; i <= n; i++) v[i].clear();
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  INPUT();
  int t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
