#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int sz = 2e5 + 10;
int t;
int n, x;
vector<int> G[sz];
int ar[sz];
int br[sz];
int cr[sz];
struct node {
  int tz, to;
  int cz, co;
  ll ans;
  node() {
    tz = 0;
    to = 0;
    cz = 0;
    co = 0;
    ans = 0;
  }
  node operator+(const node& rhs) {
    node h;
    h.tz = this->tz + rhs.tz;
    h.to = this->to + rhs.to;
    h.cz = this->cz + rhs.cz;
    h.co = this->co + rhs.co;
    h.ans = this->ans + rhs.ans;
    return h;
  }
  void clear() {
    tz = 0;
    to = 0;
    cz = 0;
    co = 0;
    ans = 0;
  }
} dr[sz];
void change(int u) {
  int mn = min(dr[u].tz, dr[u].cz);
  mn = min(dr[u].to, dr[u].co);
  dr[u].tz -= mn;
  dr[u].to -= mn;
  dr[u].cz -= mn;
  dr[u].co -= mn;
  dr[u].ans += (1ll * mn * ar[u]);
  dr[u].ans += (1ll * mn * ar[u]);
}
void dfs(int u, int pr = -1, int cost = INT_MAX) {
  if (br[u] != cr[u]) {
    dr[u].to = (cr[u] == 0);
    dr[u].tz = (cr[u] == 1);
    dr[u].co = (br[u] == 0);
    dr[u].cz = (br[u] == 1);
  }
  for (int v : G[u])
    if (v != pr) {
      dfs(v, u, min(cost, ar[u]));
      dr[u] = dr[u] + dr[v];
    }
  if (ar[u] < cost) {
    change(u);
  }
}
int main() {
  while (cin >> n) {
    for (int i = 0; i <= n; i++) {
      G[i].clear();
      dr[i].clear();
    }
    for (int i = 1; i <= n; i++) {
      cin >> ar[i] >> br[i] >> cr[i];
    }
    for (int i = 1; i < n; i++) {
      int u, v;
      cin >> u >> v;
      G[u].push_back(v);
      G[v].push_back(u);
    }
    dfs(1);
    if (dr[1].tz || dr[1].to) {
      cout << "-1\n";
    } else {
      cout << dr[1].ans << "\n";
    }
    cerr << "---\n";
  }
  return 0;
}
