let id x = x
let n = Scanf.scanf "%d\n" id

let array = Array.init n (fun _ -> (Scanf.scanf "%d\n") id)

let rec solve n cnt list =
  if n = 1 then cnt else
    if List.mem n list then -1 else
      solve (array.(n) - 1) (cnt + 1) (n :: list)

let ans = solve 0 0 []
  
let () =
  Printf.printf "%d\n" ans