let n = stdin.ReadLine() |> int

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

let ans =
  let visited =
    [| for i in 0 .. n - 1 -> i = 0 |]

  [ let mutable finish = false
    let mutable i = 0
    let mutable tmp = 0
    while not finish do
      i <- i + 1
      tmp <- a.[tmp]
      if tmp = 1 then
        finish <- true
        yield i
      elif visited.[tmp] then
        finish <- true
        yield -1
      else
        visited.[tmp] <- true ]
  |> List.head

ans |> stdout.WriteLine