Hide

Problem B
Hackenbush Shrubbery

Languages en is
/problems/hackenbushshrubbery/file/statement/en/img-0001.png
Sample input 1

Hackenbush is a game played on edges of three colours which all connect to each other or the ground. The edges are all red, green or blue. The first player can hack red and green edges and the second blue and green edges. When a player hacks an edge it is deleted. Any edges that can’t reach the ground are discarded.

The value of a Hackenbush game with only green edges is simply the Grandy-Sprague number of that game.

You are given a position in Hackenbush shrubbery. This is a position in the game Hackenbush where all edges are green (can be chopped by either player) and all edges have exactly one path down to the ground. That is to say there are no cycles of edges.

You need to determine the value of the game. As the game only has green edges this will always be an integral nimber.

Input

The first line contains a single integer $0 \leq n \leq 10^5$, the number of edges. The next line contains $n$ integers $-1 \leq e_i \leq n$, $e_i \neq 0$. The number $e_i$ denotes which edge is directly below edge $i$ on the unique path to the ground. If $e_i = -1$ the edge touches the ground. The index $e_i$ will always be an an edge that has already appeared in the input, or be the ground.

Output

If the value of the game is $\ast k$ then print *k. The value of the game is always of this form.

Sample Input 1 Sample Output 1
20
-1 1 2 3 3 1 6 7 8 6 10 1 12 13 12 -1 16 17 16 19
*4

Please log in to submit a solution to this problem

Log in