Buggy python submission (verdict: Time Limit Exceeded):
# import bisect
# import copy
# import fractions
# import math
# import numpy as np
# from collections import Counter, deque
# from itertools import accumulate,permutations, combinations,combinations_with_replacement,product

def resolve():
    N=int(input())

    A=[int(input()) for i in range(N)]
    temp=[0]*N
    i = 0
    cnt=0

    while True:
        if A[i] not in temp:
            temp[cnt] = A[i]
            if A[i]==2:
                print(cnt+1)
                break
            i=A[i]-1
            cnt+=1

        else:
            print(-1)
            break
resolve()

Fixed submission:
# import bisect
# import copy
# import fractions
# import math
# import numpy as np
# from collections import Counter, deque
# from itertools import accumulate,permutations, combinations,combinations_with_replacement,product

def resolve():
    N=int(input())

    A=[int(input()) for i in range(N)]
    num=0
    cnt=0

    for i in range(N):
        if A[num]==2:
            print(cnt+1)
            break
        num=A[num]-1
        cnt+=1

        if i==N-1:
            print(-1)

resolve()

Buggy python submission (verdict: Time Limit Exceeded):
# import bisect
# import copy
# import fractions
# import math
# import numpy as np
# from collections import Counter, deque
# from itertools import accumulate,permutations, combinations,combinations_with_replacement,product

def resolve():
    N=int(input())

    A=[int(input()) for i in range(N)]
    temp=[0]*N
    i = 0
    cnt=0

    while True:
        if A[i] not in temp:
            temp[cnt] = A[i]
            if A[i]==2:
                print(cnt+1)
                break
            i=A[i]-1
            cnt+=1

        else:
            print(-1)
            break
resolve()

Fixed submission:
# import bisect
# import copy
# import fractions
# import math
# import numpy as np
# from collections import Counter, deque
# from itertools import accumulate,permutations, combinations,combinations_with_replacement,product

def resolve():
    N=int(input())

    A=[int(input()) for i in range(N)]
    num=0
    cnt=0

    for i in range(N):
        if A[num]==2:
            print(cnt+1)
            break
        num=A[num]-1
        cnt+=1

        if i==N-1:
            print(-1)

resolve()

Buggy python submission (verdict: Time Limit Exceeded):
# import bisect
# import copy
# import fractions
# import math
# import numpy as np
# from collections import Counter, deque
# from itertools import accumulate,permutations, combinations,combinations_with_replacement,product

def resolve():
    N=int(input())

    A=[int(input()) for i in range(N)]
    temp=[0]*N
    i = 0
    cnt=0

    while True:
        if A[i] not in temp:
            temp[cnt] = A[i]
            if A[i]==2:
                print(temp.index(A[i])+1)
                break
            i=A[i]-1
            cnt+=1

        else:
            print(-1)
            break
resolve()

Fixed submission:
# import bisect
# import copy
# import fractions
# import math
# import numpy as np
# from collections import Counter, deque
# from itertools import accumulate,permutations, combinations,combinations_with_replacement,product

def resolve():
    N=int(input())

    A=[int(input()) for i in range(N)]
    num=0
    cnt=0

    for i in range(N):
        if A[num]==2:
            print(cnt+1)
            break
        num=A[num]-1
        cnt+=1

        if i==N-1:
            print(-1)

resolve()