Problem p02684 (python)

Fixed submission:
import collections


def main():
    N, K = list(map(int, input().split(' ')))
    A  = list(map(int, input().split(' ')))
    
    where_li = []
    pre = 0
    for i in range(N):
        where_li.append(A[pre])
        pre = A[pre]-1
    
    len_w = len(where_li)
    a = where_li.index(A[pre])
    # print(a)
    mod_s = len_w-a # ループにかかる歩数
    # print(where_li)
    if K<=len_w:
        print(where_li[K-1])
    else:
        nokori = K - len_w
        if nokori % mod_s==0:
            d = mod_s
        else:
            d = nokori % mod_s
        print(where_li[d-1+a])

main()

Buggy submission (verdict: Runtime Error):
import collections


def main():
    N, K = list(map(int, input().split(' ')))
    An  = list(map(int, input().split(' ')))
    
    dic = {}
    li = []
    STAMP = [0 for _ in range(N)]
    for i in range(len(An)):
        dic[str(i+1)] = An[i]

    status = 1
    ittatokoro = [status]
    for k in range(K):
        pre_s = status
        status = dic[str(status)]
        # print(k, pre_s, status)
        if pre_s == status:
            print(pre_s)
            return
        
        if status not in ittatokoro:
            ittatokoro.append(status)
        else:
            ittatokoro.append(status)
            break
    # print('最後の位置:', status) # 
    # print(ittatokoro, k)
    
    # c = collections.Counter(ittatokoro).most_common()[0] # nがk回数
    # K += 1 #  saisyo 1 ni irus
    START = 0
    for i, iku in enumerate(ittatokoro):
        if iku == status:
            START = i
            break
    # print('START:', START)
    
    S_ittatoko = ittatokoro[:START]
    junkan = ittatokoro[START:-1]
    len_junkan = len(junkan)
    # print('junkan:', junkan, len_junkan, S_ittatoko)
    
    junkannmadeno_hosuu = K - START
    num_junkan_kaisuu = junkannmadeno_hosuu // len_junkan
    amari_junkan_kaisuu = junkannmadeno_hosuu % len_junkan
    # print('循環回数:', num_junkan_kaisuu, amari_junkan_kaisuu)

    total = num_junkan_kaisuu * len_junkan + amari_junkan_kaisuu
    
    nokori = junkannmadeno_hosuu - total
    print(junkan[amari_junkan_kaisuu])
    
    
    
main()


# 7 4569534
# 6 5 2 5 5 3 2

Buggy submission (verdict: Runtime Error):
import collections


def main():
    N, K = list(map(int, input().split(' ')))
    An  = list(map(int, input().split(' ')))
    
    dic = {}
    li = []
    mae = []
    ato = []
    for i in range(len(An)):
        dic[str(i+1)] = An[i]
        li.append([i+1, An[i]])
        # mae.append(i+1)
        # ato.append(An[i])

    status = 1
    ittatokoro = [1]
    for k in range(K):
        pre_s = status
        status = dic[str(status)]
        if status not in ittatokoro:
            ittatokoro.append(status)
        else:
            ittatokoro.append(status)
            break
    # print('最後の位置:', status) # 
    # print(ittatokoro, k)
    
    # c = collections.Counter(ittatokoro).most_common()[0] # nがk回数
    # K += 1 #  saisyo 1 ni irus
    START = 0
    for i, iku in enumerate(ittatokoro):
        if iku == status:
            START = i
            break
    # print('START:', START)
    
    S_ittatoko = ittatokoro[:START]
    junkan = ittatokoro[START:-1]
    len_junkan = len(junkan)
    # print('junkan:', junkan, len_junkan, S_ittatoko)
    
    junkannmadeno_hosuu = K - START
    num_junkan_kaisuu = junkannmadeno_hosuu // len_junkan
    amari_junkan_kaisuu = junkannmadeno_hosuu % len_junkan
    # print('循環回数:', num_junkan_kaisuu, amari_junkan_kaisuu)

    total = num_junkan_kaisuu * len_junkan + amari_junkan_kaisuu
    
    nokori = junkannmadeno_hosuu - total
    print(junkan[amari_junkan_kaisuu])
    
    
    
main()