qyliang/CodeNanoFix huggingface dataset:
Buggy code:
from collections import defaultdict
n,k=map(int,input().split())
l=list(map(int,input().split()))
d=defaultdict(int)
for i in range(n):
    d[i+1]=l[i]
visited=set()
l=[]
i=1
while True:
    l.append(i)
    if i not in visited:
        visited.add(i)
    else:
        break
    i=d[i]
first=l.index(l[-1])
if k<first:
    print(l[k])
else:
    k=(len(l)-first)%((len(l)-1)-first)
    k+=first
    print(l[k])

Fixed code:
from collections import defaultdict
n,k=map(int,input().split())
l=list(map(int,input().split()))
d=defaultdict(int)
for i in range(n):
    d[i+1]=l[i]
visited=set()
l=[]
i=1
while True:
    l.append(i)
    if i not in visited:
        visited.add(i)
    else:
        break
    i=d[i]
first=l.index(l[-1])
if k<first:
    print(l[k])
else:
    k=(k-first)%((len(l)-1)-first)
    k+=first
    print(l[k])
Buggy code:
N, K = map(int, input().split())
A = tuple( map(int, input().split()) )

L = [0 for i in range(N+1)]
L[1] = 1
x = 1
t = 1
while K>0:
  t += 1
  K -= 1
  x = A[x]
  if L[x] != 0:
    p = t - L[x]
    K %= p
    break
  else:
    L[x] = t
    
while K>0:
  x = A[x]
  K -= 1
print(x)

Fixed code:
N, K = map(int, input().split())
A = tuple( map(int, input().split()) )

L = [0 for i in range(N+1)]
L[1] = 1
x = 1
t = 1
while K>0:
  t += 1
  K -= 1
  x = A[x-1]
  if L[x] != 0:
    p = t - L[x]
    K %= p
    break
  else:
    L[x] = t
    
while K>0:
  x = A[x-1]
  K -= 1
print(x)
Buggy code:
n, k = map(int, input().split())
a = list(map(int, input().split()))

visited = [0] * (n + 1)
now = 1
check = 2 * n
loop_start = 1
while check > 0:
    if visited[now - 1] == 1:
        loop_start = now
        break
    else:
        visited[now - 1] += 1
        now = a[now - 1]
        check -= 1

visited = [0] * (n + 1)
now = loop_start
check = n
loop = 0
while check > 0:
    if visited[now - 1] == 1:
        break
    else:
        visited[now - 1] += 1
        now = a[now - 1]
        check -= 1
        loop += 1

now = 1
while k > 0:
    if now == loop_start:
        k %= loop
        if k <= 0:
            break
        else:
            now = a[now - 1]
            k = min(k - 1, 0)
    else:
        now = a[now - 1]
        k -= 1

ans = now
print(ans)


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

visited = [0] * (n + 1)
now = 1
check = 2 * n
loop_start = 1
while check > 0:
    if visited[now - 1] == 1:
        loop_start = now
        break
    else:
        visited[now - 1] += 1
        now = a[now - 1]
        check -= 1

visited = [0] * (n + 1)
now = loop_start
check = n
loop = 0
while check > 0:
    if visited[now - 1] == 1:
        break
    else:
        visited[now - 1] += 1
        now = a[now - 1]
        check -= 1
        loop += 1

now = 1
while k > 0:
    if now == loop_start:
        k %= loop
        if k == 0:
            break
    now = a[now - 1]
    k -= 1

print(now)
Buggy code:
from collections import defaultdict
def main():
    n, k = map(int, input().split(" "))
    a = list(map(int, input().split(" ")))
    s = []
    ord = [-1]*(n)
    u = 1
    while ord[u] == -1:
        ord[u] = len(s)
        s.append(u)
        u = a[u-1]
    l = len(s) - ord[u]
    if k < ord[u]:
        print(s[k])
    else:
        print(s[(k-ord[u])%l+ord[u]])

