Problem p02684 (python)

Fixed submission:
N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
r = 0
if K <= N:
    current = 1
    for i in range(K):
        current = A[current-1]
    print(current)
else:
    isReturn = [0 for i in range(N)]
    step = 1
    current = 1
    isReturn[current-1] = step
    for i in range(N):
        current = A[current-1]
        step += 1
        if isReturn[current-1] == 0:
            isReturn[current-1] = step
        else:
            r = step - isReturn[current-1]
            break
    
    t = ((K-step) % r) + 1
    for i in range(t):
        current = A[current-1]
        
    print(current)

Buggy submission (verdict: Runtime Error):
N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
r = 0
if K <= N:
    current = 0
    for i in K:
        current = A[current-1]
    print(current)
else:
    isReturn = [0]*(N)
    step = 1
    current = 1
    isReturn[current-1] = step
    for i in range(N):
        current = A[current-1]
        step += 1
        if isReturn[current-1] == 0:
            isReturn[current-1] = step
        else:
            r = step - isReturn[current-1]
            break
    
    t = (K-step) % r
    for i in range(t+1):
        current = A[current-1]
        
    print(current)

Buggy submission (verdict: Runtime Error):
N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
r = 0
if K <= N:
    current = 0
    for i in K:
        current = A[current-1]
    print(current)
else:
    isReturn = [0]*(N)
    step = 1
    current = 1
    isReturn[current-1] = step
    for i in range(K):
        current = A[current-1]
        step += 1
        if isReturn[current-1] == 0:
            isReturn[current-1] = step
        else:
            r = step - isReturn[current-1]
            break
    
    t = (K-step) % r
    for i in range(t+1):
        current = A[current-1]
        
    print(current)

Buggy submission (verdict: Runtime Error):
N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
r = 0
if K <= N:
    current = 0
    for i in K:
        current = A[current-1]
    print(current)
else:
    isReturn = [0]*(N+1)
    step = 1
    current = 1
    isReturn[current] = step
    for i in range(K):
        current = A[current-1]
        step += 1
        if isReturn[current] == 0:
            isReturn[current] = step
        else:
            r = step - isReturn[current]
            break
    
    t = (K-step) % r
    for i in range(t+1):
        current = A[current-1]
        
    print(current)

Buggy submission (verdict: Runtime Error):
N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
r = 0
if K <= N:
    current = 0
    for i in K:
        current = A[current-1]
    print(current+1)
else:
    isReturn = [0]*(N+1)
    step = 1
    current = 1
    isReturn[current] = step
    for i in range(K):
        current = A[current-1]
        step += 1
        if isReturn[current] == 0:
            isReturn[current] = step
        else:
            r = step - isReturn[current]
            b = isReturn[current] - 1
            break
    
    t = (K-step) % N
    for i in range(t+1):
        current = A[current-1]
        
    print(current)