You are given an undirected tree with vertices, numbered from to (a tree is an undirected connected graph in which there are no cycles) and a permutation of its nodes (a sequence of length , arranged in any order and no node number appears twice in the sequence).
Task
You are asked to compute the number of contiguous subsequences () of such that the nodes form a connected undirected graph.
Input data
The first line contains an integer .
The next lines contain 2 integers , , denoting an edge of the tree.
The last line of the input contains the numbers , representing the permutation .
Output data
You need to write a single line with an integer: the number of contiguous subsequences () of such that the nodes form a connected undirected graph.
Constraints and clarifications
# | Points | Constraints |
---|---|---|
1 | 10 | |
2 | 20 | |
3 | 30 | The given tree is just a simple chain. |
4 | 40 | No additional constraints. |
Example
stdin
5
1 2
1 3
2 4
2 5
1 2 4 3 5
stdout
10
Explanation
In the sample case, one example of a contiguous subsequence of nodes which forms a connected region is the one formed by the first elements of . One example of a subrange of nodes from which does not form a connected region is .