'''
def main():
    n, k = map(int, input().split(" "))
    a = list(map(lambda i: int(i)-1, input().split(" ")))
    s = [0]
    d = defaultdict(lambda:0)
    x = a[0]
    for i in range(n):
        s.append(x)
        x = a[x]

    bb=None
    for i in range(n):
        d[s[i]] += 1
        if d[s[i]] ==2:
            bb=s[i]
            break

    cc = s.index(bb)
    s[cc]=-1
    dd = s.index(bb)
    loop_len = dd-cc
    s[cc]=s[dd]
        
    if bb ==None or k < cc:
        print(s[k]+1)
    else:
        y = (k-cc) % loop_len
        print(s[y+cc]+1)
'''
if __name__ == "__main__":
    main()

Fixed code:
from collections import defaultdict
def main():
    n, k = map(int, input().split(" "))
    a = list(map(int, input().split(" ")))
    s = []
    ord = [-1]*(n+1)
    u = 1
    while ord[u] == -1:
        ord[u] = len(s)
        s.append(u)
        u = a[u-1]
    l = len(s) - ord[u]
    if k < ord[u]:
        print(s[k])
    else:
        print(s[(k-ord[u])%l+ord[u]])

'''
def main():
    n, k = map(int, input().split(" "))
    a = list(map(lambda i: int(i)-1, input().split(" ")))
    s = [0]
    d = defaultdict(lambda:0)
    x = a[0]
    for i in range(n):
        s.append(x)
        x = a[x]

    bb=None
    for i in range(n):
        d[s[i]] += 1
        if d[s[i]] ==2:
            bb=s[i]
            break

    cc = s.index(bb)
    s[cc]=-1
    dd = s.index(bb)
    loop_len = dd-cc
    s[cc]=s[dd]
        
    if bb ==None or k < cc:
        print(s[k]+1)
    else:
        y = (k-cc) % loop_len
        print(s[y+cc]+1)
'''
if __name__ == "__main__":
    main()
Buggy code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
N,K = map(int, input().split())		# 5 7 2
A = [0] + list( map(int, input().split()) )	# 1 2 3 4 5 ...


M = [ [0,0] for _ in range(N+1) ]			#=> [[], [], [], [], [], [], [], []]

p(M)

i = 1
x = y = 0
for k in range(1, K+1):
	x,y = M[i]
	
	if x==0:
		M[i][0] = A[i]
		M[i][1] = k
		i = A[i]
	
	else:
		
		break

else:
	ans = i
	print(ans)
	exit(0)


p(x,y,k)
p("M[1:]", M[1:])

k = k - y

j = k % (k-y)

i = 
for a in range(j):
	x,y = M[i]
	M[i][0] = A[i]
	M[i][1] = k
	i = A[i]

print(i)	

Fixed code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
N,K = map(int, input().split())		# 5 7 2
A = [0] + list( map(int, input().split()) )	# 1 2 3 4 5 ...


M = [ [0,0] for _ in range(N+1) ]			#=> [[], [], [], [], [], [], [], []]

p(M)

i = 1
x = y = 0
for k in range(K):
	x,y = M[i]
	
	if x==0:
		M[i][0] = A[i]
		M[i][1] = k
		i = A[i]
		continue
	
	
	a = K - y
	b = a % (k-y)
	for _ in range(b):
		i = A[i]
	
	p(x,y,k)
	p("M[1:]", M[1:])
	
	break

ans = i
print(ans)
Buggy code:
import sys
l = sys.stdin.readline().strip()
N, K = map(int, l.split())
l = sys.stdin.readline().strip()
A = [0] + list(map(int, l.split())) 
visited = [-1] * (len(A)+1)

remain = K
crt = 1
entrance = -1
while remain > 0:
    remain -= 1
    # check visited
    if visited[crt] == -1:
        visited[crt] = K - remain
        crt = A[crt]
    else:
        entrance = crt
        crt = A[crt]
        break
print(entrance, remain)

if entrance == -1:
    print(crt)
else:
    loop = (K-remain) - visited[entrance] 
 
    later = remain % loop
    for i in range(later):
        crt = A[crt]
    print(crt)


