ZhenlongDai/ACPR huggingface dataset:
Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n,k=map(int,input().split())
a=list(map(int,input().split()))
dict={}
for i in range(n):
    dict[i]=a[i]-1
tmp=0
num2city={}
rec={}
rec[0]=1
num2city[0]=0
num2city[0]=0
for j in range(k):
    tmp=dict[tmp]
  
    if tmp not in num2city.keys():
        num2city[tmp]=j 
    else:
        res=k-j-1
        mae=num2city[tmp]
        term=j-mae+1
        r=res%term
      
       
        
        tmp=dict[r]

        break



print(tmp+1)

Failing test case input:
6 452899880549059817
2 4 1 5 3 0

Expected output:
4


Actual output:
1


Fixed submission (verdict: Accepted):
n,k=map(int,input().split())
a=list(map(int,input().split()))
dict={}
for i in range(n):
    dict[i]=a[i]-1 
tmp=0
num2city={}
city2num={}
city2num[0]=0
num2city[0]=0
alre=[-1]*n
alre[0]=1
for j in range(1,k+1):
    if tmp==dict[tmp]:
        break
    tmp=dict[tmp]
    if alre[tmp]==-1:
        alre[tmp]=1
        num2city[j]=tmp
        city2num[tmp]=j
    else:
        mae=city2num[tmp]
        term=j-mae 
        res=k-j
        r=res%term
  
        tmp=num2city[mae+r]
 
        break
   
    


print(tmp+1)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):

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

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


now=1
root=[1]
for i in range(N):
    now=A[now-1]

    if now not in root:
        root.append(now)
    else:
        index=root.index(now)
        num=len(root[index:])
        root=root[index:]
        break

an = (K-i) - (K-i)
print(root[an-1])


Failing test case input:
6 2190427306125562200
6 4 2 1 3 2

Expected output:
1


Actual output:
4


Fixed submission (verdict: Accepted):

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

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


now=1
flag=[-1]*(N)

for i in range(K):
    if flag[now-1]==-1:
        flag[now-1]=i
        now = A[now-1]
    else:
        loop = i - flag[now-1]
        zan=(K-(i-loop))%loop
        ans = flag.index(i-loop+zan)+1
        print(ans)
        exit()

print(now)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k=map(int,input().split())
a=list(map(int,input().split()))
i=0
s=set()
s.add(1)
order=[1]
while 1:
    if a[i] in s:
        order.append(a[i])
        break
    else:
        order.append(a[i])
        s.add(a[i])
    i=a[i]-1
for i in range(len(order)):
    if order[-1]==order[i]:
        idx=i
        break

cnt=len(order)-(idx+1)
k%=cnt
if len(order)>k:
    print(order[k])
else:
    k%=cnt
    print(order[k])



Failing test case input:
6 727202214173249351
4 5 1 5 2 3

Expected output:
2


Actual output:
4


Fixed submission (verdict: Accepted):
n,k=map(int,input().split())
a=list(map(int,input().split()))
i=0
s=set()
s.add(1)
order=[1]
while 1:
    if a[i] in s:
        order.append(a[i])
        break
    else:
        order.append(a[i])
        s.add(a[i])
    i=a[i]-1
for i in range(len(order)):
    if order[-1]==order[i]:
        idx=i
        break

cnt=len(order)-(idx+1)
if len(order)>k:
    print(order[k])
else:
    k-=len(order)
    order=order[idx+1:]
    
    
    k%=cnt
    
    print(order[k])
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, k = map(int, input().split())
A = list(map(int, input().split()))

V = [0 for i in range(n)]
P = [-100 for i in range(n)]
cp = 1
t = 0
while 1:
    
    V[cp - 1] += 1
    if V[cp - 1] < 2:
        P[cp - 1] = t
    else:
        break
    cp = A[cp - 1]
    t += 1

pret = P[cp - 1]
t = t - pret
print('pret = ', pret)
print('t = ', t)
print('P = ', P)

if k <= pret:
    pass
else:
    k = k - pret
    P = list(map(lambda x: x - pret, P))
print('P = ', P)

l = k % t
ans = 0
for i in range(n):
    if P[i] == l:
        ans = i + 1
        break
    else:
        pass
print(ans)

Failing test case input:
4 9
3 2 4 1

Expected output:
1


Actual output:
pret =  0
t =  3
P =  [0, -100, 1, 2]
P =  [0, -100, 1, 2]
1


Fixed submission (verdict: Accepted):
n, k = map(int, input().split())
A = list(map(int, input().split()))

C = [0 for i in range(n)]   
P = [-1 for i in range(n)]  
cp = 1  
t = 0   
while 1:
    
    C[cp - 1] += 1
    if C[cp - 1] < 2:
        P[cp - 1] = t
    else:
        break
    cp = A[cp - 1]
    t += 1

pret = P[cp - 1]    
t = t - pret




if k <= pret:
    l = k 
else:
    k = k - pret
    l = k % t
    P = list(map(lambda x: x - pret, P))


ans = 0
for i in range(n):
    if P[i] == l:
        ans = i + 1
        break
    else:
        pass
print(ans)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, k = map(int, input().split())
a = list(map(int, input().split()))

pos = -1
seen = [False] * n
last = [0] * n
hist = []
cycle = []
i = 0
cnt = 0
while(seen[i] == False):
    seen[i] = True
    last[i] = cnt
    cnt += 1
    hist.insert(0, i)
    ne = a[i]-1
    i = ne
    if (seen[i] == True):
        pos = i

preCircle = len(hist.copy())

cycle = hist[:len(hist) - last[pos]]

preCircle -= cnt

if (k < preCircle):
    print(hist[-k-1] + 1)
else:
    k -= preCircle
    ans = k % len(cycle)
    print(cycle[ans] + 1)



Failing test case input:
6 601702492831334422
6 5 2 5 3 2

Expected output:
3


Actual output:
5


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

pos = -1
seen = [False] * n
last = [-1] * n
hist = []
i = 0
cnt = 0

while(seen[i] == False):
    seen[i] = True
    last[i] = cnt
    cnt += 1
    hist.append(i)
    ne = a[i]-1
    i = ne
    if (seen[i] == True):
        pos = i

cycle = hist[last[pos]:]
preCircle = last[pos]

if (k <= preCircle):
    print(hist[k] + 1)
