open Batteries
open Printf
let () =
  let n = Scanf.scanf "%d " (fun a -> a) in
  let arr = Array.init n (fun _ -> (Scanf.scanf "%d " (fun a -> a))) in
  let visited = Array.init n (fun _ -> false) in

  let rec aux i cnt =
    let next = arr.(i) - 1 in
    if visited.(i) then -1 else
    if i = 1 then cnt else (
      visited.(i) <- true;
      aux next (cnt+1)
    )
  in

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