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 = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]

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


n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

visited = []
x=0
for i in range(n):
  x = a[x]
  if x in visited:
    for j in range(i):
      if visited[j] == x:
        first = j
        break
    T = i - j
    break
  else:
    visited.append(a[i])
mod = (k-first) % T
print(a[first+mod]+1)


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])

n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

x = 0
for i in range(k):
  x = a[x]
print(x+1)

n, k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]

visited = [0]
x = 0
T = -1
while T < 0:
  x = a[x]
  if x in visited:
    for j in range(i):
      if visited[j] == x:
        first = j
        break
    T = i - j
  else:
    visited.append(x)
  break
    
mod = (k-first) % T
print(a[first+mod]+1)


n, k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]

visited = [0]
x = 0
for i in range(n):
  x = a[x]
  if x in visited:
    for j in range(i):
      if visited[j] == x:
        first = j
        break
    T = i - j
  break
  else:
    
mod = (k-first) % T
print(a[first+mod]+1)


n, k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]

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

n, k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]

visited = []
x=0
for i in range(n):
  x = a[x]
  if x in visited:
    for j in range(i):
      if visited[j] == x:
        first = j
        break
    T = i - j
  break
  else:
    visited.append(a[i])
mod = (k-first) % T
print(a[first+mod]+1)


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])


n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

visited = [0]
x=1
for i in range(n):
  x = a[i]
  if x in visited:
    for j in range(i):
      if visited[j] == x:
        first = j
        break
    T = i - j
  else:
    visited.append(a[i])
mod = (k-first) % T
print(a[first+mod])


n, k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]

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


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]
    break
if k < s:
  print(visited[k])
else:
  mod = (k-s) % T
  print(visited[s+mod])


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 = []
order = {}
x = 1
T = -1

while visited[x] == -1:
  x = a[x-1] 
  points.append(x)
  visited[x] = len(points)
T = len(points) - visited[x]
s = visited[x]
if k < s:
  print(points[k])
else:
  k -= s
  mod = k % T
  print(points[s+mod])
  

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])
  


Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Wrong Answer

Runtime Error

Accepted