Warping the Ward


Submit solution

Points: 1
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type
Allowed languages
C, C++, Java, Python, Rust

A call bell is ringing in room k, but you are in the hospital's break room. You need to get to your patient, stat!

The hospital has n rooms, numbered 1 to n, joined by n-1 one-way hallways. Every room except room 1 has exactly one hallway leading into it, and every room can be reached from room 1. You start in room 1 and need to get to room k. More formally, it is a unidirectional tree rooted at node 1.

We'll say that room b is d hallways deeper than room a if the route from a to b passes through exactly d hallways.

To move between rooms, the hospital is trialling an inter-dimensional remote. If you are standing in some room v, you can pick an integer d with 1 \le d \le m and teleport to any room d hallways deeper than v. More formally, you can pick an integer d with 1 \le d \le m and teleport to any node in v's subtree that is d hallways deeper than v.

However, teleporting is very loud! Each time you use the remote, you must first warn every room that is d hallways deeper than v, including the one you are traveling to. Warnings take 1 second per room.

Moving optimally, what is the minimum total time required to get from room 1 to room k?

Input

The first line contains three integers n, k, and m (2 \le n \le 10^5, 2 \le k \le n, 1 \le m \le n), the number of rooms in the hospital, the room you need to travel to, and the greatest depth you can travel with a single use of the remote.

The second line contains n-1 integers p_2, p_3, \dots, p_n (1 \le p_i \le n), where p_i is the room that the hallway leading into room i.

It is guaranteed that n \times m \leq 10^6.

Output

Output a single integer, the minimum total time to reach room k, in seconds.

Example

Input 1
12 12 2
1 1 2 2 2 2 2 3 7 7 11
Output 1
5

The optimal route is:

  • Teleport from room 1 to room 2, which is 1 hallway deeper. There are 2 rooms exactly 1 hallway deeper than room 1, so you spend 2 seconds warning them.
  • Teleport from room 2 to room 11, which is 2 hallways deeper. There are 2 rooms exactly 2 hallways deeper than room 2, so you spend 2 seconds warning them.
  • Teleport from room 11 to room 12, which is 1 hallway deeper. There is only 1 room exactly 1 hallway deeper than room 11, so you spend 1 second warning it.

This gives a total of 5 seconds.

Input 2
17 14 2
1 6 1 1 2 4 7 14 17 17 17 17 10 1 11 6
Output 2
6

Input 3
20 20 9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Output 3
3

Comments

There are no comments at the moment.