#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
vector<int> arr[200001];
long long n, u, v, w;
long long val[200001];
bool curr[200001];
bool want[200001];
long long ans = 0;
void dfs(int to, int from) {
  if (to != 1) {
    val[to] = min(val[to], val[from]);
  }
  for (auto i : arr[to]) {
    if (i != from) {
      dfs(i, to);
    }
  }
}
pair<long long int, long long int> dfs2(int to, int from) {
  pair<long long int, long long int> a = {0, 0};
  if (curr[to] != want[to]) {
    if (want[to]) {
      a.second++;
    } else {
      a.first++;
    }
  }
  for (auto i : arr[to]) {
    if (i != from) {
      auto p = dfs2(i, to);
      a.first += p.first;
      a.second += p.second;
    }
  }
  if (a.first > a.second) {
    ans += 2 * val[to] * a.second;
    a.first -= a.second;
    a.second = 0;
  } else {
    ans += 2 * val[to] * a.first;
    a.second -= a.first;
    a.first = 0;
  }
  return a;
}
void pre() {}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> val[i] >> curr[i] >> want[i];
  }
  for (int i = 0; i < n - 1; i++) {
    cin >> u >> v;
    arr[v].push_back(u);
    arr[u].push_back(v);
  }
  dfs(1, 0);
  auto i = dfs2(1, 0);
  if (i.first != 0 || i.second != 0) {
    cout << -1 << "\n";
  } else {
    cout << ans << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  pre();
  long long int num = 1;
  for (long long int i = 0; i < num; i++) {
    solve();
  }
}

### Prompt

Construct a CPP code solution to the problem outlined:

Ashish has a tree consisting of n nodes numbered 1 to n rooted at node 1. The i-th node in the tree has a cost a_i, and binary digit b_i is written in it. He wants to have binary digit c_i written in the i-th node in the end.

To achieve this, he can perform the following operation any number of times: 

  * Select any k nodes from the subtree of any node u, and shuffle the digits in these nodes as he wishes, incurring a cost of k ⋅ a_u. Here, he can choose k ranging from 1 to the size of the subtree of u. 



He wants to perform the operations in such a way that every node finally has the digit corresponding to its target.

Help him find the minimum total cost he needs to spend so that after all the operations, every node u has digit c_u written in it, or determine that it is impossible.

Input

First line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5) denoting the number of nodes in the tree.

i-th line of the next n lines contains 3 space-separated integers a_i, b_i, c_i (1 ≤ a_i ≤ 10^9, 0 ≤ b_i, c_i ≤ 1) — the cost of the i-th node, its initial digit and its goal digit.

Each of the next n - 1 lines contain two integers u, v (1 ≤ u, v ≤ n,   u ≠ v), meaning that there is an edge between nodes u and v in the tree.

Output

Print the minimum total cost to make every node reach its target digit, and -1 if it is impossible.

Examples

Input


5
1 0 1
20 1 0
300 0 1
4000 0 0
50000 1 0
1 2
2 3
2 4
1 5


Output


4

Input


5
10000 0 1
2000 1 0
300 0 1
40 0 0
1 1 0
1 2
2 3
2 4
1 5


Output


24000

Input


2
109 0 1
205 0 1
1 2


Output


-1

Note

The tree corresponding to samples 1 and 2 are: 

<image>

In sample 1, we can choose node 1 and k = 4 for a cost of 4 ⋅ 1 = 4 and select nodes {1, 2, 3, 5}, shuffle their digits and get the desired digits in every node.

In sample 2, we can choose node 1 and k = 2 for a cost of 10000 ⋅ 2, select nodes {1, 5} and exchange their digits, and similarly, choose node 2 and k = 2 for a cost of 2000 ⋅ 2, select nodes {2, 3} and exchange their digits to get the desired digits in every node.

In sample 3, it is impossible to get the desired digits, because there is no node with digit 1 initially.

### Response

```cpp
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
vector<int> arr[200001];
long long n, u, v, w;
long long val[200001];
bool curr[200001];
bool want[200001];
long long ans = 0;
void dfs(int to, int from) {
  if (to != 1) {
    val[to] = min(val[to], val[from]);
  }
  for (auto i : arr[to]) {
    if (i != from) {
      dfs(i, to);
    }
  }
}
pair<long long int, long long int> dfs2(int to, int from) {
  pair<long long int, long long int> a = {0, 0};
  if (curr[to] != want[to]) {
    if (want[to]) {
      a.second++;
    } else {
      a.first++;
    }
  }
  for (auto i : arr[to]) {
    if (i != from) {
      auto p = dfs2(i, to);
      a.first += p.first;
      a.second += p.second;
    }
  }
  if (a.first > a.second) {
    ans += 2 * val[to] * a.second;
    a.first -= a.second;
    a.second = 0;
  } else {
    ans += 2 * val[to] * a.first;
    a.second -= a.first;
    a.first = 0;
  }
  return a;
}
void pre() {}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> val[i] >> curr[i] >> want[i];
  }
  for (int i = 0; i < n - 1; i++) {
    cin >> u >> v;
    arr[v].push_back(u);
    arr[u].push_back(v);
  }
  dfs(1, 0);
  auto i = dfs2(1, 0);
  if (i.first != 0 || i.second != 0) {
    cout << -1 << "\n";
  } else {
    cout << ans << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  pre();
  long long int num = 1;
  for (long long int i = 0; i < num; i++) {
    solve();
  }
}

```