Under construction

Still being built. Things may change

Depth of the Tree Node with Value k

Interview

Time limit1sMemory limit512 MB

Summary
Given a rooted tree and a value k assigned to one vertex, print the depth of the vertex that holds value k.
Level

Easy3 of 10

Topics
Tree, BFS
Solved
No attempts yet

Problem

A tree T has nn vertices and n−1n-1 edges. The vertices are numbered from 0 to n−1n-1, and vertex 0 is the root. The edges have no weights. Each vertex in T is assigned a distinct value. Print the depth of the vertex whose assigned value is kk. The root has depth 0, and each child has a depth exactly 1 greater than its parent.

Input

The first line contains the number of vertices nn and the integer kk, separated by a space.

Each of the next n−1n-1 lines describes one edge. Each line contains a parent vertex number pp and a child vertex number cc, separated by a space.

The next line contains the values assigned to vertices 0 through n−1n-1, separated by spaces. The ii-th number is the value assigned to vertex i−1i-1.

Output

Print the depth of the vertex whose assigned value is kk on the first line.

Constraints

  • 2≤n≤100,0002 \le n \le 100,000
  • 0≤k≤n−10 \le k \le n-1
  • 0≤p,c≤n−10 \le p, c \le n-1, p≠cp \ne c
  • The edges form a tree.
  • 0≤0 \le the value assigned to each vertex ≤n−1\le n-1
  • The values assigned to the vertices are distinct integers.

Examples1

  1. Example 1

    Input
    8 5
    0 1
    0 2
    1 3
    1 4
    2 5
    2 6
    6 7
    0 1 2 3 4 5 6 7
    
    Expected output
    2