Fixed code:
import sys
l = sys.stdin.readline().strip()
N, K = map(int, l.split())
l = sys.stdin.readline().strip()
A = [0] + list(map(int, l.split())) 
visited = [-1] * (len(A)+1)

remain = K
crt = 1
entrance = -1
while remain > 0:
    remain -= 1
    # check visited
    if visited[crt] == -1:
        visited[crt] = K - remain
        crt = A[crt]
    else:
        entrance = crt
        crt = A[crt]
        break
#print(entrance, remain)

if entrance == -1:
    print(crt)
else:
    loop = (K-remain) - visited[entrance] 
 
    later = remain % loop
    for i in range(later):
        crt = A[crt]
    print(crt)
Buggy code:
import sys
n,k=map(int,input().split())
A=[int(i) for i in input().split()]
d=dict()
for i,a in enumerate(A):
    d[i+1] = a
p=1
li = set()
li.add(1)
for i in range(k):
    #print(p,li)
    p = d[p]
    #print(p,li)
    if p in li:
        cyc =  i+ 1- pidx
        q = 1
        st=0
        while q != p:
            q = d[q]
            st+=1
        k = (k-st) % cyc + st
        r = 1
        #print(k)
        for _ in range(k):
            #print(r)
            r = d[r]
        print(r)
        sys.exit()
    li.add(p)
else:
    print(p)

Fixed code:
import sys
n,k=map(int,input().split())
A=[int(i) for i in input().split()]
d=dict()
for i,a in enumerate(A):
    d[i+1] = a
p=1
li = set()
li.add(1)
for i in range(k):
    #print(p,li)
    p = d[p]
    #print(p,li)
    if p in li:
        cyc =  i + 1
        q = 1
        st=0
        while q != p:
            q = d[q]
            st+=1
        cyc -=st
        k = (k-st) % cyc + st
        r = 1
        #print(k)
        for _ in range(k):
            #print(r)
            r = d[r]
        print(r)
        sys.exit()
    li.add(p)
else:
    print(p)
Buggy code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
d2 = []
hist=[-1]*n
hist[0] = 0
cur = 1
d2.append(1)
st_idx = 0
rp_lgt = 0
for i in range(n):
  cur = a[cur-1]
  if hist[cur-1] >= 0 :
     st_idx = hist[cur-1] 
     rp_lgt = i - st_idx + 1
     break
  else:
    d2.append(cur)
    hist[cur-1] = i
wk = k - st_idx
md = wk % rp_lgt
if k < st_idx:
  ans = d2[k]
else:
  ans = d2[md+st_idx]
print(ans)


Fixed code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
d2 = []
hist=[-1]*(n+1)
hist[0] = 0
cur = 1
d2.append(1)
st_idx = 0
rp_lgt = 1
for i in range(n):
  cur = a[cur-1]
  if hist[cur-1] >= 0 :
     d2.append(cur)
     st_idx = hist[cur-1]
     rp_lgt = i - st_idx + 1 
     break
  else:
    d2.append(cur)
    hist[cur-1] = i + 1
wk = k - st_idx
md = wk % rp_lgt
if k < st_idx:
  ans = d2[k]
else:
  ans = d2[md+st_idx]
print(ans)
Buggy code:
N, K = map(int, input().split())
A = list(map(lambda x: x - 1, map(int, input().split())))
print(A)
current = 0
memo = {
    0: 0
}
for i in range(1, K + 1):
    current = A[current]
    if current in memo:
        # loop length
        length = i - memo[current]
        tmp = (K - memo[current]) % length
        # print(current, tmp)
        for j in range(tmp):
            current = A[current]
        break
    else:
        memo[current] = i
print(current + 1)

Fixed code:
N, K = map(int, input().split())
A = list(map(lambda x: x - 1, map(int, input().split())))
current = 0
memo = {
    0: 0
}
for i in range(1, K + 1):
    current = A[current]
    if current in memo:
        # loop length
        length = i - memo[current]
        tmp = (K - memo[current]) % length
        for j in range(tmp):
            current = A[current]
        break
    else:
        memo[current] = i
