#include <bits/stdc++.h>
using namespace std;
template <typename T, typename S>
ostream& operator<<(ostream& output, const pair<T, S>& to_print) {
  output << to_print.first << ":" << to_print.second;
  return output;
}
template <typename T, typename S>
ostream& operator<<(ostream& output, const map<T, S>& to_print) {
  for (typename map<T, S>::const_iterator it = to_print.begin();
       it != to_print.end(); it++)
    cout << *it << endl;
  return output;
}
template <typename T>
ostream& operator<<(ostream& output, const vector<T>& to_print) {
  for (typename vector<T>::const_iterator it = to_print.begin();
       it != to_print.end(); it++)
    cout << *it << " ";
  return output;
}
struct info {
  int a;
  bool b, c;
  info() {}
  info(int a_inp, bool b_inp, bool c_inp) : a(a_inp), b(b_inp), c(c_inp) {}
};
vector<int> adj[200010];
bool visited[200010];
info info_inp[200010];
int dp[200010];
int n;
long long ans = 0;
pair<int, int> combine(pair<int, int> one, pair<int, int> two, int s) {
  pair<int, int> to_return =
      make_pair(one.first + two.first, one.second + two.second);
  long long minimum = min(to_return.first, to_return.second);
  ans += 2 * minimum * dp[s];
  to_return.first -= minimum;
  to_return.second -= minimum;
  return to_return;
}
pair<int, int> dfs(int s, int prev) {
  visited[s] = true;
  dp[s] = min(dp[prev], dp[s]);
  pair<int, int> to_return;
  if (info_inp[s].b == 1 && info_inp[s].c == 0)
    to_return = make_pair(0, 1);
  else if (info_inp[s].b == 0 && info_inp[s].c == 1)
    to_return = make_pair(1, 0);
  else
    to_return = make_pair(0, 0);
  for (int i = 0; i < adj[s].size(); i++) {
    int to = adj[s][i];
    if (!visited[to]) {
      pair<int, int> returned = dfs(to, s);
      to_return = combine(returned, to_return, s);
    }
  }
  return to_return;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    bool b, c;
    cin >> a >> b >> c;
    info_inp[i + 1] = info(a, b, c);
    dp[i + 1] = a;
  }
  dp[0] = (int)2e9;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  pair<int, int> returned = dfs(1, 0);
  if (returned.first || returned.second)
    cout << "-1" << endl;
  else
    cout << ans << endl;
  return 0;
}
