#include <bits/stdc++.h>
using namespace std;
vector<vector<int64_t>> g;
vector<int64_t> h;
vector<int64_t> used;
vector<int64_t> a;
vector<int64_t> cur;
vector<int64_t> need;
void dfs(int64_t v) {
  used[v] = 1;
  for (auto u : g[v]) {
    if (used[u] == 0) {
      h[u] = min(h[v], a[u]);
      dfs(u);
    }
  }
}
int sign(int a) {
  if (a >= 0)
    return 1;
  else
    return 0;
}
pair<int64_t, int64_t> dfs2(int64_t v) {
  used[v] = 1;
  pair<int64_t, int64_t> ans = {0, 0};
  for (auto u : g[v]) {
    if (used[u] == 0) {
      pair<int64_t, int64_t> y = dfs2(u);
      ans.first += y.first;
      if (sign(ans.second) != sign(y.second)) {
        ans.first += 2 * h[v] * min(abs(y.second), abs(ans.second));
        ans.second += y.second;
      } else {
        ans.second += y.second;
      }
    }
  }
  if (cur[v] != need[v]) {
    if (cur[v] == 0) {
      if (ans.second < 0) {
        ans.first += 2 * h[v];
        ans.second++;
      } else {
        ans.second++;
      }
      return ans;
    } else {
      if (ans.second > 0) {
        ans.first += 2 * h[v];
        ans.second--;
      } else {
        ans.second--;
      }
      return ans;
    }
  }
  return ans;
}
int main() {
  int64_t n;
  cin >> n;
  h.resize(n);
  used.resize(n);
  a.resize(n);
  g.resize(n);
  need.resize(n);
  cur.resize(n);
  vector<bool> ok(n);
  for (int64_t i = 0; i < n; ++i) {
    cin >> a[i];
    int64_t x, y;
    cin >> x >> y;
    cur[i] = x;
    need[i] = y;
    ok[i] = (x != y);
  }
  for (int64_t i = 0; i < n; ++i) h[i] = a[0];
  for (int64_t i = 0; i < n - 1; ++i) {
    int64_t u, v;
    cin >> u >> v;
    --u, --v;
    g[v].push_back(u);
    g[u].push_back(v);
  }
  dfs(0);
  used = vector<int64_t>(n);
  pair<int64_t, int64_t> z = dfs2(0);
  if (z.second != 0) {
    cout << -1;
  } else
    cout << z.first;
}
