Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding:utf-8 -*-
import sys
nyuryoku = [int(i) for i in sys.stdin]

N = nyuryoku[0]
a_list = nyuryoku[1:]

"""
a_list=[]

for _ in range(N):
    a_list.append(int(input()))
"""


cnt_push = 1
button_list = []
cur_but_posi = a_list[0]

while True:
    #cur_but_posi = a_list[cur_but_posi - 1]
    if cur_but_posi == 2:
        print(cnt_push)
        break
    elif cur_but_posi in button_list:
        print(-1)
        break
    else:
        button_list.append(cur_but_posi)
        cur_but_posi = a_list[cur_but_posi - 1]    
        cnt_push += 1    



Fixed submission:
import sys

l = [int(i) - 1 for i in sys.stdin]

N = l[0] + 1

a = l[1:]

done = [False] * N

i = 0

ans = 0

while True:
    if done[i] == False:
        done[i] = True
        ans += 1
        if a[i] == 1:
            break
        i = a[i]
    else:
        ans = -1
        break

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding:utf-8 -*-
N = int(input())
"""
a_list=[]

for _ in range(N):
    a_list.append(int(input()))
"""
a_list = [int(input()) for i in range(N)]


cnt_push = 1
button_list = []
cur_but_posi = a_list[0]

while True:
    #cur_but_posi = a_list[cur_but_posi - 1]
    if cur_but_posi == 2:
        print(cnt_push)
        break
    elif cur_but_posi in button_list:
        print(-1)
        break
    else:
        button_list.append(cur_but_posi)
        cur_but_posi = a_list[cur_but_posi - 1]    
        cnt_push += 1    



Fixed submission:
import sys

l = [int(i) - 1 for i in sys.stdin]

N = l[0] + 1

a = l[1:]

done = [False] * N

i = 0

ans = 0

while True:
    if done[i] == False:
        done[i] = True
        ans += 1
        if a[i] == 1:
            break
        i = a[i]
    else:
        ans = -1
        break

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding:utf-8 -*-
N = int(input())
a_list=[]

for _ in range(N):
    a_list.append(int(input()))

cnt_push = 1
button_list = []
cur_but_posi = a_list[0]

while True:
    #cur_but_posi = a_list[cur_but_posi - 1]
    if cur_but_posi == 2:
        print(cnt_push)
        break
    elif cur_but_posi in button_list:
        print(-1)
        break
    else:
        button_list.append(cur_but_posi)
        cur_but_posi = a_list[cur_but_posi - 1]    
        cnt_push += 1    



Fixed submission:
import sys

l = [int(i) - 1 for i in sys.stdin]

N = l[0] + 1

a = l[1:]

done = [False] * N

i = 0

ans = 0

while True:
    if done[i] == False:
        done[i] = True
        ans += 1
        if a[i] == 1:
            break
        i = a[i]
    else:
        ans = -1
        break

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding:utf-8 -*-
N = int(input())
a_list=[]

for _ in range(N):
    a_list.append(int(input()))

cnt_push = 1
button_list = []
cur_but_posi = a_list[0]

while True:
    #cur_but_posi = a_list[cur_but_posi - 1]
    if cur_but_posi == 2:
        print(cnt_push)
        break
    elif cur_but_posi in button_list:
        print(-1)
        break
    else:
        button_list.append(cur_but_posi)
        cur_but_posi = a_list[cur_but_posi - 1]    
        cnt_push += 1    



Fixed submission:
import sys

l = [int(i) - 1 for i in sys.stdin]

N = l[0] + 1

a = l[1:]

done = [False] * N

i = 0

ans = 0

while True:
    if done[i] == False:
        done[i] = True
        ans += 1
        if a[i] == 1:
            break
        i = a[i]
    else:
        ans = -1
        break

print(ans)