let rec read f n =
  if n <= 0 then []
  else
    let x = f () in
    x :: read f (n - 1)

let () =
  let n = read_int () in
  let xs = read read_int n in
  let tbl = Array.of_list (0 :: xs) in
  let vt = Array.make (n+1) false in
  let rec iter c i =
    if vt.(i) then (-1)
    else if tbl.(i) = 2 then c
    else begin
        vt.(i) <- true;
        iter (c+1) tbl.(i)
      end
  in iter 1 1 |> string_of_int |> print_endline
