let () =
  let a = Scanf.scanf "%d" (fun n -> Array.init n (fun _ -> Scanf.scanf " %d" (fun x -> x-1))) in
  let rec f c x =
    if a.(x) < 0 then -1
    else if a.(x) = 1 then c
    else (let x' = a.(x) in a.(x) <- -1; f (c+1) x') in
  f 1 0 |> Printf.printf "%d\n"
