#icpc2014springa. [icpc2014spring_a]Breadth-First Search by Foxpower

[icpc2014spring_a]Breadth-First Search by Foxpower

MathJax.Hub.Config({ tex2jax: { inlineMath: [["","",""], ["\\(","\\)"]], processEscapes: true }}); blockquote { font-family: Menlo, Monaco, "Courier New", monospace; color: #333333; display: block; padding: 8.5px; margin: 0 0 9px; font-size: 12px; line-height: 18px; background-color: #f5f5f5; border: 1px solid #ccc; border: 1px solid rgba(0, 0, 0, 0.15); -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; white-space: pre; white-space: pre-wrap; word-break: break-all; word-wrap: break-word; }

Problem Statement

Fox Ciel went to JAG Kingdom by bicycle, but she forgot a place where she parked her bicycle. So she needs to search it from a bicycle-parking area before returning home.

The parking area is formed as a unweighted rooted tree TT with nn vertices, numbered 11 through nn. Each vertex has a space for parking one or more bicycles. Ciel thought that she parked her bicycle near the vertex 11, so she decided to search it from there by the breadth-first search. That is, she searches it at the vertices in the increasing order of their distances from the vertex 11. If multiple vertices have the same distance, she gives priority to the vertices in the order of searching at their parents. If multiple vertices have the same parent, she searches at the vertex with minimum number at first.

Unlike a computer, she can't go to a next vertex by random access. Thus, if she goes to the vertex jj after the vertex ii, she needs to walk the distance between the vertices ii and jj. BFS by fox power perhaps takes a long time, so she asks you to calculate the total moving distance in the worst case starting from the vertex 11.


Input

The input is formatted as follows.

nn
p_2p\_2 p_3p\_3 p_4p\_4 cdots\\cdots p_np\_n

The first line contains an integer nn (1lenle1051 \\le n \\le 10^5), which is the number of vertices on the unweighted rooted tree TT. The second line contains n1n-1 integers p_ip\_i (1lep_i<i1 \\le p\_i < i), which are the parent of the vertex ii. The vertex 11 is a root node, so p_1p\_1 does not exist.

Output

Print the total moving distance in the worst case in one line.


Sample Input 1

4
1 1 2```

### Output for the Sample Input 1

```plain
6```

* * *

### Sample Input 2

```plain
4
1 1 3```

### Output for the Sample Input 2

```plain
4```

* * *

### Sample Input 3

```plain
11
1 1 3 3 2 4 1 3 2 9```

### Output for the Sample Input 3

```plain
25```

* * *

### Source Name

[Japan Alumni Group Spring Contest 2014](http://acm-icpc.aitea.net/index.php?2013%2FPractice%2F%BD%D5%A5%B3%A5%F3%A5%C6%A5%B9%A5%C8%2F%B0%C6%C6%E2)

* * *