n=io.read("*n")
a={}
counter=0
for i=1,n do
    a[i]=io.read("*n")
    if a[i]~=2 then
        counter=counter+1
    end
end
if counter==n then
    print(-1)
    return
end

i=1
a_pushed={}
while i~=2 do
    if not a_pushed[i] then
        a_pushed[i]=1
    else
        print(-1)
        return
    end
    j=a[i]
    i=j
end

moves=0
for k,v in pairs(a_pushed) do
    moves=moves+v
end
print(moves)