let n = stdin.ReadLine() |> int

let a =
  [for _ in 1 .. n -> stdin.ReadLine() |> (fun x -> (int x) - 1) ]

let ans =
  [|
    let mutable tmp = 0
    yield tmp
    for i in 1 .. n do
      tmp <- a.[tmp]
      yield tmp
  |]
  |> Array.tryFindIndex (fun i -> i = 1)
  |> (fun x ->
        match x with
        | Some i -> i
        | None -> -1 )

ans |> stdout.WriteLine