Problem p02684 (python)

Fixed submission:
N, K = list(map(int,input().split()))

A = [0]+ list(map(int,input().split()))

n = 1
visited = [1]
visitedbit = [0] * (N+1) 
visitedbit[1] = 1
k = 0
flag = True
while True:
    
    k += 1
    #A[n]はテレポート先
    if k == K:
        print(A[n])
        flag = False
        break
    if visitedbit[A[n]]  == 0:
        visited.append(A[n])
        visitedbit[A[n]] = 1
        n = A[n]
    else:
        hazime = visited.index(A[n])
        break

if flag == True:
    # print(visited)
    loop = visited[hazime:]
    # print(loop)
    # print(hazime)
    print(loop[ (K-hazime)%(len(loop)) ])

Buggy submission (verdict: Runtime Error):
N,K = list(map(int,input().split()))

A = list(map(int,input().split()))
visited = [1]
visited_list = [0]*N
visited_list[0] = 1
k = 0
l = 0
kaburi = 1
flag = True
def f(Ai):
    global k,l,visited,kaburi,flag
    # print("Ai",Ai,k)
    if visited_list[Ai-1] == 1 :
        kaburi = Ai
        k += 1
        l = k
    else:
        visited.append(Ai)
        visited_list[Ai-1] = 1
        k += 1
        if k == K:
            flag = False
            print(Ai)
        else:
            f(A[Ai-1])

f(A[0])
if flag == True:
    hazime = visited.index(kaburi)
    b = visited[hazime:]
    print(b[(K-hazime)%(l-hazime)])

Buggy submission (verdict: Runtime Error):
N,K = list(map(int,input().split()))

A = list(map(int,input().split()))
visited = [1]
visited_list = [0]*N
visited_list[0] = 1
k = 0
l = 0
kaburi = 1
flag = True
def f(Ai):
    global k,l,visited,kaburi,flag
    # print("Ai",Ai,k)
    if k == K:
        flag = False
        print(Ai)
    elif visited_list[Ai-1] == 1 :
        kaburi = Ai
        k += 1
        l = k
    else:
        visited.append(Ai)
        visited_list[Ai-1] = 1
        k += 1
        f(A[Ai-1])

f(A[0])
if flag == True:
    hazime = visited.index(kaburi)
    b = visited[hazime:]
    print(b[(K-hazime)%(l-hazime)])

Buggy submission (verdict: Runtime Error):
N,K = list(map(int,input().split()))

A = list(map(int,input().split()))
visited = [1]
visited_list = [0]*N
visited_list[0] = 1
k = 0
l = 0
kaburi = 1
def f(Ai):
    global k,l,visited,kaburi
    # print("Ai",Ai,k)
    if k == K:
        print(A[Ai])
    elif visited_list[Ai-1] == 1 :
        kaburi = Ai
        k += 1
        l = k
    else:
        visited.append(Ai)
        visited_list[Ai-1] = 1
        k += 1
        f(A[Ai-1])

f(A[0])
hazime = visited.index(kaburi)
b = visited[hazime:]
# print(b)
# print(l)
print(b[(K-hazime)%(l-hazime)])

Buggy submission (verdict: Runtime Error):
N,K = list(map(int,input().split()))

A = list(map(int,input().split()))
visited = [1]
visited_list = [0]*N
visited_list[0] = 1
k = 0
l = 0
kaburi = 1
def f(Ai):
    global k,l,visited,kaburi
    # print("Ai",Ai,k)
    if k == K:
        print(A[Ai])
    elif visited_list[Ai-1] == 1 :
        visited.append(Ai)
        kaburi = Ai
        k += 1
        l = k
    else:
        visited.append(Ai)
        visited_list[Ai-1] = 1
        k += 1
        f(A[Ai-1])

f(A[0])
# print(l)
# print(visited)
a = [i for i, x in enumerate(visited) if x == kaburi]
b = visited[a[0]:a[1]]
# print(b)
# print(l)
print(b[(K-a[0])%(l-a[0])])