type t = Node of t * t | Leaf of int

module IntMap = Map.Make (struct
  type t = int
  let compare = compare
end)

let rec balance ts =
  let d, t, ts' =
    match IntMap.min_binding ts with
    | (d, [t]) -> (d, t, IntMap.remove d ts)
    | (d, t :: tt) -> (d, t, IntMap.add d tt ts) in
  match IntMap.min_binding ts' with
  | d', [t'] ->
      let ts'' = IntMap.remove d' ts' in
      let d'' = 1 + max d d' in
      balance (IntMap.add d'' (Node (t, t') :: try IntMap.find d'' ts'' with Not_found -> []) ts'')
  | d', t' :: tt ->
      let ts'' = IntMap.add d' tt ts' in
      let d'' = 1 + max d d' in
      balance (IntMap.add d'' (Node (t, t') :: try IntMap.find d'' ts'' with Not_found -> []) ts'')
  | exception Not_found -> (d, t)

let rec make_tournament losers winner =
  List.map (make_tournament losers) (losers winner)
    |> List.fold_left (fun s (d, t) ->
        IntMap.add d (t :: try IntMap.find d s with Not_found -> []) s)
          (IntMap.singleton 0 [Leaf winner])
    |> balance

let () =
  let n = Scanf.scanf "%d\n" (fun n -> n) in
  let as_ = Array.init (n - 1) (fun _ -> Scanf.scanf "%d\n" (fun a -> a)) in
  let losers = Array.make n [] in
  Array.iteri (fun i a ->
    losers.(a - 1) <- i + 1 :: losers.(a - 1)) as_;
  Printf.printf "%d\n" @@ fst @@ make_tournament (fun i -> losers.(i)) 0