print(current + 1)
Buggy code:
import sys

input = sys.stdin.readline


def main():
    N, K = map(int, input().split())
    A = list(map(lambda x: int(x) - 1, input().split()))

    route = [0]
    visited = set([0])
    now_town = 0
    while True:
        next_town = A[now_town]
        if next_town in visited:
            break
        else:
            visited.add(next_town)
            route.append(next_town)
            now_town = next_town

    loop_start = route.index(next_town)
    loop_len = len(route[loop_start:])
    count_before_loop = len(route) - loop_len
    if K <= count_before_loop:
        ans = route[K] + 1
    else:
        r = K % loop_len
        ans = route[loop_start + r] + 1
    print(ans)


if __name__ == "__main__":
    main()


Fixed code:
import sys

input = sys.stdin.readline


def main():
    N, K = map(int, input().split())
    A = list(map(lambda x: int(x) - 1, input().split()))

    route = [0]
    visited = set([0])
    now_town = 0
    while True:
        next_town = A[now_town]
        if next_town in visited:
            break
        else:
            visited.add(next_town)
            route.append(next_town)
            now_town = next_town

    loop_start = route.index(next_town)
    loop_len = len(route[loop_start:])
    count_before_loop = len(route) - loop_len
    if K <= count_before_loop:
        ans = route[K] + 1
    else:
        K -= count_before_loop
        r = K % loop_len
        ans = route[loop_start + r] + 1
    print(ans)


if __name__ == "__main__":
    main()
Buggy code:
N, K = [int(x) for x in input().split()]
A = [int(x) - 1 for x in input().split()]

i = 0
T = []
S = set()
st = 0
while True:
    T.append(i)
    S.add(i)
    i = A[i]
    if i in S:
        st = T.index(i)
        break

print(T)

idx = (K - st) % (len(T) - st)
print(T[st + idx] + 1)





Fixed code:
N, K = [int(x) for x in input().split()]
A = [int(x) - 1 for x in input().split()]

i = 0
T = []
S = set()
st = 0
while True:
    T.append(i)
    S.add(i)
    i = A[i]
    if i in S:
        st = T.index(i)
        break

# print(T)


idx = (K - st) % (len(T) - st)

if K > st:
    print(T[st + idx] + 1)
else:
    print(T[K] + 1)
Buggy code:
N,K = map(int,input().split())
A = [0] + list(map(int,input().split()))
ans = 1
n = 1
check = 1
city = [-1 for i in range(N+1)]
city[1] = 0
roop = 1
i = 0
while(check == 1):
  if i == K:
    check = 0
  else:
    n = A[n]
    i = i + 1 
    if city[n] == -1:
      city[n] = i
    else:
      roop = i - city[n] + 1
      check = 0
roop_num = (K - i)%roop
for i in range(roop_num):
  n = A[n]
print(n)

Fixed code:
N,K = map(int,input().split())
A = [0] + list(map(int,input().split()))
ans = 1
n = 1
check = 1
city = [-1 for i in range(N+1)]
city[1] = 0
roop = 1
i = 0
while(check == 1):
  if i == K:
    check = 0
  else:
    n = A[n]
    i = i + 1 
    if city[n] == -1:
      city[n] = i
    else:
      roop = i - city[n]
      check = 0
roop_num = (K - i)%roop
for i in range(roop_num):
  n = A[n]
print(n)
Buggy code:
n,k = list(map(int,input().split()))
A = list(map(int,input().split()))
tour=[0]
visited=[0]*n
visited[0]=1
next_town = 0
flag=False
for i in range(n):
    next_town=A[next_town]-1
    if i==k:
        flag = True
        ans = next_town
    if visited[next_town]>0:
        first_roop_idx = visited[next_town]
        break
    tour.append(next_town)
    visited[next_town]=i+1
