let n = stdin.ReadLine() |> int

let a =
  seq {
    for i in 0 .. n - 1 -> (i, stdin.ReadLine() |> int)
  }
  |> Map

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

ans |> stdout.WriteLine