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 =
  if n = 1 then cnt else
    if cnt > 100005 then -1 else
      solve (array.(n) - 1) (cnt + 1)

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