n=io.read("*n")
a={}
for i=1,n do
    a[i]=io.read("*n")
end

i=1
j=0
counter=0
while j~=2 do
    j=a[i]
    if a[i]==j and a[j]==i then
        print(-1)
        return
    end
    i=j
    counter=counter+1
end
print(counter)