Buggy python submission (verdict: Time Limit Exceeded):
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
memo = []
memo_cir = []
index = 0
flag = True
while flag:
    if index in memo:
        flag = False
    else:
        memo.append(index)
        index = a[index] - 1
flag = False
for i_ in memo:
    if i_ == index:
        flag = True
    if flag:
        memo_cir.append(i_)
if k + 1 >= (len(memo) - len(memo_cir)):
    k -= (len(memo) - len(memo_cir) - 1)
    print(memo_cir[(k % len(memo_cir)) - 1] + 1)
else:
    print(memo[k] + 1)


Fixed submission:
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
memo = []
memo_cir = []
visited = [False] * n
index = 0
flag = True
while flag:
    if visited[index]:
        flag = False
    else:
        memo.append(index)
        visited[index] = True
        index = a[index] - 1
flag = False
for i_ in memo:
    if i_ == index:
        flag = True
    if flag:
        memo_cir.append(i_)
if k + 1 >= (len(memo) - len(memo_cir)):
    k -= (len(memo) - len(memo_cir) - 1)
    print(memo_cir[(k % len(memo_cir)) - 1] + 1)
else:
    print(memo[k] + 1)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
memo = []
memo_cir = []
index = 0
flag = True
while flag:
    if index in memo:
        flag = False
    else:
        memo.append(index)
        index = a[index] - 1
flag = False
for i_ in memo:
    if i_ == index:
        flag = True
    if flag:
        memo_cir.append(i_)
if k + 1 >= (len(memo) - len(memo_cir)):
    k -= (len(memo) - len(memo_cir) - 1)
    print(memo_cir[(k % len(memo_cir)) - 1] + 1)
else:
    print(memo[k] + 1)


Fixed submission:
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
memo = []
memo_cir = []
visited = [False] * n
index = 0
flag = True
while flag:
    if visited[index]:
        flag = False
    else:
        memo.append(index)
        visited[index] = True
        index = a[index] - 1
flag = False
for i_ in memo:
    if i_ == index:
        flag = True
    if flag:
        memo_cir.append(i_)
if k + 1 >= (len(memo) - len(memo_cir)):
    k -= (len(memo) - len(memo_cir) - 1)
    print(memo_cir[(k % len(memo_cir)) - 1] + 1)
else:
    print(memo[k] + 1)

Buggy python submission (verdict: Runtime Error):
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
visited = [0] * n
f = True
visited[0] = 1
index = 0
memo = [0]
while f:
    index = a[index] - 1
    if visited[index]:
        v = index
        break
    memo.append(index)
    visited[index] = 1
f = False
list_ = []
for i in range(len(memo)):
    if not (f) and memo[i] == v:
        f = True
        list_.append(memo[i])
    if f:
        list_.append(memo[i])
if k + 1 > len(memo):
    k -= len(memo)
    if (k + 1) % len(list_) == 0:
        i = len(list_) - 1
    else:
        i = (k + 1) % len(list_) - 1
    print(memo[i] + 1)
else:
    print(memo[k + 1] + 1)


Fixed submission:
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
memo = []
memo_cir = []
visited = [False] * n
index = 0
flag = True
while flag:
    if visited[index]:
        flag = False
    else:
        memo.append(index)
        visited[index] = True
        index = a[index] - 1
flag = False
for i_ in memo:
    if i_ == index:
        flag = True
    if flag:
        memo_cir.append(i_)
if k + 1 >= (len(memo) - len(memo_cir)):
    k -= (len(memo) - len(memo_cir) - 1)
    print(memo_cir[(k % len(memo_cir)) - 1] + 1)
else:
    print(memo[k] + 1)

Buggy python submission (verdict: Runtime Error):
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
visited = [0] * n
f = True
visited[0] = 1
index = 0
memo = [0]
while f:
    index = a[index] - 1
    if visited[index]:
        v = a[index] - 1
        break
    memo.append(index)
    visited[index] = 1
f = False
list_ = []
for i in range(len(memo)):
    if not (f) and memo[i] == v:
        f = True
        list_.append(memo[i])
    if f:
        list_.append(memo[i])
if (k + 1) % len(list_) == 0:
    i = len(list_) - 1
else:
    i = (k + 1) % len(list_) - 1
print(memo[i] + 1)


Fixed submission:
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
memo = []
memo_cir = []
visited = [False] * n
index = 0
flag = True
while flag:
    if visited[index]:
        flag = False
    else:
        memo.append(index)
        visited[index] = True
        index = a[index] - 1
flag = False
for i_ in memo:
    if i_ == index:
        flag = True
    if flag:
        memo_cir.append(i_)
if k + 1 >= (len(memo) - len(memo_cir)):
    k -= (len(memo) - len(memo_cir) - 1)
    print(memo_cir[(k % len(memo_cir)) - 1] + 1)
else:
    print(memo[k] + 1)