#/usr/bin/env python



n = int(eval(input()))

a = [int(eval(input())) for _ in range(n)]



lighted = [0] 

ans = 0 



for i in range(n):

    u = lighted[-1]

    v = a[u]-1

    ans += 1

    if v == 1:

        print(ans)

        exit()

    lighted.append(v)



print((-1))

#/usr/bin/env python



n = int(eval(input()))

a = [int(eval(input())) for _ in range(n)]



ans = 0 

v = 0 

for i in range(n):

    v = a[v]-1

    ans += 1

    if v == 1:

        print(ans)

        exit()



print((-1))

# /usr/bin/env python
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
lighted = [0]
ans = 0
for i in range(n):
    u = lighted[-1]
    v = a[u] - 1
    ans += 1
    if v == 1:
        print(ans)
        exit()
    lighted.append(v)
print((-1))

# /usr/bin/env python
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
ans = 0
v = 0
for i in range(n):
    v = a[v] - 1
    ans += 1
    if v == 1:
        print(ans)
        exit()
print((-1))

-lighted = [0] +v = 0 -    u = lighted[-1] -    v = a[u] - 1 +    v = a[v] - 1 -    lighted.append(v)