Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n,k = map(int, input().split())
    town = list(map(int, input().split()))
    set_t = set()
    list_t = []
    current = 1
    is_loop = False
    counter = {}
    for i in range(k):
        current = town[current-1]
        set_t.add(current)
        list_t.append(current)
        if len(list(set_t)) != len(list_t):
            loop_start = counter[list_t[-1]]
            is_loop = True
            break
        counter[current] = i
    if not is_loop:
        print(list_t[-1])
    else:
        loop_list = list_t[loop_start:-1]
        am = (k-i) % len(loop_list)
        print(loop_list[am-1])

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    n,k = map(int, input().split())
    town = list(map(int, input().split()))
    town = [i-1 for i in town]
    visited = [-1] * n
    current_town = 0
    current_order = 0
    memo = []
    while visited[current_town] == -1:
        visited[current_town] = current_order
        memo.append(current_town)
        current_town = town[current_town]
        current_order += 1
    loop_start = visited[current_town]
    if k < len(memo):
        print(memo[k]+1)
    else:
        loop_length = len(memo) - loop_start
        k =  (k - loop_start) % loop_length
        print(memo[k+loop_start]+1)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n,k = map(int, input().split())
    town = list(map(int, input().split()))
    dic = {}
    for i,t in enumerate(town):
        dic[i+1] = t
    set_t = set()
    list_t = []
    current = 1
    is_loop = False
    counter = {}
    for i in range(k):
        current = dic[current]
        set_t.add(current)
        list_t.append(current)
        if len(list(set_t)) != len(list_t):
            loop_start = counter[list_t[-1]]
            is_loop = True
            break
        counter[current] = i
    if not is_loop:
        print(list_t[-1])
    else:
        loop_list = list_t[loop_start:-1]
        am = (k-i) % len(loop_list)
        print(loop_list[am-1])

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    n,k = map(int, input().split())
    town = list(map(int, input().split()))
    town = [i-1 for i in town]
    visited = [-1] * n
    current_town = 0
    current_order = 0
    memo = []
    while visited[current_town] == -1:
        visited[current_town] = current_order
        memo.append(current_town)
        current_town = town[current_town]
        current_order += 1
    loop_start = visited[current_town]
    if k < len(memo):
        print(memo[k]+1)
    else:
        loop_length = len(memo) - loop_start
        k =  (k - loop_start) % loop_length
        print(memo[k+loop_start]+1)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n,k = map(int, input().split())
    town = list(map(int, input().split()))
    dic = {}
    for i,t in enumerate(town):
        dic[i+1] = t
    set_t = set()
    list_t = []
    current = 1
    is_loop = False
    for i in range(k):
        current = dic[current]
        set_t.add(current)
        list_t.append(current)
        if len(list(set_t)) != len(list_t):
            loop_start = list_t[-1]
            is_loop = True
            break
    if not is_loop:
        print(list_t[-1])
    else:
        loop_list = list_t[list_t.index(loop_start):-1]
        am = (k-i) % len(loop_list)
        print(loop_list[am-1])

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    n,k = map(int, input().split())
    town = list(map(int, input().split()))
    town = [i-1 for i in town]
    visited = [-1] * n
    current_town = 0
    current_order = 0
    memo = []
    while visited[current_town] == -1:
        visited[current_town] = current_order
        memo.append(current_town)
        current_town = town[current_town]
        current_order += 1
    loop_start = visited[current_town]
    if k < len(memo):
        print(memo[k]+1)
    else:
        loop_length = len(memo) - loop_start
        k =  (k - loop_start) % loop_length
        print(memo[k+loop_start]+1)
if __name__ == '__main__':
    main()