(* O(n) *)
let n = Scanf.scanf " %d" @@ (+) 0
let a_s = Array.init n @@ fun _ -> Scanf.scanf " %d" @@ fun a -> a - 1
let flags = Array.make n false
let rec loop acc i =
  if flags.(i) then (print_endline "-1"; exit 0)
  else if i = 1 then (Printf.printf "%d\n" acc; exit 0)
  else (flags.(i) <- true; loop (acc + 1) a_s.(i))
let _ = loop 0 0