#print(tour)
#print(visited)
#print(first_roop_idx)
if not flag:
    if k<=len(tour):
        ans = tour[k]
    else:
        k-=first_roop_idx
        #print(k)
        k=k%(len(tour)-first_roop_idx)
        #print(k)
        ans = tour[first_roop_idx+k]
print(ans+1)


Fixed code:
n,k = list(map(int,input().split()))
A = list(map(int,input().split()))
tour=[0]
visited=[0]*n
visited[0]=1
next_town = 0
flag=False
for i in range(n):
    next_town=A[next_town]-1
    if i+1==k:
        flag = True
        ans = next_town
        break
    if next_town==0:
        first_roop_idx = 0
        break
    if visited[next_town]>0:
        first_roop_idx = visited[next_town]
        break
    tour.append(next_town)
    visited[next_town]=i+1
#print(tour)
#print(visited)
#print(first_roop_idx)
if not flag:
    if k<=len(tour):
        ans = tour[k]
    else:
        k-=first_roop_idx
        #print("k=",k)
        if len(tour)-first_roop_idx==0:
            k=0
        else:k=k%(len(tour)-first_roop_idx)
        #print("k=",k)
        ans = tour[first_roop_idx+k]
print(ans+1)
#print(flag)
Buggy code:
import sys
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))

kyori = [-1]*n

now = 0;kyori[now] = 0
jyunkan = 0;v = 0
for i in range(n):
    tugi = a[now] - 1
    if kyori[tugi] != -1:
        jyunkan = kyori[now] + 1
        v = tugi
        break
    kyori[tugi] = kyori[now] + 1
    if kyori[tugi] == k:
        print(tugi + 1)
        sys.exit()
    now = tugi
print(kyori)
k -= kyori[v]
mawaru = jyunkan - kyori[v]
amari = k % mawaru

now = v;cunt = 0
for i in range(amari):
    now = a[now] - 1
    
print(now + 1)

Fixed code:
import sys
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))

kyori = [-1]*n

now = 0;kyori[now] = 0
jyunkan = 0;v = 0
for i in range(n):
    tugi = a[now] - 1
    if kyori[tugi] != -1:
        jyunkan = kyori[now] + 1
        v = tugi
        break
    kyori[tugi] = kyori[now] + 1
    if kyori[tugi] == k:
        print(tugi + 1)
        sys.exit()
    now = tugi
k -= kyori[v]
mawaru = jyunkan - kyori[v]
amari = k % mawaru

now = v;cunt = 0
for i in range(amari):
    now = a[now] - 1
    
print(now + 1)
Buggy code:
import sys

def main():
    n, k = map(int, sys.stdin.buffer.readline().split())
    a = [0] + list(map(int, sys.stdin.buffer.readline().split()))

    visited = [0]*(n+1)
    town = 1
    for i in range(1, k+1):
        town = a[town]
        if visited[town] == 0:
            visited[town] = i
        else:
            cyc = (k - visited[town])%(i + visited[town]) + visited[town]
            print(visited.index(cyc))
            exit()
    print(town)

if __name__ == "__main__":
    main()

Fixed code:
import sys

def main():
    n, k = map(int, sys.stdin.buffer.readline().split())
    a = [0] + list(map(int, sys.stdin.buffer.readline().split()))

    visited = [0]*(n+1)
    town = 1
    for i in range(1, k+1):
        town = a[town]
        if visited[town] == 0:
            visited[town] = i
        else:
            cyc = (k - visited[town])%(i - visited[town]) + visited[town]
            print(visited.index(cyc))
            exit()
    print(town)

if __name__ == "__main__":
    main()
Buggy code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
flag=0
now=0
i=0
count=0
c=[0]*N
r=[0]
while flag==0:
  now=A[now]-1
  count+=1
  if count==K:
    flag=1
  elif c[now]==1:
    flag=2
  c[now]=1
  r.append(now)
if flag==1:
  print(now+1)
else:
  o=[i for i, x in enumerate(r) if x == now]
  print(A[r[K%(o[1]-o[0])]-1])