else:
    k -= preCircle
    ans = k % len(cycle)
    print(cycle[ans] + 1)
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
def main():
    n, k = map(int, input().split())
    As = list(map(int, input().split()))
    i = 0
    visit = [0]*n
    move = []
    roop = []
    while visit[i] != 2:
        if visit[i] == 0:
            move.append(i)
        else:
            roop.append(i)
        visit[i] += 1
        i = As[i] - 1
    ans = As[(k % len(roop))]
    print(ans)


if __name__ == "__main__":
    main()


Failing test case input:
6 727202214173249351
4 3 2 3 3 3

Expected output:
2


Actual output:
3


Fixed submission (verdict: Accepted):
def main():
    n, k = map(int, input().split())
    As = list(map(int, input().split()))
    i = 0
    visit = [0]*n
    move = []
    roop = []
    while visit[i] != 2:
        if visit[i] == 0:
            move.append(i)
        else:
            roop.append(i)
        visit[i] += 1
        i = As[i] - 1

    if len(move) > k:
        ans = move[k] + 1
    else:
        ans = roop[(k-(len(move)-len(roop))) % len(roop)]+1
    print(ans)


if __name__ == "__main__":
    main()

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = map(int, input().split())
A = list(map(int, input().split()))

c = [0]*N
cc = [0]
c[0] = 1
i = 0
while True:
  c[A[i]-1] += 1  
  cc.append(A[i]-1)
  if c[A[i]-1] > 1:
    memo = A[i]-1
    break
  i = A[i] - 1  
    
used = [i for i in range(N) if c[i] > 0]

pos = [i for i in range(len(cc)) if cc[i] == memo]

res = pos[1] - pos[0]

if K < pos[0]+1:
  for i in range(K):
    c[A[i]-1] += 1  
    i = A[i] - 1
  ans = i+1
else:
  i = (K - (pos[0]+1)) % res
  ans = cc[i]
  
print(ans)

Failing test case input:
6 727202214173249351
6 5 1 5 1 2

Expected output:
5


Actual output:
1


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
A = list(map(int, input().split()))

c = [0]*N
cc = [0]
c[0] = 1
i = 0
while True:
  c[A[i]-1] += 1  
  cc.append(A[i]-1)
  if c[A[i]-1] > 1:
    memo = A[i]-1
    break
  i = A[i] - 1  
    
used = [i for i in range(N) if c[i] > 0]

pos = [i for i in range(len(cc)) if cc[i] == memo]

res = pos[1] - pos[0]
loop = [i for i in cc[pos[0]:pos[1]]]

if K < N:
  ans = cc[K]+1
else:
  i = (K - (pos[0])) % res
  ans = loop[i]+1
  
print(ans)
Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n, k = map(int, input().split())
a = list(map(int, input().split()))
now = 1
cnt = 0
loop = 0
visited = [False] * n
visited[now - 1] = cnt
while True:
    now = a[now - 1]
    cnt += 1
    if visited[now - 1]:
        loop = (cnt - visited[now - 1])
        print(a[(visited[now - 1]) + ((k - (visited[now - 1])) % loop)])
        exit()
    visited[now - 1] = cnt


Failing test case input:
6 727202214173249351
6 5 1 5 1 3

Expected output:
3


Actual output:
1


Fixed submission (verdict: Accepted):
n, k = map(int, input().split())
a = list(map(int, input().split()))
now = 1
cnt = 0
loop = 0
cnts = [-1] * n
cnts[now - 1] = cnt
visited = [False] * n
visited[now - 1] = True
li = [now]

while True:
    cnt += 1
    now = a[now - 1]
    li.append(now)

    if cnt == k:
        print(now)
        exit()
    if visited[now - 1]:
        loop = (cnt - cnts[now - 1])
        pre = cnts[now - 1]
        print(li[pre + (k - pre) % loop])
        exit()

    cnts[now - 1] = cnt
    visited[now - 1] = True

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
from collections import deque

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

tere = {}
for i in range(n):
    tere[i+1] = a[i]

mati = deque([1])

tugi = 1
for i in range(1, k+1):
    if tere[tugi] not in mati:
        mati.append(tere[tugi])
    else:
        mati.append(tere[tugi])
        break
    tugi = tere[tugi]
else:
    print(tugi)
    exit()
print("No")

Failing test case input:
6 357997123416626975
4 4 1 1 3 2

Expected output:
4


Actual output:
No


Fixed submission (verdict: Accepted):
from collections import deque

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

tere = {}
for i in range(n):
    tere[i+1] = a[i]

mati = deque([1])
mark = set([1])
tugi = 1
for i in range(1, k+1):
    if tere[tugi] not in mark:
        mati.append(tere[tugi])
        mark.add(tere[tugi])
    else:
        mati.append(tere[tugi])
        mark.add(tere[tugi])
        break
    tugi = tere[tugi]
else:
    print(tugi)
    exit()

mati = list(mati)
cnta = len(mati)-1
i = mati.index(mati[-1]) + 1
cntroop = cnta - i + 1
roop = mati[i:]

print(roop[(k-cnta)%cntroop-1])

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

visited = []
order = {}
x = 1
T = -1
while T < 0:
  x = a[x-1] 
  if x not in visited:
    visited.append(x)
    order[x] = len(visited)-1
  else:
    T = len(visited) - order[x]
    s = order[x]
if k < s:
  print(visited[k])
else:
  mod = (k-s) % T
  print(visited[s+mod])

Failing test case input:
6 727202214173249351
6 4 1 5 3 2

Expected output:
3


Actual output:
1


Fixed submission (verdict: Accepted):
n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

visited = [-1 for i in range(n+1)] 
points = []
p = 1
while visited[p] == -1:
  visited[p] = len(points)
  points.append(p)
  p = a[p-1] 

T = len(points) - visited[p]
s = visited[p]

if k < s:
  print(points[k])
else:
  mod = (k - s) % T
  print(points[s+mod])
Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):

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


flag=[0]*N
patt=[]
aaaa=0
i=0
while True:
    patt.append(i)
    if flag[i]==1:
        
        break
    flag[i]=1
    i=A[i]-1


srt = patt.index(patt[-1])

end = len(patt)-2
ptrn = patt[srt:end+1]

if K<len(ptrn):
    print(patt[K-1])
else:
    B = (K-srt) % len(ptrn)
    

    print(ptrn[B]+1)
    

    
    

















Failing test case input:
4 0
3 2 4 1

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
[N,K] = list(map(int,input().split()))
A = list(map(int,input().split()))
 

