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

let d =
    new System.Collections.Generic.Dictionary<int, int>()

let rec solve (d: System.Collections.Generic.Dictionary<int, int>) (arr: int []) current =
    let c = current - 1
    let next = arr.[c]
    if next = 2 then
        d.Count + 1
    elif d.ContainsKey(next) then
        -1
    else
        d.Add(next, next)
        solve d arr next


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