Fixed code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
flag=0
now=0
i=0
count=0
c=[0]*N
r=[0]
c[0]=1
while flag==0:
  now=A[now]-1
  count+=1
  if count==K:
    flag=1
  elif c[now]==1:
    flag=2
  c[now]=1
  r.append(now)
if flag==1:
  print(now+1)
else:
  o=[i for i, x in enumerate(r) if x == now]
  print(r[(K-o[0])%(o[1]-o[0])+o[0]]+1)
Buggy code:
n, k = map(int, input().split())
a = tuple(map(int, input().split()))

i = 0
aa = 0
visited = {}
for i in range(k):
    aa = a[aa] - 1
    if aa in visited:
        s, e = visited[aa], i
        loop = e - s
        k = s + ((k - s) % loop)
        exit(0)
    visited[aa] = i

print(aa + 1)


Fixed code:
n, k = map(int, input().split())
a = tuple(map(int, input().split()))

i = 0
aa = 0
visited = {}
for i in range(k):
    aa = a[aa] - 1
    if aa in visited:
        s, e = visited[aa], i
        loop = e - s
        k = s + (((k-1) - s) % loop)
        print(list(visited.keys())[k] + 1)
        exit(0)
    visited[aa] = i

print(aa+1)
Buggy code:
from copy import copy, deepcopy
from collections import Counter
from math import sqrt, floor, factorial
from itertools import permutations, combinations, combinations_with_replacement
from operator import mul
from functools import reduce
import bisect

MOD = 10**9 + 7
INF = float('inf')

N, K = list(map(int, input().split()))

A = list(map(int, input().split()))

A = [A[i]-1 for i in range(len(A))]

mem = [0]*N
mem2 = [0]*N

idx = 0
count1 = 0
while True:
    mem[idx] += 1
    if mem[idx] == 2:
        break
    count1 += 1
    idx = A[idx]


idx2 = idx
count2 = 0

count2 += 1
idx2 = A[idx2]

while True:
    if idx2 == idx:
        break
    count2 += 1
    idx2 = A[idx2]

tmp = count1 - count2
print(tmp, count2)

if tmp >= K:
    ans = 0
    for _ in range(K):
        ans = A[ans]
else:
    e = (K - tmp) % count2 + tmp
    ans = 0
    for _ in range(e):
        ans = A[ans]

print(ans+1)


Fixed code:
from copy import copy, deepcopy
from collections import Counter
from math import sqrt, floor, factorial
from itertools import permutations, combinations, combinations_with_replacement
from operator import mul
from functools import reduce
import bisect

MOD = 10**9 + 7
INF = float('inf')

N, K = list(map(int, input().split()))

A = list(map(int, input().split()))

A = [A[i]-1 for i in range(len(A))]

mem = [0]*N
mem2 = [0]*N

idx = 0
count1 = 0
while True:
    mem[idx] += 1
    if mem[idx] == 2:
        break
    count1 += 1
    idx = A[idx]


idx2 = idx
count2 = 0

count2 += 1
idx2 = A[idx2]

while True:
    if idx2 == idx:
        break
    count2 += 1
    idx2 = A[idx2]

tmp = count1 - count2

if tmp >= K:
    ans = 0
    for _ in range(K):
        ans = A[ans]
else:
    e = (K - tmp) % count2 + tmp
    ans = 0
    for _ in range(e):
        ans = A[ans]

print(ans+1)
Buggy code:
n, k = map(int, input().split())

num = list(map(int, input().split()))
t = [-1] * n
count = 0
s = 1

while t[s - 1] == -1:
  if count == k:
    break

  t[s - 1] = count
  count += 1
  s = num[s - 1]