flag=[0]*N
patt=[]
aaaa=0
i=0
while True:
    patt.append(i)
    if flag[i]==1:
        
        break
    flag[i]=1
    i=A[i]-1
 
 
srt = patt.index(patt[-1])

end = len(patt)-2
ptrn = patt[srt:end+1]
 
if K<srt:
    print(patt[K]+1)
else:
    B = (K-srt) % len(ptrn)
    
 
    print(ptrn[B]+1)

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
n, k = [int(i) for i in sys.stdin.readline().split()]
a_s = [int(i)-1 for i in sys.stdin.readline().split()]
cnt = 0
ind = 0
ls = [ind]
last_pos = {0:0}
_set = {ind}
while cnt < k:
    ind = a_s[ind]
    if ind in _set:
        break
    _set.add(ind)
    ls.append(ind)
    cnt += 1
    last_pos[ind] = cnt
if cnt == k:
    print(ind+1)
else:
    start = last_pos[ind]
    end = cnt+1
    print(ls[start + (k - cnt) % (end - start)-1]+1)

Failing test case input:
6 116449238586591883
6 2 1 4 3 2

Expected output:
2


Actual output:
6


Fixed submission (verdict: Accepted):
import sys
n, k = [int(i) for i in sys.stdin.readline().split()]
a_s = [int(i)-1 for i in sys.stdin.readline().split()]
cnt = 0
ind = 0
ls = [ind]
last_pos = {0:0}
_set = {ind}
while cnt < k:
    ind = a_s[ind]
    if ind in _set:
        break
    _set.add(ind)
    ls.append(ind)
    cnt += 1
if cnt == k:
    print(ind+1)
else:
    start = ls.index(ind)
    end = cnt+1
    print(ls[start:][(k - cnt) % (end - start)-1]+1)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [i-1 for i in a]
visited = [-1]*n
route = []
now = 0
hop = 0
while visited[now] == -1:
    visited[now]=hop
    route.append(now)
    now = a[now]
    hop+=1
loop_st = 0
for r in route:
    if r==now: break
    loop_st+=1
loop = route[loop_st:]
if k-1 < len(route):
    print(route[k-1]+1)
else:
    print(loop[(k-loop_st)%len(loop)]+1)

Failing test case input:
4 0
3 2 1 1

Expected output:
1


Actual output:
3


Fixed submission (verdict: Accepted):
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [i-1 for i in a]
visited = [-1]*n
route = []
now = 0
hop = 0
while visited[now] == -1:
    visited[now]=hop
    route.append(now)
    now = a[now]
    hop+=1
loop_st = 0
for r in route:
    if r==now: break
    loop_st+=1
loop = route[loop_st:]
if k < len(route):
    print(route[k]+1)
else:
    print(loop[(k-loop_st)%len(loop)]+1)
Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [1]+a
v = [-1 for _ in range(n+1)]
v[1] = 0
x = a[1]
c = 1

while k > 0 and v[x] < 0:
    y = a[x]
    v[y] = v[x]+1
    x = y
    k -= 1
    c += 1

if k==0:
    print(x)
else:
    z = c - v[x]

    k = k%z
    while k > 0:
        k -= 1
        x = a[x]
    print(x)

Failing test case input:
6 165759462200935729
4 5 2 5 3 2

Expected output:
2


Actual output:
5


Fixed submission (verdict: Accepted):
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [i-1 for i in a]
v = [-1 for _ in range(n)]
p = 0
x = 0
c = 0
while k > 0 and v[x] < 0:
    v[x] = p+1
    p = v[x]
    x = a[x]
    c += 1
    k -= 1

z = c-v[x]+1
k = k%z

while k > 0:
    k -= 1
    x = a[x]
print(x+1)

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
259
N,K=map(int,input().split())
A=[0]+list(map(int,input().split()))
m=1
towns=[]
while 1:
    towns.append(m)
    m=A[m]
    if m in towns:
        l=len(towns)-towns.index(m)
        break
print(towns[K%l])

Failing test case input:
6 727202214173249351
6 5 4 5 3 2

Expected output:
4


Actual output:
2


Fixed submission (verdict: Accepted):
259
N,K=map(int,input().split())
A=[0]+list(map(int,input().split()))
m=1
towns=[1]
flg=[0]*(N+1)
flg[1]=1
while 1:
    m=A[m]
    towns.append(m)
    if flg[m]==1:
        l=len(towns)-towns.index(m)-1
        break
    flg[m]=1
pl=towns.index(m)
if K<=pl:
    print(towns[K])
else:
    print(towns[(K-pl)%l+pl])

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N,K=map(int,input().split())
A=list(map(int,input().split()))
A=[i-1 for i in A]
c=A[A[0]]
 
s=set()
s.add(0)
s.add(A[0])
I=[]
I.append(0)
I.append(A[0])

while True:
  if c in s:
    break
  s.add(c)
  I.append(c)
  c=A[c]

a=I.index(c)
K-=a

ans=A[0]
for i in range(a-1):
  ans=A[ans]

mod=len(s)-a
K%=mod

if K==0:
  print(ans+1)
  exit()




for i in range(K-1):
  ans=A[ans]
print(ans+1)

Failing test case input:
4 9
3 2 4 1

Expected output:
1


Actual output:
3


Fixed submission (verdict: Accepted):
N,K=map(int,input().split())
A=list(map(int,input().split()))
A=[i-1 for i in A]
c=A[A[0]]
 
s=set()
s.add(0)
s.add(A[0])
I=[]
I.append(0)
I.append(A[0])

while True:
  if c in s:
    break
  s.add(c)
  I.append(c)
  c=A[c]

a=I.index(c)
if K>=a:
  K-=a
else:
  ans=0
  for i in range(K):
    ans=A[ans]
  print(ans+1)
  exit()

ans=0
for i in range(a):
  ans=A[ans]

mod=len(s)-a
K%=mod

if K==0:
  print(ans+1)
  exit()




for i in range(K):
  ans=A[ans]
print(ans+1)
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k=map(int, input().split())
a=[int(x)-1 for x in list(input().split())]
way=[0]
noappear=[i for i in range(n)]
while True:
        way.append(a[way[-1]])
        
        if bool(way[-1] in noappear)==False:
                break

        noappear.remove(way[-1])

last=way[-1]
st=way.index(last)
en=len(way)-1
step=en-st
rest=k%step
if k>=en:
        ans=way[rest]+1

