Problem: The Kingdom of Takahashi has N towns, numbered 1 through N.  There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.  Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.  Help the king by writing a program that answers this question.

Submission attempts (in order):
N,K=map(int,input().split())
N=list(map(int,input().split()))
a={}
 
b=1
a[1]=1
count=0
while(count<=K):
  count+=1
  b=N[b-1]
  #print(b)
  if b in a:
    print(a)
    print(b)
    print(count)
    cycle=count-a[b]
    rest=K-count+1
    ans=rest%cycle
    for i in range(ans-1):
      b=N[b-1]
    print(b)
    break
  a[b]=count

print(b)

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

b=1
a[1]=1
count=1
while(True):
  count+=1
  b=N[b-1]
  #print(b)
  if b in a:
    #print(a)
    #print(b)
    #print(count)
    cycle=count-a[b]
    rest=K-count+1
    ans=rest%cycle
    for i in range(ans-1):
      b=N[b-1]
    print(b)
    break
  a[b]=count

N,K=map(int,input().split())
A=list(map(int,input().split()))
 
a={}
 
b=1
a[1]=1
count=1
while(count<=K+1):
    count+=1
    b=A[b-1]
    if b in a:
        cycle=count-a[b]
        rest=K-count+1
        ans=rest%cycle-1
        for i in range(min([ans,rest])):
            b=A[b-1]
        print(b)
        break
    a[b]=count
    if count==K+1:
        print(b)
        break

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

a={}
 
b=1
a[1]=1
count=1
while(True):
    count+=1
    b=A[b-1]
    if b in a:
        cycle=count-a[b]
        rest=K-count+1
        ans=rest%cycle-1
        for i in range(min([ans,rest])):
            b=A[b-1]
        print(b)
        break
    a[b]=count
    if count==K+1:
        print(b)
        break

import sys

N,K=map(int,input().split())
N=list(map(int,input().split()))
a={}
 
b=1
a[1]=1
count=0
while(count<=K+1):
  count+=1
  b=N[b-1]
  #print(b)
  if b in a:
    #print(a)
    #print(b)
    #print(count)
    cycle=count-a[b]
    rest=K-count+1
    ans=rest%cycle
    for i in range(ans-1):
      b=N[b-1]
      count+=1
      if count==K+1:
        print(b)
        sys.exit()
    print(b)
    break
  a[b]=count
  if count==K+1:
    print(b)
    break

N,K=map(int,input().split())
N=list(map(int,input().split()))
a={}
 
b=1
a[1]=1
count=0
while(count<=K+1):
  count+=1
  b=N[b-1]
  #print(b)
  if b in a:
    #print(a)
    #print(b)
    #print(count)
    cycle=count-a[b]
    rest=K-count+1
    ans=rest%cycle
    for i in range(ans-1):
      b=N[b-1]
      count+=1
      if count==K+1:
        break
    print(b)
    break
  a[b]=count
  if count==K+1:
    print(b)
    break

N,K=map(int,input().split())
A=list(map(int,input().split()))
 
a={}
 
b=1
a[1]=1
count=1
while(count<=K+1):
    count+=1
    b=A[b-1]
    if b in a:
        cycle=count-a[b]
        rest=K-count+1
        ans=rest%cycle-1
        for i in range(min([ans,rest])):
            b=A[b-1]
        print(b)
        break
    a[b]=count
    if count==K+1:
        print(b)
        break

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

N=6
K=727202214173249351
A=[6,5,2,5,3,2]

a={}
 
b=1
a[1]=1
count=1
while(True):
    count+=1
    b=A[b-1]
    if b in a:
        cycle=count-a[b]
        rest=K-count+1
        ans=rest%cycle
        for i in range(min([ans,rest])):
            b=A[b-1]
        print(b)
        break
    a[b]=count
    if count==K+1:
        print(b)
        break

N,K=map(int,input().split())
N=list(map(int,input().split()))
a={}
 
b=1
a[1]=1
count=0
while(count<=K+1):
  count+=1
  b=N[b-1]
  #print(b)
  if b in a:
    #print(a)
    #print(b)
    #print(count)
    cycle=count-a[b]
    rest=K-count+1
    ans=rest%cycle
    for i in range(ans-1):
      b=N[b-1]
    print(b)
    break
  a[b]=count
  if count==K+1:
    print(b)
    break

N,K=map(int,input().split())
N=list(map(int,input().split()))
a={}
 
b=1
a[1]=1
count=1
while(True):
  count+=1
  b=N[b-1]
  #print(b)
  if b in a:
    #print(a)
    #print(b)
    #print(count)
    cycle=count-a[b]
    rest=K-count+1
    ans=rest%cycle
    for i in range(ans-1):
      b=N[b-1]
    print(b)
    break
  a[b]=count
if count==K+1:
  print(b)
  break

N,K=map(int,input().split())
N=list(map(int,input().split()))
a={}
 
b=1
a[1]=1
count=1
while(True):
  count+=1
  b=N[b-1]
  #print(b)
  if b in a:
    #print(a)
    #print(b)
    #print(count)
    cycle=count-a[b]
    rest=K-count+1
    ans=rest%cycle
    for i in range(ans-1):
      b=N[b-1]
    print(b)
    break
  a[b]=count
  if count==K+1:
    print(b)
    break

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

count=0
k=1
countlist={}

count=1
while True:    
    k=A[k-1]
    
    if k in countlist:
        cycle=count-countlist[k]
        rest=K-count
        move=rest%cycle
        for i in range(min(rest,move)):
            k=A[k-1]
        print(k)
        break
    
    countlist[k]=count
    
    if count==K:
        print(k)
        break
    
    count+=1

Verdicts: Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Runtime Error

Wrong Answer

Wrong Answer

Runtime Error

Runtime Error

Wrong Answer

Accepted