Buggy python submission (verdict: Runtime Error):
import numpy as np
from itertools import product
n, m, x = [int(x) for x in input().split()]
a = np.stack([np.array(input().split(), dtype=int) for _ in range(n)])
tot = np.sum(a, axis=0)
if min(tot[1:]) < x:
    print(-1)
    exit(0)
cost = tot[0]
whole = np.array(list(product([0,1],repeat=n)), dtype=bool)[1:-1]
for flag in whole:
    tot = np.sum(a[flag], axis=0)
    if min(tot[1:]) >= x and tot[0] < cost:
        cost = tot[0]
print(cost)


Fixed submission:
n, k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]
assert len(a) == n
current = 0
visited = [-1] * n
visited[0] = 0
for i in range(1, k):
  current = a[current]
  if visited[current] == -1:
    visited[current] = i
  else:
    pre = visited[current]
    count = (k - pre) % (i - pre) + pre
    print(visited.index(count)+1)
    break
else:
  print(a[current]+1)

Buggy python submission (verdict: Runtime Error):
n, k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]
assert len(a) == n
current = 0
visited = [-1] * n
visited[0] = 0
for i in range(1, k):
  current = a[current]
  if visited[current] == -1:
    visited[current] = i
  else:
    pre = visited[current]
    count = (k - pre) % (i - pre) + pre
    print(visited.index(count)+1)
else:
  print(a[current]+1)


Fixed submission:
n, k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]
assert len(a) == n
current = 0
visited = [-1] * n
visited[0] = 0
for i in range(1, k):
  current = a[current]
  if visited[current] == -1:
    visited[current] = i
  else:
    pre = visited[current]
    count = (k - pre) % (i - pre) + pre
    print(visited.index(count)+1)
    break
else:
  print(a[current]+1)

Buggy python submission (verdict: Runtime Error):
n, k = [int(x) for x in input().strip().split()]
a = [int(x)-1 for x in input().strip().split()]
assert len(a) == n
current = 0
visited = [-1] * n
visited[0] = 0
way = [0]
for i in range(1, k):
  current = a[current]
  if visited[current] == -1:
    visited[current] = i
  else:
    pre = visited[current]
    count = (k - pre) % (i - pre) + pre
    print(visited.index(count)+1)
else:
  print(a[current]+1)


Fixed submission:
n, k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]
assert len(a) == n
current = 0
visited = [-1] * n
visited[0] = 0
for i in range(1, k):
  current = a[current]
  if visited[current] == -1:
    visited[current] = i
  else:
    pre = visited[current]
    count = (k - pre) % (i - pre) + pre
    print(visited.index(count)+1)
    break
else:
  print(a[current]+1)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = [int(x) for x in input().strip().split()]
a = [int(x) for x in input().strip().split()]
assert len(a) == n
a = [0] + a
current = 1
way = [1]
for i in range(k):
  current = a[current]
  if current in way:
    li = way.index(current)
    loop = way[li:]
    print(loop[(k - li) % len(loop)])
    break
  else:
    way.append(current)
else:
  print(current)


Fixed submission:
n, k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]
assert len(a) == n
current = 0
visited = [-1] * n
visited[0] = 0
for i in range(1, k):
  current = a[current]
  if visited[current] == -1:
    visited[current] = i
  else:
    pre = visited[current]
    count = (k - pre) % (i - pre) + pre
    print(visited.index(count)+1)
    break
else:
  print(a[current]+1)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = [int(x) for x in input().strip().split()]
a = [int(x)-1 for x in input().strip().split()]
assert len(a) == n
current = 0
way = [0]
for i in range(k):
  current = a[current]
  if current in way:
    li = way.index(current)
    loop = way[li:]
    print(loop[(k - li) % len(loop)]+1)
    break
  else:
    way.append(current)
else:
  print(current+1)


Fixed submission:
n, k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]
assert len(a) == n
current = 0
visited = [-1] * n
visited[0] = 0
for i in range(1, k):
  current = a[current]
  if visited[current] == -1:
    visited[current] = i
  else:
    pre = visited[current]
    count = (k - pre) % (i - pre) + pre
    print(visited.index(count)+1)
    break
else:
  print(a[current]+1)