Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
      
A =  list(map(int, input().split()))
B = [0] * (N + 1)
 
for i in range(N):
  B[i+1] = A[i]

start = 1
d = []
first = 0
while True:
  a = B[start]
  start = a
  first += 1
  if d.count(a) == 1:
    break
  d.append(a)

'''second = 0
start_ = start
while True:
  start_ = B[start_]
  second += 1
  if start_ == start:
    break'''
second  =  len(d) - d.index(start)
#print(first, second, start, d, len(d) - d.index(start))

if K <= first:
  S = 1
else:
  K -= first
  K %= second
  S = start

for i in range(K):
  S = B[S]
print(S)

Fixed submission:
def main():

  N, K = map(int, input().split())   
  A = [0] + list(map(int, input().split()))
  cur = 1
  visited = [-1] * (N + 1)
  visited[cur] = 0
  for i in range(1, K + 1):
    next_cur = A[cur]
    if next_cur == cur:
      print(next_cur)
      return
    elif visited[next_cur] != -1:
      mod = i - visited[next_cur]
      start = visited[next_cur]
      break
    else:
      visited[next_cur] = visited[cur] + 1
      cur = next_cur
  else:
    print(next_cur)
    return
  K = (K - start) % mod
  print(visited.index(K+start))
  
  
if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N, K = map(int, input().split())
      
A =  list(map(int, input().split()))
B = [0] * (N + 1)

for i in range(N):
  B[i+1] = A[i]

start = 1
d = deque([])
first = 0
while True:
  a = B[start]
  start = a
  first += 1
  if d.count(a) == 1:
    break
  d.append(a)

second = 0
start_ = start
while True:
  start_ = B[start_]
  second += 1
  if start_ == start:
    break

if K <= first:
  S = 1
else:
  K -= first
  K %= second
  S = start

for i in range(K):
  S = B[S]
print(S)

Fixed submission:
def main():

  N, K = map(int, input().split())   
  A = [0] + list(map(int, input().split()))
  cur = 1
  visited = [-1] * (N + 1)
  visited[cur] = 0
  for i in range(1, K + 1):
    next_cur = A[cur]
    if next_cur == cur:
      print(next_cur)
      return
    elif visited[next_cur] != -1:
      mod = i - visited[next_cur]
      start = visited[next_cur]
      break
    else:
      visited[next_cur] = visited[cur] + 1
      cur = next_cur
  else:
    print(next_cur)
    return
  K = (K - start) % mod
  print(visited.index(K+start))
  
  
if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N, K = map(int, input().split())
      
A =  list(map(int, input().split()))
B = [0] * (N + 1)

for i in range(N):
  B[i+1] = A[i]
#print(B)

start = 1
d = deque([])
first = 0
while True:
  a = B[start]
  start = a
  first += 1
  if a in d:
    break
  d.append(a)
    
#print(first, start)

second = 0
start_ = start
while True:
  start_ = B[start_]
  second += 1
  if start_ == start:
    break
#print(second)

if K <= first:
  S = 1
else:
  K -= first
  K %= second
  S = start

for i in range(K):
  S = B[S]
print(S)

Fixed submission:
def main():

  N, K = map(int, input().split())   
  A = [0] + list(map(int, input().split()))
  cur = 1
  visited = [-1] * (N + 1)
  visited[cur] = 0
  for i in range(1, K + 1):
    next_cur = A[cur]
    if next_cur == cur:
      print(next_cur)
      return
    elif visited[next_cur] != -1:
      mod = i - visited[next_cur]
      start = visited[next_cur]
      break
    else:
      visited[next_cur] = visited[cur] + 1
      cur = next_cur
  else:
    print(next_cur)
    return
  K = (K - start) % mod
  print(visited.index(K+start))
  
  
if __name__ == "__main__":
  main()