Problem: The Kingdom of Takahashi has N towns, numbered 1 through N.  There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.  Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.  Help the king by writing a program that answers this question.

Submission attempts (in order):
import numpy as np
def read_int():
    return int(input())

def read_ints():
    return map(int, input().split())

def read_lines(N):
    return [input() for i in range(N)]

def main():
    N, K = read_ints()
    towns = np.array(list(read_ints())) - 1
    portals = {i: towns[i] for i in towns}

    loop = []
    next_town = 0
    while next_town not in loop:
        loop.append(next_town)
        next_town = portals[next_town]
    loop.append(next_town)

    i = loop.index(next_town)
    all_loop = loop
    pre_loop, loop = loop[:i], loop[i:]

    if K < len(pre_loop):
        print(pre_loop[K] + 1)
    elif len(pre_loop) <= K < len(all_loop):
        print(loop[K - len(pre_loop)] + 1)
    else:
        print(loop[(K - len(pre_loop)) % len(loop)] + 1)
        



if __name__ == '__main__':
    main()


def read_int():
    return int(input())

def read_ints():
    return map(int, input().split())

def read_lines(N):
    return [input() for i in range(N)]

def main():
    N, K = read_ints()
    towns = [int(input()) - 1 for i in range(N)]
    portals = {i: towns[i] for i in towns}

    loop = []
    next_town = 0
    while next_town not in loop:
        loop.append(next_town)
        next_town = portals[next_town]
    loop.append(next_town)

    i = loop.index(next_town)
    all_loop = loop
    pre_loop, loop = loop[:i], loop[i:]

    if K < len(pre_loop):
        print(pre_loop[K] + 1)
    elif len(pre_loop) <= K < len(all_loop):
        print(loop[K - len(pre_loop)] + 1)
    else:
        print(loop[(K - len(pre_loop)) % len(loop)] + 1)
        



if __name__ == '__main__':
    main()

import numpy as np
def read_int():
    return int(input())

def read_ints():
    return map(int, input().split())

def read_lines(N):
    return [input() for i in range(N)]

def main():
    N, K = read_ints()
    towns = np.array(list(read_ints())) - 1
    #portals = {i: towns[i] for i in towns}
    portals = towns

    loop = []
    next_town = 0
    while next_town not in loop:
        loop.append(next_town)
        next_town = portals[next_town]
    loop.append(next_town)

    i = loop.index(next_town)
    all_loop = loop
    pre_loop, loop = loop[:i], loop[i:]

    if K < len(pre_loop):
        print(pre_loop[K])
    elif len(pre_loop) <= K < len(all_loop):
        print(loop[K - len(pre_loop)])
    else:
        print(loop[(K - len(pre_loop)) % len(loop)])
        



if __name__ == '__main__':
    main()

def read_int():
    return int(input())

def read_ints():
    return map(int, input().split())

def read_lines(N):
    return [input() for i in range(N)]

def main():
    N, K = read_ints()
    towns = [int(t) -1  for t in input().split() ]
    #print(N, K , towns)

    loop = []
    loop_set = set()
    next_town = towns[0]
    while next_town not in loop_set:
        #print(f"next_t={next_town}")
        loop.append(next_town)
        loop_set.add(next_town)
        next_town = towns[next_town]

    i = loop.index(next_town)
    all_loop = loop
    pre_loop = loop[:i]
    loop = loop[i:]
    pre_length = len(pre_loop)
    loop_length = len(loop)

    """
    for k in range(1, 10):
        if k - 1  < len(all_loop):
            print(f"k={k}", all_loop[k-1] + 1)
        else:
            i = ((k -1 - len(pre_loop)) % loop_length)
            print(i, pre_loop, loop)
            print(f"k={k}", loop[i]+1)
    """

    #print(pre_loop, loop)
    if K - 1  < len(all_loop):
        print(all_loop[K-1] + 1)
    else:
        i = ((K -1 - pre_length) % loop_length)
        #print(i, pre_loop, loop)
        print(loop[i]+1)

if __name__ == '__main__':
    main()

Verdicts: Runtime Error

Runtime Error

Wrong Answer

Accepted