Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
town=[0]
i=0
while True:
  idx=town[-1]
  if a[idx] in town:
    s=a[idx]
    i+=1
    break
  else:
    i+=1
    town.append(a[idx])
if i!=n:
  idp=town.index(s)
  chain=len(town[:idp])
  cycle=len(town[idp:])
  if k<=chain:
    print(town[k]+1)
  else:
    p=(k-chain)%cycle + chain
    print(town[p]+1)
else:
  print(town[k%n]+1)

Fixed submission:
n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
town=[0]
set={0}
i=0
while True:
  idx=town[-1]
  if a[idx] in set:
    s=a[idx]
    i+=1
    break
  else:
    i+=1
    town.append(a[idx])
    set.add(a[idx])
if i!=n:
  idp=town.index(s)
  chain=len(town[:idp])
  cycle=len(town[idp:])
  if k<=chain:
    print(town[k]+1)
  else:
    p=(k-chain)%cycle + chain
    print(town[p]+1)
else:
  print(town[k%n]+1)

Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
town=[0]
s=0
while True:
  idx=town[-1]
  if a[idx] in town:
    s=a[idx]
    break
  else:
    town.append(a[idx])

idp=town.index(s)
chain=len(town[:idp])
cycle=len(town[idp:])
if k<=chain:
  print(town[k]+1)
else:
  p=(k-chain)%cycle + chain
  print(town[p]+1)

Fixed submission:
n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
town=[0]
set={0}
i=0
while True:
  idx=town[-1]
  if a[idx] in set:
    s=a[idx]
    i+=1
    break
  else:
    i+=1
    town.append(a[idx])
    set.add(a[idx])
if i!=n:
  idp=town.index(s)
  chain=len(town[:idp])
  cycle=len(town[idp:])
  if k<=chain:
    print(town[k]+1)
  else:
    p=(k-chain)%cycle + chain
    print(town[p]+1)
else:
  print(town[k%n]+1)

Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
town=[0]
s=0
while True:
  idx=town[-1]
  if a[idx] in town:
    s=a[idx]
    break
  else:
    town.append(a[idx])

idp=town.index(s)
chain=len(town[:idp])
cycle=len(town[idp:])
if k<=chain:
  print(town[k]+1)
else:
  p=(k-chain)%cycle + chain
  print(town[p]+1)

Fixed submission:
n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
town=[0]
set={0}
i=0
while True:
  idx=town[-1]
  if a[idx] in set:
    s=a[idx]
    i+=1
    break
  else:
    i+=1
    town.append(a[idx])
    set.add(a[idx])
if i!=n:
  idp=town.index(s)
  chain=len(town[:idp])
  cycle=len(town[idp:])
  if k<=chain:
    print(town[k]+1)
  else:
    p=(k-chain)%cycle + chain
    print(town[p]+1)
else:
  print(town[k%n]+1)

Buggy python submission (verdict: Runtime Error):
import math
n,k=map(int,input().split())
a_lis=list(map(int,input().split()))
direct={ i+1:a_lis[i] for i in range(len(a_lis))}
 
d_lis=[1]
j=direct[1]
while True:
  d_lis.append(j)
  j=direct[j]
  if j==1:
    break
dp=d_lis*(math.floor(k/len(d_lis))+1)
 
print(dp[k-1])

Fixed submission:
n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
town=[0]
set={0}
i=0
while True:
  idx=town[-1]
  if a[idx] in set:
    s=a[idx]
    i+=1
    break
  else:
    i+=1
    town.append(a[idx])
    set.add(a[idx])
if i!=n:
  idp=town.index(s)
  chain=len(town[:idp])
  cycle=len(town[idp:])
  if k<=chain:
    print(town[k]+1)
  else:
    p=(k-chain)%cycle + chain
    print(town[p]+1)
else:
  print(town[k%n]+1)

Buggy python submission (verdict: Runtime Error):
import math
n,k=map(int, input().split())
a_lis=[map(int,input().split())]
direct={ i+1:a_lis[i] for i in range(n)}

d_lis=[1]
j=direct[1]
while True:
  d_lis.append(j)
  j=direct[j]
  if j==1:
    break

dp=d_lis*(math.floor(k/len(d_lis))+1)

print(dp[k-1])
  

Fixed submission:
n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
town=[0]
set={0}
i=0
while True:
  idx=town[-1]
  if a[idx] in set:
    s=a[idx]
    i+=1
    break
  else:
    i+=1
    town.append(a[idx])
    set.add(a[idx])
if i!=n:
  idp=town.index(s)
  chain=len(town[:idp])
  cycle=len(town[idp:])
  if k<=chain:
    print(town[k]+1)
  else:
    p=(k-chain)%cycle + chain
    print(town[p]+1)
else:
  print(town[k%n]+1)