else:
        ans=way[k]+1
print(ans)

        

        



Failing test case input:
6 601702492831334422
6 5 2 5 3 2

Expected output:
3


Actual output:
6


Fixed submission (verdict: Accepted):
n,k=map(int, input().split())
a=[int(x)-1 for x in list(input().split())]
way=[0]
seen=[0 for i in range(n)]
while True:
        now=way[-1]
        way.append(a[now])
        
        if seen[a[now]]==1:
                break
        seen[a[now]]=1


last=way[-1]
st=way.index(last)
en=len(way)-1
step=en-st
rest=(k-st+1)%step
if k>=st:
        if rest==0:
                ans=way[st-1+step]+1
        else:
                ans=way[st-1+rest]+1

else:
        ans=way[k]+1
print(ans)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
r = 0
if K <= N:
    current = 0
    for i in range(K):
        current = A[current-1]
    print(current)
else:
    isReturn = [0 for i in range(N)]
    step = 1
    current = 1
    isReturn[current-1] = step
    for i in range(N):
        current = A[current-1]
        step += 1
        if isReturn[current-1] == 0:
            isReturn[current-1] = step
        else:
            r = step - isReturn[current-1]
            break
    
    t = ((K-step) % r) + 1
    for i in range(t):
        current = A[current-1]
        
    print(current)
    
    
                
            
    
    
                
            


Failing test case input:
4 0
3 2 1 1

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
r = 0
if K <= N:
    current = 1
    for i in range(K):
        current = A[current-1]
    print(current)
else:
    isReturn = [0 for i in range(N)]
    step = 1
    current = 1
    isReturn[current-1] = step
    for i in range(N):
        current = A[current-1]
        step += 1
        if isReturn[current-1] == 0:
            isReturn[current-1] = step
        else:
            r = step - isReturn[current-1]
            break
    
    t = ((K-step) % r) + 1
    for i in range(t):
        current = A[current-1]
        
    print(current)
Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N, K = map(int, input().split())
A = list(map(int, input().split()))
li = [False] * N
f = True
now = 0
l = []
l2 = [0] * N
i = 0
while f:
    l.append(now)
    li[now] = True
    l2[now] = i
    if li[A[now]-1]:
        f = False
        num = len(l) - l2[A[now]-1]
    i += 1
    now = A[now]-1
K %= num
print(l[K]+1)

Failing test case input:
6 727202214173249351
6 3 3 4 3 5

Expected output:
3


Actual output:
1


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N):
    A[i] -= 1
li = [False] * N
f = True
now = 0
l = []
l2 = [0] * N
i = 0
while f:
    l.append(now)
    li[now] = True
    l2[now] = i
    if li[A[now]]:
        f = False
        num = len(l) - l2[A[now]]
        n = l2[A[now]]
    i += 1
    now = A[now]
k = (K-n) % num
print(l[min(K, n+k)]+1)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
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])

Failing test case input:
6 727202214173249351
4 5 2 3 3 2

Expected output:
3


Actual output:
5


Fixed submission (verdict: Accepted):
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)

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))

x = 0
l = [0]
s = set([0])
flag = False
for i in range(N):
    x = A[x]-1
    if x in s:
        break
    l.append(x)

for i in range(len(l)):
    if l[i]==x:
        break

y = len(l)
z = i

if K<len(l):
    print(l[K]+1)
else:
    
    
    
    
    
    print(l[z-1 + (K-z)%(y-z)]+1)

Failing test case input:
6 2190427306125562200
6 4 1 1 3 2

Expected output:
1


Actual output:
4


Fixed submission (verdict: Accepted):
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))

x = 0
l = [0]
s = set([0])
flag = False
for i in range(N):
    x = A[x]-1
    if x in s:
        break
    s.add(x)
    l.append(x)

for i in range(len(l)):
    if l[i]==x:
        break

y = len(l)
z = i

if K<len(l):
    print(l[K]+1)
else:
    
    
    
    
    
    print(l[z + (K-z)%(y-z)]+1)
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, k = map(int, input().split())
A = list(map(int, input().split()))
route = [1]
dest = A[0]
while dest not in route:
    route.append(dest)
    dest = A[dest - 1]
ind = route.index(dest)
cycle = route[ind:]
print(ind, cycle)
if k >= ind:
    ans = (k - ind) % len(cycle)
    print(cycle[ans])
else:
    print(route[k])

Failing test case input:
6 2190427306125562200
6 6 3 1 3 2

Expected output:
2


Actual output:
1 [6, 2]
2


Fixed submission (verdict: Accepted):
n, k = map(int, input().split())
A = list(map(int, input().split()))
route = [1]
dest = A[0]
check = {i: False for i in range(n)}
while not check[dest - 1]:
    check[dest - 1] = True
    route.append(dest)
    dest = A[dest - 1]
ind = route.index(dest)
cycle = route[ind:]
if k >= ind:
    ans = (k - ind) % len(cycle)
    print(cycle[ans])
else:
    print(route[k])

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a = [a[i] - 1 for i in range(n)]
    next_i = 0
    seen = [-1] * n
    count = 0
    while True:
        if seen[next_i] != -1:
            start = next_i
            l = count - seen[next_i]
            break
        seen[next_i] = count
        count += 1
        next_i = a[next_i]
    index = k
    if k > start:
        index = ((k - start) % l) + start
    for i in range(n):
        if seen[i] == index:
            print(i + 1)
            exit()


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()


Failing test case input:
6 64871420930086488
6 2 1 5 3 2

Expected output:
2


Actual output:
6


Fixed submission (verdict: Accepted):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a = [a[i] - 1 for i in range(n)]
    next_i = 0
    seen = [-1] * n
    count = 0
    while True:
        if seen[next_i] != -1:
            start = seen[next_i]
            l = count - seen[next_i]
            break
        seen[next_i] = count
        count += 1
        next_i = a[next_i]
    if k > start:
        k = ((k - start) % l) + start
    for i in range(n):
        if seen[i] == k:
            print(i + 1)
            break


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):

from sys import stdin
N,K = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
A = [0] + A

visited = [False]*(N+1)
visited[1] = True
step = [0]*(N+1)
step[0] = 1
step[1] = A[1]
count = [0]*(N+1)
count[1] = 1

now = A[1]
cnt = 1
roop = 0

