let readInt _ = stdin.ReadLine() |> int

let rec solve log (arr: int []) current =
    let c = current - 1
    let next = arr.[c]
    if next = 2 then log |> List.length |> (+) 1
    elif log |> List.contains next then -1
    else solve (next :: log) arr next


()
|> readInt
|> fun x -> [| 1 .. x |]
|> Array.map readInt
|> solve []
<| 1
|> printfn "%d"
