Buggy python submission (verdict: Runtime Error):
n, k = list(map(int,input().split()))
a = list(map(int,input().split()))

now=1
count=0

cities = [1]+[0]*(n)

for i in range(1,k+1):
    tmp=a[now-1]
    if tmp in cities[:i]:
        init_city=cities.index(tmp)
        if init_city>k:
            print(cities[k])
        else:
            print(cities[init_city:i][(k-init_city)%(i-init_city)])
        break
    else:
        cities[i]=tmp
        now=tmp
else:
    print(tmp_city[init_city+1])

Fixed submission:
n, k = map(int, input().split())
A = list(map(int, input().split()))
D = [0] * (n+1)
now = 1
for i in range(1, k+1):
    tmp = A[now-1]
    if not D[tmp]:
        now = tmp
        D[now] += i
    else:
        pre = D[tmp]
        loop = i - D[tmp]
        if loop>1:
            now=D.index((k - pre) % loop + pre)
        else:
            now=tmp
        break
print(now)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = list(map(int,input().split()))
a = list(map(int,input().split()))
# 循環を調べる
c=0
count=0
init_city=0
tmp_city = [0] * (n+1)

while (1):
    count+=1
#     print(c)
    tmp=a[c]
    if k==count:
        tmp_city.append(tmp)
        tmp_city=[1]+tmp_city
        break
    if tmp == 1:
        init_city=0
        tmp_city=[1]+tmp_city
        break
    if tmp in tmp_city:
        init_city=tmp_city.index(tmp)+1
        tmp_city=[1]+tmp_city
        break
    tmp_city[count-1]=tmp
    c=tmp-1
if k==count:
    print(tmp_city[-1])
else:
    print(tmp_city[init_city:count][(k-(init_city))%(count-init_city)])

Fixed submission:
n, k = map(int, input().split())
A = list(map(int, input().split()))
D = [0] * (n+1)
now = 1
for i in range(1, k+1):
    tmp = A[now-1]
    if not D[tmp]:
        now = tmp
        D[now] += i
    else:
        pre = D[tmp]
        loop = i - D[tmp]
        if loop>1:
            now=D.index((k - pre) % loop + pre)
        else:
            now=tmp
        break
print(now)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = list(map(int,input().split()))
a = list(map(int,input().split()))
# 循環を調べる
tmp_city=[]
c=0
count=0
init_city=0

while (1):
    count+=1
    tmp=a[c]
    if k==count:
        tmp_city.append(tmp)
        tmp_city=[1]+tmp_city
        break
    if tmp == 1:
        init_city=0
        tmp_city=[1]+tmp_city
        break
    if tmp in tmp_city:
        init_city=tmp_city.index(tmp)+1
        tmp_city=[1]+tmp_city
        break
    tmp_city.append(tmp)
    c=tmp-1
if k==count:
    print(tmp_city[-1])
else:
    print(tmp_city[init_city:][(k-(init_city))%len(tmp_city[init_city:])])

Fixed submission:
n, k = map(int, input().split())
A = list(map(int, input().split()))
D = [0] * (n+1)
now = 1
for i in range(1, k+1):
    tmp = A[now-1]
    if not D[tmp]:
        now = tmp
        D[now] += i
    else:
        pre = D[tmp]
        loop = i - D[tmp]
        if loop>1:
            now=D.index((k - pre) % loop + pre)
        else:
            now=tmp
        break
print(now)

Buggy python submission (verdict: Runtime Error):
n, k = list(map(int,input().split()))
# a = list(map(int,input().split()))
# 循環を調べる
tmp_city=[]
c=0
count=0
init_city=0

while (1):
    count+=1
    tmp=a[c]
    if k==count:
        tmp_city.append(tmp)
        tmp_city=[1]+tmp_city
        break
    if tmp == 1:
        init_city=0
        tmp_city=[1]+tmp_city
        break
    if tmp in tmp_city:
        init_city=tmp_city.index(tmp)+1
        tmp_city=[1]+tmp_city
        break
    tmp_city.append(tmp)
    c=tmp-1
if k==count:
    print(tmp_city[-1])
else:
    print(tmp_city[init_city:][(k-(init_city))%len(tmp_city[init_city:])])

Fixed submission:
n, k = map(int, input().split())
A = list(map(int, input().split()))
D = [0] * (n+1)
now = 1
for i in range(1, k+1):
    tmp = A[now-1]
    if not D[tmp]:
        now = tmp
        D[now] += i
    else:
        pre = D[tmp]
        loop = i - D[tmp]
        if loop>1:
            now=D.index((k - pre) % loop + pre)
        else:
            now=tmp
        break
print(now)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = list(map(int,input().split()))
a = list(map(int,input().split()))
# 循環を調べる
tmp_city=[]
c=0
init_city=0

while (1):
    tmp=a[c]
    if tmp in tmp_city:
        init_city=tmp_city.index(tmp)+1
        tmp_city=[1]+tmp_city
        break
    tmp_city.append(tmp)
    c=tmp-1
if len(tmp_city)>k:
    print(tmp_city[k])
else:
    print(tmp_city[init_city:][(k-(init_city))%len(tmp_city[init_city:])])

Fixed submission:
n, k = map(int, input().split())
A = list(map(int, input().split()))
D = [0] * (n+1)
now = 1
for i in range(1, k+1):
    tmp = A[now-1]
    if not D[tmp]:
        now = tmp
        D[now] += i
    else:
        pre = D[tmp]
        loop = i - D[tmp]
        if loop>1:
            now=D.index((k - pre) % loop + pre)
        else:
            now=tmp
        break
print(now)