Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [-1] + A
memo = [1]
i = 0
town = 1
judge = True
while judge:
    town = A[town]
    i += 1
    if town in memo:
        judge = False
    memo.append(town)
outlier = memo.index(memo[-1])
T = len(memo) - memo.index(memo[-1]) - 1
if k <= outlier:
    print(memo[k])
else:
    print(memo[outlier + (k-outlier) % T])

Fixed submission:
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [-1] + A
memo = []
loop = []
visit = [0]*n
town = 1
while visit[town-1] < 2:
    if visit[town-1] == 0:
        memo.append(town)
    else:
        loop.append(town)
    visit[town-1] += 1
    town = A[town]
outlier = len(memo) - len(loop) 
T = len(loop)
if k <= outlier:
    print(memo[k])
else:
    print(loop[(k-outlier) % T])

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [-1] + A
memo = [1]
def func(i, town=1):
    global memo
    arrival = A[town]
    memo.append(arrival)
    if memo.count(arrival) == 2:
        return memo
    return func(i+1, A[town])

table = func(0, 1)
index = table.index(table[-1])
r = (k - index) % (len(table)-1 - index)
print(table[index+r])

Fixed submission:
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [-1] + A
memo = []
loop = []
visit = [0]*n
town = 1
while visit[town-1] < 2:
    if visit[town-1] == 0:
        memo.append(town)
    else:
        loop.append(town)
    visit[town-1] += 1
    town = A[town]
outlier = len(memo) - len(loop) 
T = len(loop)
if k <= outlier:
    print(memo[k])
else:
    print(loop[(k-outlier) % T])

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [-1] + A
if A[1] != 1:
    memo = [1]
    def dfs(i, town=1):
        global memo
        arrival = A[town]
        if arrival in memo:
            memo.append(arrival)
            return memo
        memo.append(arrival)
        return dfs(i+1, A[town])

    table = dfs(0, 1)
    indexes = [i for i, x in enumerate(table) if x == table[-1]]
    q = indexes[0]
    r = (k - q) % (indexes[1] - q)
    print(table[q+r])
else:
    print(1)

Fixed submission:
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [-1] + A
memo = []
loop = []
visit = [0]*n
town = 1
while visit[town-1] < 2:
    if visit[town-1] == 0:
        memo.append(town)
    else:
        loop.append(town)
    visit[town-1] += 1
    town = A[town]
outlier = len(memo) - len(loop) 
T = len(loop)
if k <= outlier:
    print(memo[k])
else:
    print(loop[(k-outlier) % T])

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [-1] + A
memo = [1]
def dfs(i, town=1):
    global memo
    arrival = A[town]
    if arrival in memo:
        memo.append(arrival)
        return memo
    memo.append(arrival)
    return dfs(i+1, A[town])

table = dfs(0, 1)
indexes = [i for i, x in enumerate(table) if x == table[-1]]
q = indexes[0]
r = (k - q) % (indexes[1] - q)
print(table[q+r])

Fixed submission:
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [-1] + A
memo = []
loop = []
visit = [0]*n
town = 1
while visit[town-1] < 2:
    if visit[town-1] == 0:
        memo.append(town)
    else:
        loop.append(town)
    visit[town-1] += 1
    town = A[town]
outlier = len(memo) - len(loop) 
T = len(loop)
if k <= outlier:
    print(memo[k])
else:
    print(loop[(k-outlier) % T])