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())
A = [int(input()) for _ in range(n)]
B = []
b = 1
ans = 1
while ans:
  a = b
  b = A[a-1]
  B.append(b)
  if len(B) == len(A):
    ans = 0
  else:
    continue

print(B[k-1])


n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  a = b
  b = A[a-1]
  B.append(b)
  if len(B) == len(A):
    ans = 0
  else:
    continue

B = B*1000000000000000000000
print(B[k-1])


n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  a = b
  b = A[a-1]
  B.append(b)
  if len(B) == len(A):
    ans = 0
  else:
    continue

print(B[k-1])


n,k = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
B = []
b = 1
ans = 1
while ans:
  a = b
  b = A[a-1]
  B.append(b)
  if len(B) == len(A):
    ans = 0
  else:
    continue

print(B[k-1])


n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  b = A[b-1]
  B.append(b)
  if len(A) == len(B):
    ans = 0


n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
B.append(A[0])
b = 1

while True:
  a = A[b-1]
  b = A[a-1]
  B.append(b)
  if len(B) == len(A):
    break

print(B[k])


n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
B.append(A[0])
b = 1
ans = 1
while ans:
  a = A[b-1]
  b = A[a-1]
  B.append(b)
  if len(B) == len(A):
    ans = 0

print(B[k])


n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1


n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  a = b
  b = A[a-1]
  B.append(b)
  if len(B) == k:
    ans = 0
  if B[-1] == B[-2]:
    ans = 0
print(B[k-1])


n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
B.append(A[0])
b = 1
ans = 1
while ans:
  a = b
  b = A[a-1]
  B.append(b)
  if len(B) == len(A):
    ans = 0

print(B[k])


n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
B.append(A[0])
c = 0
while True:
  b = A[0]
  c = A[b-1]
  B.append(c)
  if c == b:
    break
  if len(B) == len(A):
    break
print(B[k])

  


n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  a = b
  b = A[a-1]
  B.append(b)
  if len(B) == len(A):
    ans = 0
C = B * 10000000000
print(C[k-1])


n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
B.append(A[0])
b = 0
c = 1
while True:
  b = A[c-1]
  c = A[b-1]
  B.append(c)
  if A[-1] == A[-2]:
    break
  if len(B) == len(A):
    break
print(B[k])


n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
ans = 1
c = 1
while ans:
  b = A[c-1]
  c = A[b-1]
  c.append(B)
  if c == b:
    B = B*k
    break
print(B[k-1])
  
  

n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
B.append(A[0])
c = 1
while True:
  b = A[c-1]
  c = A[b-1]
  B.append(c)
  if c == b:
    break
  if len(B) == len(A):
    break
B = B*(10**18)
print(B[k])

  


n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  b = A[b-1]
  B.append(b)
  if len(B) == len(A):
    

n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  a = b
  b = A[a-1]
  B.append(b)
  if len(B) == k:
    ans = 0
  if B[-1] == B[-2]:
    ans = 0
cnt = k % len(B)
print(B[cnt])


n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  a = b
  b = A[a-1]
  B.append(b)
  if len(B) == len(A):
    ans = 0

print(B[k-1])


n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  b = A[b-1]
  B.append(b)
  if len(A) == len(B):
    ans = 0
B = B*1000000000
print(B[k-1])

n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  b = A[b-1]
  B.append(b)
  if len(A) == len(B):
    ans = 0

print(B[k])

n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  b = A[b-1]
  B.append(b)
  if len(B) == len(A):
    ans = 0
    
print(B[k-1])


n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  b = A[b-1]
  B.append(b)
  

N,K = map(int,input().split())
A = list(map(int,input().split()))
pos = 0
visit = [0] * N
move = []
roop = []
while visit[pos]!=2:
    if visit[pos]==0:
        move.append(pos)
    else:
        roop.append(pos)
    visit[pos] += 1
    pos = A[pos] - 1
if len(move)>K:
    print(move[K]+1)
else:
    print(roop[(K-(len(move)-len(roop)))%len(roop)]+1)


Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Time Limit Exceeded

Accepted