import scala.collection.mutable
import scala.io.StdIn._
object Main extends App {

  val n = readInt
  if (n != 1) {
    val children = Array.fill(n) {
      Nil: List[Int]
    }
    val parents = Array.fill(n) {
      0
    }
    val childCount = Array.fill(n) {
      0
    }
    for ((p, c) ← readLine.trim.split(' ').map(_.toInt).zipWithIndex.map { case (p, i) ⇒ (p - 1, i + 1) }.reverse) {
      children(p) ::= c
      childCount(p) += 1
      parents(c) = p
    }
    val heights = Array.fill(n) {
      0
    }
    val queue = mutable.Queue[Int](0)
    var path = Nil: List[Int]
    while (queue.nonEmpty) {
      val top = queue.dequeue()
      path ::= top
      for (c ← children(top)) {
        heights(c) = heights(top) + 1
        queue.enqueue(c)
      }
    }
    val trees = Array.tabulate(n) { i ⇒ new PersistentUnionFind[Int](i) }
    val maxHeights = heights.max
    queue.enqueue(childCount.indices.filter(i ⇒ childCount(i) == 0): _*)
    while (queue.nonEmpty) {
      val top = queue.dequeue()
      if (top != 0) {
        trees(top).mergeAt(trees(parents(top)), maxHeights - heights(parents(top)))
        childCount(parents(top)) -= 1
        if (childCount(parents(top)) == 0) {
          queue.enqueue(parents(top))
        }
      }
    }
    val move = path.dropRight(1).zip(path.tail).map {
      case (f, t) ⇒
        heights(f) + heights(t) - (maxHeights - trees(f).mergedTime(trees(t)).get) * 2L
    }
    println(
      move.sum
    )
  }else {
    println(0)
  }

  class PersistentUnionFind[T](val value: T) {
    private var parent = this
    private var time = Int.MaxValue
    private var size = 1
    private def isTop: Boolean = parent == this
    private def top: PersistentUnionFind[T] = {
      if (parent != this) parent.top
      else this
    }
    def findAt(time: Int): PersistentUnionFind[T] = {
      if (time < this.time) {
        this
      }else {
        parent.findAt(time)
      }
    }
    def mergedTime(that: PersistentUnionFind[T]): Option[Int] = {
      var ti = this
      var ta = that
      var t = time
      while(ti != ta && (!ti.isTop || !ta.isTop)) {
        if (ti.time < ta.time){
          t = ti.time
          ti = ti.parent
        }else {
          t = ta.time
          ta = ta.parent
        }
      }
      if (ti == ta) Some(t)
      else None
    }
    def mergeAt(that: PersistentUnionFind[T], time: Int): Unit = {
      val ti = top
      val ta = that.top
      if (ti != ta){
        if (ti.size < ta.size){
          ti.parent = ta
          ti.time = time
          ta.size += ti.size
        }else {
          ta.parent = ti
          ta.time = time
          ti.size += ta.size
        }
      }
    }
  }
  implicit class Extension[T](val value: T) extends AnyVal {
    @inline def let[A](@inline func: T ⇒ A): A = func(value)
  }
}