for _  in range(N):
    now = A[now]
    cnt += 1
    
    if visited[now]:
        roop = cnt - count[now] + 1
        K -= (count[now]-1)
        break
    count[now] = cnt
    step[cnt] = now
    visited[now] = True
    
ind = K % roop
print(step[ind])

Failing test case input:
6 727202214173249351
3 5 3 4 3 2

Expected output:
3


Actual output:
1


Fixed submission (verdict: Accepted):

from sys import stdin
N,K = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
A = [0] + A

visited = [False]*(N+1)
visited[1] = True
step = [0]*(N+1)
step[0] = 1
step[1] = A[1]
count = [0]*(N+1)
count[1] = 0
count[A[1]] = 1

now = A[1]
cnt = 1
roop = 0

for _  in range(N):
    now = A[now]
    cnt += 1

    if visited[now]:
        roop = cnt - count[now]
        break
    count[now] = cnt
    step[cnt] = now
    visited[now] = True

if K < count[now]:
    print(step[K])
else:
    K -= count[now]
    ind = K % roop
    print(step[ind+count[now]])

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n,k=map(int,input().split())
al=[0]+list(map(int,input().split()))


visited=[-1]*(1+n)
cl=[0]*(1+n)
visited[1]=0
c=0
now=1
t=0

start=0

for i in range(1,n+1):
    if visited[al[now]]==-1: 
        visited[al[now]]=0
        c+=1
        cl[al[now]]=c
        now=al[now]
    else:  
        start=cl[al[now]]
        t=i-start
        break
if start==0:
    print(now)
else:
    cl2=cl[1:]
    print(cl2.index(start+(k-start)%t)+1)

Failing test case input:
6 237500909755776657
2 4 1 5 3 2

Expected output:
4


Actual output:
3


Fixed submission (verdict: Accepted):
n,k=map(int,input().split())
al=[0]+list(map(int,input().split()))

visited=[-1]*(n+1)
visited[1]=0

lstart=0
t=0

now=1
while lstart==0:
    if visited[al[now]]<0:
        visited[al[now]]=visited[now]+1
        now=al[now]
        
    else:
        t=visited[now]-visited[al[now]]+1
        lstart=al[now]

if visited[lstart]>=k:
    print(visited.index(k))
            
else:
    print(visited.index(visited[lstart]+(k-visited[lstart])%t))
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, k = map(int, input().split())
lst = [int(i) for i in input().split()]

tele_lst = []
town = 1
while True:
  if town not in tele_lst:
    tele_lst.append(town)
  else:
    break
  town = lst[town - 1]
ind = tele_lst.index(town)
length = len(tele_lst)
circle = length - ind


if k < ind:
  town = lst[k]
  print(town)
else:
  k -= ind
  k %= circle
  town = lst[ind + k]
  print(town)

Failing test case input:
6 165759462200935729
4 5 2 5 3 2

Expected output:
2


Actual output:
3


Fixed submission (verdict: Accepted):
n, k = map(int, input().split())
lst = [int(i) for i in input().split()]

tele_lst = []
town_set = set()
town = 1
while True:
  if town not in town_set:
    tele_lst.append(town)
    town_set.add(town)
  else:
    break
  town = lst[town - 1]
ind = tele_lst.index(town)
length = len(town_set)
circle = length - ind


if k < ind:
  town = tele_lst[k]
  print(town)
else:
  k -= ind
  k %= circle
  
  town = tele_lst[ind + k]
  print(town)

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N, K = map(int, input().split())
A = list(map(int, input().split()))
li = [False] * N
f = True
now = 0
l = []
l2 = [0] * N
i = 0
while f:
    print(now)
    l.append(now)
    li[now] = True
    l2[now] = i
    if li[A[now]-1]:
        f = False
        num = len(l) - l2[A[now]-1]
    i += 1
    now = A[now]-1
K %= num
print(l[K]+1)

Failing test case input:
4 0
3 0 4 1

Expected output:
1


Actual output:
0
2
3
1


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N):
    A[i] -= 1
li = [False] * N
f = True
now = 0
l = []
l2 = [0] * N
i = 0
while f:
    l.append(now)
    li[now] = True
    l2[now] = i
    if li[A[now]]:
        f = False
        num = len(l) - l2[A[now]]
        n = l2[A[now]]
    i += 1
    now = A[now]
k = (K-n) % num
print(l[min(K, n+k)]+1)

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n, k = map(int, input().split())
a = list(map(int, input().split()))
 
l = [1]
for i in range(n):
    l.append(a[l[i] - 1])
 
p = l.index(l[n])
 
if k <= n:
    print(l[k])
else:
    x = (k - n + 1) % (n - p)
    if x == 0:
        print(l[n - 1])

Failing test case input:
6 916742600949256759
6 5 2 4 2 2

Expected output:
5


Actual output:

Fixed submission (verdict: Accepted):
n, k = map(int, input().split())
a = list(map(int, input().split()))
 
l = [1]
for i in range(n):
    l.append(a[l[i] - 1])
 
p = l.index(l[n])
 
if k <= n:
    print(l[k])
else:
    x = (k - n + 1) % (n - p)
    if x == 0:
        print(l[n - 1])
    else:
        print(l[x + p - 1])
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):

def input2():
	return map(int,input().split())


def input_array():
	return list(map(int,input().split()))
  
n,k=input2()
A=input_array()


tmps=[1] 
loops=[] 
st=0 
count=0 

for i in range(2*(10**5)+1):
	if A[count] not in tmps:
		tmps.append(A[count])
	elif (A[count] in tmps) and (A[count] not in loops):
		loops.append(A[count])
		st=i
	elif A[count] in loops:
		break
	count=A[count]-1



if k<=st:
	print(tmps[k-1])
else:
	ans=(k-(st-1))%len(loops)
	print(loops[ans])

Failing test case input:
6 916742600949256759
6 5 2 4 2 2

Expected output:
5


Actual output:
2


Fixed submission (verdict: Accepted):

def input2():
	return map(int,input().split())


def input_array():
	return list(map(int,input().split()))
  
n,k=input2()
A=input_array()


judge=[True]*n
tmps=[1] 
count=0 

for i in range(2*(10**5)+1):
	if judge[count]==True:
		judge[count]=False
		count=A[count]-1
		tmps.append(count+1)
	else:
		break

loop_st=tmps.index(tmps[-1])
loops=tmps[loop_st:-1]


if k<loop_st:
	print(tmps[k])
