open Batteries
open Printf
let () =
  let n = Scanf.scanf "%d " (fun a -> a) in
  let a_lst = Array.to_list @@ Array.init n (fun i -> (Scanf.scanf "%d " (fun a -> a))) in

  let rec aux visited i cnt =
    let next = (List.nth a_lst i)-1 in
    if List.mem i visited then -1 else
    if i = 1 then cnt else
      aux (i::visited) next (cnt+1)
  in

  Printf.printf "%d\n" @@
  aux [] 0 0 
