human
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: 


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...
impor
assistant
#Here is Python program to 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.
t io
import os
import sys
from collections import Counter, defaultdict, deque

from types import GeneratorType

# https://github.com/cheran-senthil/PyRival/blob/master/pyrival/misc/bootstrap.py
def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to

    return wrappedfunc

def solve(N, ABC, edges):
    g = [[] for i in range(N + 1)]
    for u, v in edges:
        g[u].append(v)
        g[v].append(u)

    if sum(b - c for a, b, c in ABC) != 0:
        return -1

    ans = 0

    @bootstrap
    def f(node, parent, bestCost):
        nonlocal ans

        # Anything in this subtree can shuffle with smaller of ancestor or this node's cost
        a, b, c = ABC[node - 1]

        bestCost = min(bestCost, a)
        needsZero = 0
        needsOne = 0
        if b != c:
            if c == 1:
                needsZero += 1
            else:
                needsOne += 1
        for nbr in g[node]:
            if nbr != parent:
                zeroes, ones = yield f(nbr, node, bestCost)
                needsZero += zeroes
                needsOne += ones

        common = min(needsZero, needsOne)
        ans += 2 * common * bestCost

        yield needsZero - common, needsOne - common

    root = 1
    zeroes, ones = f(root, None, ABC[0][0])
    assert zeroes == ones == 0

    return ans

if __name__ == "__main__":
    input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

    (N,) = [int(x) for x in input().split()]
    ABC = [[int(x) for x in input().split()] for i in range(N)]
    edges = [[int(x) for x in input().split()] for i in range(N - 1)]
    ans = solve(N, ABC,...