else:
	k=k-loop_st
	k%=len(loops)
	print(loops[k])

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
from collections import defaultdict

N,K = map(int,input().split())
A = list(map(int,input().split()))
route_history = defaultdict(lambda : -1)
position = 1
b_pos = -1
for i in range(1,N+1):
    if(route_history[position] != -1):
        b_pos = route_history[position]
        break
    route_history[position] = i-1
    position = A[position-1]

new_route_history = defaultdict(lambda : -1)
count = 0
for key,value in dict(route_history).items():
    if(key >= b_pos):
        new_route_history[count] = key
        count+=1

if(b_pos != -1):
    print(new_route_history[(K-b_pos)%len(new_route_history)])
else:
    print(position)

Failing test case input:
6 279024949205837559
4 5 2 5 3 3

Expected output:
3


Actual output:
5


Fixed submission (verdict: Accepted):
from collections import defaultdict

N,K = map(int,input().split())
A = list(map(int,input().split()))
position = 1
past_path = defaultdict(lambda : -1)
loop_pos = -1

def move(position):
	return A[position-1]

for i in range(1,K+1):
	if(past_path[position] != -1):
		loop_pos = past_path[position]
		break
	past_path[position] = i-1
	position = move(position)

new_past_path = defaultdict(lambda : -1)
for k,v in past_path.items():
	if v >= loop_pos:
		new_past_path[v-loop_pos] = k

if loop_pos != -1:
	print(new_past_path[(K-loop_pos)%len(new_past_path)])
else:
	print(position)
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]

town = []
dic = {}

i = 0
j = 0

while i not in dic and j < K:
    print('i', i)
    dic[i] = j
    town.append(i)
    i = X[i] - 1
    j += 1
    
if j == K:
    output = i + 1
    
else : 
    num = dic[i]
    loop = j - num
    output = town[((K-num) % loop) +num] + 1
    
print(output)

Failing test case input:
6 727202214173249351
6 5 1 2 4 2

Expected output:
2


Actual output:
i 0
i 5
i 1
i 4
i 3
2


Fixed submission (verdict: Accepted):
N, K = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]

town = []
dic = {}

i = 0
j = 0

while i not in dic and j < K:
    dic[i] = j
    town.append(i)
    i = X[i] - 1
    j += 1
    
if j == K:
    output = i + 1
    
else : 
    num = dic[i]
    loop = j - num
    output = town[((K-num) % loop) +num] + 1
    
print(output)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k=map(int,input().split())
a=list(map(int,input().split()))
for i in range(n):
  a[i]-=1
kai=[-1 for i in range(n)]
nx=0
now=0
kai[0]=0

if k<n:
  for i in range(k):
    nx=a[now]
    now=nx
  ans=nx
else:
  for i in range(n):
    nx=a[now]
    if kai[nx]==-1:
      kai[nx]=i+1
      now=nx
    else:
      T=i+1-kai[nx]
      break
  id=kai[nx]+(k-kai[nx])%T
  ans=a[id]+1
print(ans)

Failing test case input:
6 916742600949256759
6 5 2 4 2 2

Expected output:
5


Actual output:
4


Fixed submission (verdict: Accepted):
n,k=map(int,input().split())
a=list(map(int,input().split()))
for i in range(n):
  a[i]-=1
kai=[-1 for i in range(n)]
nx=0
now=0
kai[0]=0

if k<=n:
  for i in range(k):
    nx=a[now]
    now=nx
    
  ans=nx+1
else:
  for i in range(n):
    nx=a[now]
    if kai[nx]==-1:
      kai[nx]=i+1
      now=nx
    else:
      T=i+1-kai[nx]
      break
  times=kai[nx]+(k-kai[nx])%T
  now=0
  nx=0
  for i in range(times):
    nx=a[now]
    now=nx
    
  ans=nx+1
print(ans)

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N , K = map(int,input().split())
B = {}
C = {}
D = {0}
A = list(map(int,input().split()))
place = 0
num = 0
C[0] = 0
while True:
    place = int(A[place]) - 1
    num += 1   
    if place in D:
        Y = int(num) - int(C[place])
        break
    elif num == K:
        break
    else:
        B[num] = place
        C[place] = num
        D.add(place)
try:
    X = (K - num) % Y
    print(B[C[place] + X] + 1)
except:
    print(A[place]+1)


Failing test case input:
6 279024949205837559
4 5 2 5 1 3

Expected output:
1


Actual output:
5


Fixed submission (verdict: Accepted):
N , K = map(int,input().split())
B = {}
C = {}
D = {0}
A = list(map(int,input().split()))
place = 0
num = 0
C[0] = 0
while True:
    place = int(A[place]) - 1
    num += 1   
    if place in D:
        Y = int(num) - int(C[place])
        break
    elif num == K:
        break
    else:
        B[num] = place
        C[place] = num
        D.add(place)
try:
    X = (K - num) % Y
    print(B[C[place] + X] + 1)
except:
    print(place+1)
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k = map(int,input().split())

A = list(map(int,input().split()))
t = 0
c = 0
ans = [0] * n
L = []
s = 0
for _ in range(k):
    if ans[t] == 1:
        n = len(L) - L.index(t)
        temp = t + k%n
        temp %= n
        t = L[L.index(t):][temp]
        break
    else:
        ans[t] = 1
        L.append(t)
        t = A[t]-1
        c += 1
print(t+1)

Failing test case input:
6 727202214173249351
4 5 2 3 3 3

Expected output:
3


Actual output:
2


Fixed submission (verdict: Accepted):
n,k = map(int,input().split())

A = list(map(int,input().split()))
t = 0
c = 0
ans = [0] * n
L = []
s = 0
for _ in range(k):
    if ans[t] == 1:
        n = len(L) - L.index(t)
        temp = (k-c)%n
        t = L[L.index(t):][temp]
        break
    else:
        ans[t] = 1
        L.append(t)
        t = A[t]-1
        c += 1
print(t+1)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k = map(int,input().split())
k += 1
l1 = list(map(int,input().split()))
l = list(i-1 for i in l1)
bit = list(0 for i in range(n))
bit[0] =1
mi = [0]
m = l[0]
er = 0
while er ==0:
  if bit[m]==1:
    a = mi.index(m)
    r = mi[a:]
    er  =1
  else:
    bit[m] = 1
    mi.append(m)
    m = l[m]
 
be = mi[:a]+r

if k<= len(be):
  print(be[k])
