Buggy python submission (verdict: Time Limit Exceeded):
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    btn = [int(input())-1 for _ in range(N)]
    i = 0
    li = [0]
    #print(btn)
    
    for _ in range(N):
        k = btn[i]
        #print(k, li)
        if k in li:
            print('-1')
            break
        else:
            if k != 1:
                li.append(k)
            else:
                print(len(li))
                break
        i = k
    
if __name__ == '__main__':
    main()

Fixed submission:
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    btn = [int(input())-1 for _ in range(N)]
    i = 0
    li = [False]*N
    #print(btn)
    
    for _ in range(N):
        li[i] = True
        k = btn[i]
        
        #print(k, li)
        if li[k]:
            print('-1')
            break
        elif k == 1:
            print(sum(li))
            break
        
        i = k
    
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
btn = [int(input())-1 for _ in range(N)]
i = 0
li = [0]

for _ in range(N):
    k = btn[i]
    #print(k, li)
    if k in li:
        print('-1')
        break
    else:
        if k != 1:
            li.append(k)
        else:
            print(len(li))
            break
    i = k

Fixed submission:
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    btn = [int(input())-1 for _ in range(N)]
    i = 0
    li = [False]*N
    #print(btn)
    
    for _ in range(N):
        li[i] = True
        k = btn[i]
        
        #print(k, li)
        if li[k]:
            print('-1')
            break
        elif k == 1:
            print(sum(li))
            break
        
        i = k
    
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
btn = [int(input())-1 for _ in range(N)]
i = 0
li = [0]

for _ in range(N):
    k = btn[i]
    #print(k, li)
    if k in li:
        print('-1')
        break
    else:
        if k != 1:
            li.append(k)
        else:
            print(len(li))
            break
    i = k

Fixed submission:
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    btn = [int(input())-1 for _ in range(N)]
    i = 0
    li = [False]*N
    #print(btn)
    
    for _ in range(N):
        li[i] = True
        k = btn[i]
        
        #print(k, li)
        if li[k]:
            print('-1')
            break
        elif k == 1:
            print(sum(li))
            break
        
        i = k
    
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
btn = [int(input())-1 for _ in range(N)]
i = 0
li = [0]

while True:
    k = btn[i]
    #print(k, li)
    if k in li:
        print('-1')
        break
    else:
        if k != 1:
            li.append(k)
        else:
            print(len(li))
            break
    i = k

Fixed submission:
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    btn = [int(input())-1 for _ in range(N)]
    i = 0
    li = [False]*N
    #print(btn)
    
    for _ in range(N):
        li[i] = True
        k = btn[i]
        
        #print(k, li)
        if li[k]:
            print('-1')
            break
        elif k == 1:
            print(sum(li))
            break
        
        i = k
    
if __name__ == '__main__':
    main()