n = int(eval(input()))

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

ans = False

used = set()

i = 1

count = 0

while ans == False:

    count += 1

    if a[i-1] == 2:

        ans = True

    elif i-1 in used:

        break

    else:

        used.add(i-1)

        i = a[i-1]

if ans == True:

    print(count)

else:

    print("-1")

        
n = int(eval(input()))

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

count = 0

i = 0

ans = False

while count < n:

    count += 1

    if a[i] == 2:

        ans = True

        break

    else:

        i = a[i] - 1

if ans == True:

    print(count)

else:

    print("-1")
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
ans = False
used = set()
i = 1
count = 0
while ans == False:
    count += 1
    if a[i - 1] == 2:
        ans = True
    elif i - 1 in used:
        break
    else:
        used.add(i - 1)
        i = a[i - 1]
if ans == True:
    print(count)
else:
    print("-1")

n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
count = 0
i = 0
ans = False
while count < n:
    count += 1
    if a[i] == 2:
        ans = True
        break
    else:
        i = a[i] - 1
if ans == True:
    print(count)
else:
    print("-1")

+count = 0 +i = 0 -used = set() -i = 1 -count = 0 -while ans == False: +while count < n: -    if a[i - 1] == 2: +    if a[i] == 2: -    elif i - 1 in used: -        used.add(i - 1) -        i = a[i - 1] +        i = a[i] - 1