else:
  w = k-len(be)
  u = w%len(r)-1
  print(r[u]+1)

Failing test case input:
4 0
3 2 4 1

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
n,k = map(int,input().split())
l1 = list(map(int,input().split()))
l = list(i-1 for i in l1)
bit = list(0 for i in range(n))
bit[0] =1
mi = [0]
m = l[0]
er = 0
while er ==0:
  if bit[m]==1:
    a = mi.index(m)
    r = mi[a:]
    er  =1
  else:
    bit[m] = 1
    mi.append(m)
    m = l[m]
 
be = mi[:a]+r

if k< len(be):
  print(be[k]+1)
else:
  w = k-len(be)+1
  u = (w%len(r))-1
  print(r[u]+1)
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):

n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
count_arr = [0] * n
count = 0

now = 0
loop_length = -1
while True:
    if count == k:
        print(a[now] + 1)
        exit()
    if count_arr[now] != 0:
        loop_length = count - count_arr[now] + 1
        break
    count += 1
    count_arr[now] = count
    now = a[now]


rest = (k - count) % loop_length


if rest == 0:
    print(now + 1)
    exit()

while rest > 0:
    print(f"now = {now}(next = {a[now]})")
    rest -= 1
    if rest == 0:
        break
    now = a[now]
print(a[now] + 1)


Failing test case input:
6 727202214173249351
6 5 0 5 1 1

Expected output:
6


Actual output:
now = 0(next = 5)
6


Fixed submission (verdict: Accepted):


n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
count_arr = [0] * n
count = 0

now = 0
loop_length = -1
while True:
    if count == k:
        print(now + 1)
        exit()
    if count_arr[now] != 0:
        loop_length = count - count_arr[now] + 1
        break
    count += 1
    count_arr[now] = count
    now = a[now]


rest = (k - count) % loop_length


if rest == 0:
    print(now + 1)
    exit()

while rest > 0:
    rest -= 1
    if rest == 0:
        break
    now = a[now]
print(a[now] + 1)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k = map(int,input().split())
l1 = list(map(int,input().split()))
l = list(i-1 for i in l1)
bit = list(0 for i in range(n))
bit[0] =1
mi = [0]
m = l[0]
er = 0
while er ==0:
  if bit[m]==1:
    a = mi.index(m)
    r = mi[a:]
    er  =1
  else:
    bit[m] = 1
    mi.append(m)
    m = l[m]
 
be = mi[:a]+r

if k< len(be):
  print(be[k])
else:
  w = k-len(be)+1
  u = (w%len(r))-1
  print(r[u]+1)

Failing test case input:
4 0
3 1 4 1

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
n,k = map(int,input().split())
l1 = list(map(int,input().split()))
l = list(i-1 for i in l1)
bit = list(0 for i in range(n))
bit[0] =1
mi = [0]
m = l[0]
er = 0
while er ==0:
  if bit[m]==1:
    a = mi.index(m)
    r = mi[a:]
    er  =1
  else:
    bit[m] = 1
    mi.append(m)
    m = l[m]
 
be = mi[:a]+r

if k< len(be):
  print(be[k]+1)
else:
  w = k-len(be)+1
  u = (w%len(r))-1
  print(r[u]+1)
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k=list(map(int,input().split()))
As=list(map(int,input().split()))
root=[1]
count=[0]*n
count[0]+=1

for i in range(k):
    if count[root[-1]-1]!=0:
        break
    else:
        root.append(As[root[-1]-1])
        count[As[root[-1]-1]-1]+=1
print(root[k%len(root)])

Failing test case input:
6 355819207751533051
6 5 1 5 2 1

Expected output:
6


Actual output:
1


Fixed submission (verdict: Accepted):
n,k=list(map(int,input().split()))
As=list(map(int,input().split()))
root=[1]
count=[0]*n
count[0]+=1

cnt=0
for _ in range(k):
    dis=As[root[-1]-1]-1
    if count[dis]!=0:
        cnt+=1
        break
    else:
        root.append(dis+1)
        count[dis]+=1
if cnt==1:
    first=root.index(As[root[-1]-1])
    print(root[(k-first)%(len(root)-first)+first])
else:
    print(root[-1])

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k=map(int,input().split())
a=[q-1 for q in map(int,input().split())]

if k<=n:
    
    now=0
    for i in range(1,k+1):
        now=a[now]
    print(now+1)
    exit()

now=0
visited=[-100]*n
visited[now]=0
for i in range(1,2*10**5+1):
    now=a[now]
    if visited[now]==-100:
        visited[now]=i
    else:
        m=i-visited[now]
        lft=visited[now]
        break
k=k%m
if k<=lft:
    k+=m

for i in range(1,k+1):
    now=a[now]
print(now+1)

Failing test case input:
6 1007784494318310970
6 5 2 5 3 2

Expected output:
3


Actual output:
5


Fixed submission (verdict: Accepted):
n,k=map(int,input().split())
a=[0]+list(map(int,input().split()))

MAX_N=2*10**5
b=[1]
visited=[-1]*(n+10)
for i in range(1,2*MAX_N+1):
    nxt=a[b[-1]]
    b.append(nxt)
        
if k<=n:
    print(b[k])
    exit()

for i,bi in enumerate(b):
    if visited[bi]==-1:
        visited[bi]=i
    else:
        m=i-visited[bi]
        l=visited[bi]
        break
k=k%m

while k<n:
    k+=m
print(b[k])

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = map(int, input().split())
A = input().split()
A = [int(a) for a in A]
S = [1]
a = 1
number = 0
numbers=[-1 for _ in range(N)]
for _ in range(N + 1):
    b = A[a - 1]
    if numbers[b-1] != -1:
      c=numbers[b-1]
      break
    numbers[b-1]=number
    number += 1
    S.append(b)
    a = b
T = S[c:number]
if K <= number:
  print(S[K - 1])
else:
  z = K - c
  y = z % (number - c)
  print(T[y])



Failing test case input:
6 727202214173249351
6 3 3 4 3 3

Expected output:
3


Actual output:
6


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
A = input().split()
A = [int(a) for a in A]
S = [1]
a = 1
number = 0
numbers=[-1 for _ in range(N)]
for _ in range(N + 1):
    b = A[a - 1]
    if numbers[b-1] != -1:
      c=numbers[b-1]
      break
    numbers[b-1]=number
    number += 1
    S.append(b)
    a = b
