Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())
a=list(map(int,input().split()))
town=[1]
cnt=0
loop=0
index=0
while(cnt == index):
    town.append(a[town[-1]-1])
    cnt+=1
    index=town.index(town[-1])

town.pop(-1)
loop=cnt-index


if k<index:
    print(town[k])
else:
    k-=index
    k%=loop
    print(town[index+k])

Fixed submission:
n,k=map(int,input().split())
a=list(map(int,input().split()))

s=[]
ordn=[]
for i in range(n+1):
    ordn.append(-1)

v=1
while(ordn[v] == -1):
    ordn[v]=len(s)
    s.append(v)
    v=a[v-1]

c=len(s)-ordn[v]
l=ordn[v]

if k<l:
    print(s[k])
else:
    k -= l
    k%=c
    print(s[l+k])

Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
a=list(map(int,input().split()))
town=[1]
cnt=0
loop=0
index=0
while(len(town) <= k):
    town.append(a[town[-1]-1])
    cnt+=1
    index=len(town)-1
    if cnt != index:
        loop = cnt - index
        town.pop(-1)
        break

if k<index:
    print(town[k])
else:
    k-=index
    k%=loop
    print(town[index+k])


Fixed submission:
n,k=map(int,input().split())
a=list(map(int,input().split()))

s=[]
ordn=[]
for i in range(n+1):
    ordn.append(-1)

v=1
while(ordn[v] == -1):
    ordn[v]=len(s)
    s.append(v)
    v=a[v-1]

c=len(s)-ordn[v]
l=ordn[v]

if k<l:
    print(s[k])
else:
    k -= l
    k%=c
    print(s[l+k])

Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
a=list(map(int,input().split()))
town=[1]
cnt=0
loop=0
index=0
while(len(town) <= k):
    town.append(a[town[-1]-1])
    cnt+=1
    index=town.index(town[-1])
    if cnt != index:
        loop = cnt - index
        town.pop(-1)
        break

if k<index:
    print(town[k])
else:
    k-=index
    k%=loop
    print(town[index+k])

Fixed submission:
n,k=map(int,input().split())
a=list(map(int,input().split()))

s=[]
ordn=[]
for i in range(n+1):
    ordn.append(-1)

v=1
while(ordn[v] == -1):
    ordn[v]=len(s)
    s.append(v)
    v=a[v-1]

c=len(s)-ordn[v]
l=ordn[v]

if k<l:
    print(s[k])
else:
    k -= l
    k%=c
    print(s[l+k])

Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
a=list(map(int,input().split()))
town=[1]
cnt=0
loop=0
index=0
while(len(town) <= k):
    town.append(a[town[-1]-1])
    cnt+=1
    index=town.index(town[-1])
    if cnt != index:
        loop = cnt - index
        town.pop(-1)
        break

if k<index:
    print(town[k])
else:
    for i in range(0,index):
        town.pop(0)
    print(town[(k-index)%loop])

Fixed submission:
n,k=map(int,input().split())
a=list(map(int,input().split()))

s=[]
ordn=[]
for i in range(n+1):
    ordn.append(-1)

v=1
while(ordn[v] == -1):
    ordn[v]=len(s)
    s.append(v)
    v=a[v-1]

c=len(s)-ordn[v]
l=ordn[v]

if k<l:
    print(s[k])
else:
    k -= l
    k%=c
    print(s[l+k])

Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
a=list(map(int,input().split()))
town=[1]
cnt=0
loop=0
index=0
while(len(town) <= k):
    town.append(a[town[-1]-1])
    cnt+=1
    index=town.index(town[-1])
    if cnt != index:
        loop = cnt - index
        town.pop(-1)
        break
town2=[]
if k<index:
    print(town[k])
else:
    for i in range(index,len(town)):
        town2.append(town[i])
    print(town2[(k-index)%loop])


Fixed submission:
n,k=map(int,input().split())
a=list(map(int,input().split()))

s=[]
ordn=[]
for i in range(n+1):
    ordn.append(-1)

v=1
while(ordn[v] == -1):
    ordn[v]=len(s)
    s.append(v)
    v=a[v-1]

c=len(s)-ordn[v]
l=ordn[v]

if k<l:
    print(s[k])
else:
    k -= l
    k%=c
    print(s[l+k])

Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
a=list(map(int,input().split()))
town=[1]
cnt=0
loop=0
index=0
while(len(town) <= 10):
    town.append(a[town[-1]-1])
    cnt+=1
    index=town.index(town[-1])
    if cnt != index:
        loop = cnt - index
        town.pop(-1)
        break
town2=[]
if k<index:
    print(town[k])
else:
    for i in range(index,len(town)):
        town2.append(town[i])
    print(town2[(k-index)%loop])

Fixed submission:
n,k=map(int,input().split())
a=list(map(int,input().split()))

s=[]
ordn=[]
for i in range(n+1):
    ordn.append(-1)

v=1
while(ordn[v] == -1):
    ordn[v]=len(s)
    s.append(v)
    v=a[v-1]

c=len(s)-ordn[v]
l=ordn[v]

if k<l:
    print(s[k])
else:
    k -= l
    k%=c
    print(s[l+k])

Buggy python submission (verdict: Time Limit Exceeded):
N,K=(int(x) for x in input().split())
A=list(map(int, input().split()))

next=A[0]
for i in range(K-1):
    next=A[next-1]
print(next)

Fixed submission:
n,k=map(int,input().split())
a=list(map(int,input().split()))

s=[]
ordn=[]
for i in range(n+1):
    ordn.append(-1)

v=1
while(ordn[v] == -1):
    ordn[v]=len(s)
    s.append(v)
    v=a[v-1]

c=len(s)-ordn[v]
l=ordn[v]

if k<l:
    print(s[k])
else:
    k -= l
    k%=c
    print(s[l+k])