Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = list(map(int, input().split()))

num = K
memo = []
town = 1
if K > N:
    for _ in range(N):
        memo.append(town)
        if not A[town-1] in memo:
            town = A[town-1]
            num -= 1
        else:
            index = memo.index(A[town-1])
            l = len(memo) - index
            if num % l == 0:
                town = memo[-1]
            else:
                town = memo[index + (num % l) - 1]
            break
else:
    for _ in range(K):
        town = A[town - 1]

print(town)

Fixed submission:
from collections import deque
n, k = map(int, input().split())
a = [-1] + list(map(int, input().split()))
town = [-1] * (n + 1)
town[1] = 0
now_town = 1
second_first = 0
while True:
    if town[a[now_town]] != -1:
        second_first = a[now_town]
        break
    town[a[now_town]] = town[now_town] + 1
    if town[a[now_town]] == k:
        print(a[now_town])
        exit()
    now_town = a[now_town]
    # print(town)
start_index = town[second_first]
end_index = max(town)
rest = k - end_index
div = (rest-1) % (end_index - start_index + 1)
# print(rest, div)
ans = town.index((start_index) + div)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = list(map(int, input().split()))

num = K
memo = []
town = 1
if K > N:
    for _ in range(K):
        memo.append(town)
        if not A[town-1] in memo:
            town = A[town-1]
            num -= 1
        else:
            index = memo.index(A[town-1])
            l = len(memo) - index
            if num % l == 0:
                town = memo[-1]
            else:
                town = memo[index + (num % l) - 1]
            break
else:
    for _ in range(K):
        town = A[town - 1]

print(town)

Fixed submission:
from collections import deque
n, k = map(int, input().split())
a = [-1] + list(map(int, input().split()))
town = [-1] * (n + 1)
town[1] = 0
now_town = 1
second_first = 0
while True:
    if town[a[now_town]] != -1:
        second_first = a[now_town]
        break
    town[a[now_town]] = town[now_town] + 1
    if town[a[now_town]] == k:
        print(a[now_town])
        exit()
    now_town = a[now_town]
    # print(town)
start_index = town[second_first]
end_index = max(town)
rest = k - end_index
div = (rest-1) % (end_index - start_index + 1)
# print(rest, div)
ans = town.index((start_index) + div)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = list(map(int, input().split()))

num = K
memo = []
town = 1
for _ in range(K):
    memo.append(town)
    if not A[town-1] in memo:
        town = A[town-1]
        num -= 1
    else:
        index = memo.index(A[town-1])
        l = len(memo) - index
        if num % l == 0:
            town = memo[-1]
        else:
            town = memo[index + (num % l) - 1]
        break
print(town)

Fixed submission:
from collections import deque
n, k = map(int, input().split())
a = [-1] + list(map(int, input().split()))
town = [-1] * (n + 1)
town[1] = 0
now_town = 1
second_first = 0
while True:
    if town[a[now_town]] != -1:
        second_first = a[now_town]
        break
    town[a[now_town]] = town[now_town] + 1
    if town[a[now_town]] == k:
        print(a[now_town])
        exit()
    now_town = a[now_town]
    # print(town)
start_index = town[second_first]
end_index = max(town)
rest = k - end_index
div = (rest-1) % (end_index - start_index + 1)
# print(rest, div)
ans = town.index((start_index) + div)
print(ans)