T = S[c+1:number+1]
if K <= number:
  print(S[K ])
else:
  z = K - c-1
  y = z % (number - c)
  print(T[y])
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):

import sys,pprint,readline

av = {}
passed = set()

(N,K) = [int(x) for x in input().split(" ")]

arrivalary = list(map(int,input().split(" ")))



i = 0
loopflg = 0
pos = 1
nextpos = 0
loopstartpos = 0
loopstartnum = 0
loopcount = 0
while i < K:
    passed.add(pos)
    nextpos = arrivalary[pos-1]
    
    if loopflg == 0 and nextpos in passed:
        loopflg = 1
        loopstartpos = nextpos
        loopstartnum = i
        loopcount = 1
    elif loopflg == 1 and nextpos == loopstartpos:
        left = (K-loopstartnum) % loopcount
        i = K-left
    elif loopflg == 1:
        loopcount += 1
    pos = nextpos
    i += 1

print(pos)

Failing test case input:
6 786427263574235267
2 1 1 5 2 2

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):

import sys,pprint,readline

av = {}
passed = set()

(N,K) = [int(x) for x in input().split(" ")]

arrivalary = list(map(int,input().split(" ")))



i = 0
loopflg = 0
pos = 1
nextpos = 0
loopstartpos = 0
loopstartnum = 0
loopcount = 0
while i < K:
    passed.add(pos)
    nextpos = arrivalary[pos-1]
    
    if loopflg == 0 and nextpos in passed:
        loopflg = 1
        loopstartpos = nextpos
        loopstartnum = i
        loopcount = 1
    elif loopflg == 1 and nextpos == loopstartpos:
        left = (K-loopstartnum) % loopcount
        
        i = K-left
        if i == K:
            break
    elif loopflg == 1:
        loopcount += 1
    pos = nextpos
    i += 1

print(pos)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):

from collections import Counter
from copy import deepcopy
import sys
input = sys.stdin.readline
N,K = (int(x) for x in input().split())
A = list(map(int, input().split()))
connection ={}
for i,v in enumerate(A):
    connection[i+1] =v 


passed=[1]
nex =connection[1]
lap =[]
lap_num =0
pass_set =set()
for i in range(1,N+1):
    passed.append(nex)
    if nex not in pass_set:
        pass_set.add(nex)
        nex =connection[nex]
    else:
        
        break

indx_s =passed.index(nex) +1
indx_f =len(passed)
lap_num =indx_f -indx_s

mod =(K -indx_s) % lap_num
ans =passed[indx_s +mod]
print(ans)

Failing test case input:
6 727202214173249351
6 4 1 5 3 2

Expected output:
3


Actual output:
1


Fixed submission (verdict: Accepted):

from collections import Counter
from copy import deepcopy
import sys
input = sys.stdin.readline
N,K = (int(x) for x in input().split())
A = list(map(int, input().split()))
connection ={}
for i,v in enumerate(A):
    connection[i+1] =v 


passed=[1]
nex =connection[1]
lap =[]
lap_num =0
pass_set =set(passed)
for i in range(1,N+1):
    passed.append(nex)
    if nex not in pass_set:
        pass_set.add(nex)
        nex =connection[nex]
    else:
        
        break

indx_s =passed.index(nex) +1
indx_f =len(passed)
lap_num =indx_f -indx_s

if indx_f < K:
    mod =(K -indx_s) % lap_num
    ans =passed[indx_s +mod]
else:
    ans =passed[K]
print(ans)
Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
pi = 3.1415926


def inp():
    return(int(input().rstrip()))
def inlt():
    return(list(map(int,input().rstrip().split())))
def insr():
    s = input().rstrip()
    return(s[:len(s) - 1])
def invr():
    return(map(int,input().rstrip().split()))

n,k = invr()
nums = [0] + inlt()
cycle = [nums[1]]

for i in range(n + 1):
    if nums[cycle[-1]] != cycle[0]:
        cycle.append(nums[cycle[-1]])
    else:
        break

if k <= len(cycle):
    print(cycle[k - 1])
else:
    remain = k % len(cycle)
    print(cycle[remain - 1])

    


Failing test case input:
6 727202214173249351
6 5 4 5 3 2

Expected output:
4


Actual output:
3


Fixed submission (verdict: Accepted):
pi = 3.1415926


def inp():
    return(int(input().rstrip()))
def inlt():
    return(list(map(int,input().rstrip().split())))
def insr():
    s = input().rstrip()
    return(s[:len(s) - 1])
def invr():
    return(map(int,input().rstrip().split()))

n,k = invr()
nums = [0] + inlt()
cycle = [nums[1]]
s = set([cycle[0]])
bp = 0
for i in range(2*n):
    if nums[cycle[-1]] not in s:
        s.add(nums[cycle[-1]])
        cycle.append(nums[cycle[-1]])
        
    else:
        bp = nums[cycle[-1]]
        break

if k <= len(cycle):
    print(cycle[k - 1])
else:
    if bp != 0:
        for i in range(len(cycle)):
            if cycle[i] == bp:
                cycle = cycle[i:]
                break
    k -= i
    remain = k % len(cycle)
    print(cycle[remain - 1])

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N = [int(e) for e in input().split()]
nums = [int(t) for t in input().split()]
count=[]
h = 0
k = 0
check = []
t=1
for i in range(N[0]):
    count.append(0)
while count[t-1] == 0:
    t = nums[k]
    if(count[t-1] == 1):
        break
    else:
        check.append(t)
        count[t-1] += 1
    k = t -1
    h+=1
s = check.index(t)
if(N[1] < s):
    print(check[N[1]])
else:
    ans = (N[1] - s)%(len(check)-s)
    ans1 = check.index(t) - 1 + ans
    print(check[ans1])

Failing test case input:
6 727202214173249351
6 4 1 5 3 2

Expected output:
3


Actual output:
6


Fixed submission (verdict: Accepted):
N = [int(e) for e in input().split()]
nums = [int(t) for t in input().split()]
count=[]
k = 0
check = []
t = nums[0]
for i in range(N[0]):
    count.append(0)
while count[t-1] == 0:
    check.append(t)
    count[t-1] += 1
    k = t -1
    t = nums[k]

s = check.index(t) + 1
if(N[1] < s):
    print(check[N[1] - 1])
else:
    ans = (N[1] - s)%(len(check)+1-s)
    ans1 = check.index(t) + ans
    print(check[ans1])