else:
  z = (k - t[s - 1]) % (count - t[s - 1])
  print(t.index(t[s - 1] + z + 1)


Fixed code:
n, k = map(int, input().split())

num = list(map(int, input().split()))
t = [-1] * n
count = 0
s = 1

while t[s - 1] == -1:
  if count == k:
    break

  t[s - 1] = count
  count += 1
  s = num[s - 1]
else:
  z = (k - t[s - 1]) % (count - t[s - 1])
  s = t.index(t[s - 1] + z) + 1
print(s)
Buggy code:
import sys
N,K = map(int,input().split())
tele = list(map(int,input().split()))
k = 1
goal = tele[0]
miti = [1]
while (goal in miti)!=1:
    k += 1
    miti.append(goal)
    goal = tele[goal-1]
    if k == K:
        goal = tele[goal-1]
        print(goal)
        sys.exit()
print(miti)

a = miti.index(goal)
loop = k-a
ans = miti[(K-a)%loop+a]

print(ans)


Fixed code:
import sys
N,K = map(int,input().split())
tele = list(map(int,input().split()))
k = 1
flag = [0]*N
goal = tele[0]
miti = [1]
while (flag[goal-1])!=1:
    k += 1
    miti.append(goal)
    flag[goal-1] = 1
    goal = tele[goal-1]
    if k == K:
        print(goal)
        sys.exit()

a = miti.index(goal)
loop = k-a
ans = miti[(K-a)%loop+a]

print(ans)
Buggy code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
journey = [1]
dup = {}
for i in range(N+100000):
    journey.append(A[journey[-1] - 1])

end_idx, start_idx = 0, 0
dup = {}
for i, j in enumerate(journey):
    if j in dup:
        end_idx = i
        break
    else:
        dup[j] = 0
for i, j in enumerate(journey):
    if j == journey[end_idx]:
        start_idx = i
        break
        
if K >= start_idx:
    print(journey[K])
    exit()
b = end_idx - start_idx
m = (K - start_idx) % b
print(journey[start_idx + m])

Fixed code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
journey = [1]
dup = {}
for i in range(N+100000):
    journey.append(A[journey[-1] - 1])

end_idx, start_idx = 0, 0
dup = {}
for i, j in enumerate(journey):
    if j in dup:
        end_idx = i
        break
    else:
        dup[j] = 0
for i, j in enumerate(journey):
    if j == journey[end_idx]:
        start_idx = i
        break

if K <= start_idx:
    print(journey[K])
    exit()
b = end_idx - start_idx
m = (K - start_idx) % b
print(journey[start_idx + m])
Buggy code:
from collections import deque

n,k = map(int,input().split())
li = list(map(int,input().split()))

for i,l in enumerate(li):
  li[i]-=1

done = [0]*n
root = []
que  = deque()

done[0] = 1
root.append(0)
que.append(li[0])


while(len(que)!=0 and len(root)<k+1):
  v = que.pop()
  
  if done[v]!=0:
    root.append(v)
    break

  done[v] = 1
  root.append(v)
  que.append(li[v])

print(root)

if len(root)==k+1:
  print(root[-1]+1)
else:
  fidx = root.index(root[-1])
  lidx = len(root)-1
  lp   = lidx-fidx

  cnt = (k-fidx)%lp

  if cnt==0:
    print(root[-1]+1)
  else:
    que.append(root[-1])
    for i in range(cnt):
      v = que.pop()
      que.append(li[v])
    print(que.pop()+1)

Fixed code:
from collections import deque

n,k = map(int,input().split())
li = list(map(int,input().split()))

for i,l in enumerate(li):
  li[i]-=1

done = [0]*n
root = []
que  = deque()

done[0] = 1
root.append(0)
que.append(li[0])


while(len(que)!=0 and len(root)<k+1):
  v = que.pop()
  
  if done[v]!=0:
    root.append(v)
    break

  done[v] = 1
  root.append(v)
  que.append(li[v])

if len(root)==k+1:
  print(root[-1]+1)
else:
  fidx = root.index(root[-1])
  lidx = len(root)-1
  lp   = lidx-fidx

  cnt = (k-fidx)%lp

  if cnt==0:
    print(root[-1]+1)
  else:
    que.append(root[-1])
    for i in range(cnt):
      v = que.pop()
      que.append(li[v])
    print(que.pop()+1)