Scanf.scanf "%d" (fun n ->
    let nei = Array.make n [] in
    let rev = Array.make n [] in
    for i = 1 to n - 1 do
        Scanf.scanf " %d" (fun a ->
            let a = a - 1 in
            nei.(i) <- a :: nei.(i);
            rev.(a) <- i :: rev.(a)
        )
    done;
    let rec depth node =
        if rev.(node) = [] then 0 else (
            let children = Array.of_list @@ List.fold_left (fun acc v -> (depth v, v) :: acc) [] rev.(node) in
            Array.sort (fun a b -> compare b a) children;
            let k = Array.length children in
            let rec loop i acc =
                if i = k then acc else loop (i + 1) (max acc (i + 1 + fst children.(i)))
            in
            loop 0 0
        )
    in
    Printf.printf "%d\n" @@ depth 0
)