stdin.ReadLine()
|> int
|> (fun n ->
    [1..n]
    |> List.map (fun _ -> stdin.ReadLine() |> int)
    |> fun list -> 0::list
)
|> (fun list ->
    list
    |> Seq.fold (
        fun (acc, i, found) _ ->
            if found
            then (acc, i, found)
            else
                match list.[i] with
                | 2 -> (acc, 2, true)
                | ai when (acc |> Seq.contains ai) -> ([], ai, true)
                | ai -> (ai::acc, ai, false)
    ) ([], 1, false)
    |> fun (acc, _, _) -> acc |> Seq.length
    |> fun l -> if l = 0 then -1 else l + 1
)
|> printfn "%d"
