Buggy python submission (verdict: Runtime Error):
import sys

lines = sys.stdin.readlines()
N, M, X = map(int, lines[0].split())
A = []
C = []

for i in range(N):
    line = list(map(int, lines[i+1].split()))
    A.append(line[1:])
    C.append(line[0])

# return []
def full_search():
    costs = []
    for i in range(2**N):
        vals = [0] * M
        cost = 0

        # iを二進数に直して扱う。それぞれのビットがn冊目の本を買うかどうかに匹敵。
        for j in range(N):
            # ビットを移動しながらn冊目の本を買う際のコストと経験値を計算する
            if ((i >> j) & 1):
                # 現在の組み合わせに対して
                # 全てのアルゴリズム分を計算
                cost += C[j]

                # 全てのアルゴリズム(m)に対して経験値を求める
                for m in range(M):
                    vals[m] += A[j][m]

        combination_ok = True
        for m in range(M):
            if vals[m] >= X:
                continue
            else:
                combination_ok = False
                break

        if combination_ok:
            costs.append(cost)
    return costs

costs = full_search()
if len(costs) == 0:
    print(-1)
else:
    print(min(costs))


Fixed submission:
import sys

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

i = 1
l = [1] * N
s = set([1])
n = 1
while True:
    n = A[n-1]
    if n in s:
        l = l[:i]
        mod = i - l.index(n)
        init_len = l.index(n)
        break
    else:
        l[i] = n
        s.add(n)
        i += 1

if K < init_len:
    print(l[K])
else:
    rest = ((K-init_len) % mod)
    print(l[init_len+rest])

Buggy python submission (verdict: Time Limit Exceeded):
import sys

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

i = 1
l = [1] * N
n = 1
while True:
    n = A[n-1]
    if n in l:
        l = l[:i]
        mod = i - l.index(n)
        init_len = l.index(n)
        break
    else:
        l[i] = n
        i += 1

if K < init_len:
    print(l[K])
else:
    rest = ((K-init_len) % mod)
    print(l[init_len+rest])


Fixed submission:
import sys

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

i = 1
l = [1] * N
s = set([1])
n = 1
while True:
    n = A[n-1]
    if n in s:
        l = l[:i]
        mod = i - l.index(n)
        init_len = l.index(n)
        break
    else:
        l[i] = n
        s.add(n)
        i += 1

if K < init_len:
    print(l[K])
else:
    rest = ((K-init_len) % mod)
    print(l[init_len+rest])

Buggy python submission (verdict: Time Limit Exceeded):
import sys

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

l = [1]
n = 1
while True:
    n = A[n-1]
    if n in l:
        mod = len(l) - l.index(n)
        init_len = l.index(n)
        break
    else:
        l.append(n)

if K < init_len:
    print(l[K])
else:
    rest = ((K-init_len) % mod)
    print(l[init_len+rest])


Fixed submission:
import sys

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

i = 1
l = [1] * N
s = set([1])
n = 1
while True:
    n = A[n-1]
    if n in s:
        l = l[:i]
        mod = i - l.index(n)
        init_len = l.index(n)
        break
    else:
        l[i] = n
        s.add(n)
        i += 1

if K < init_len:
    print(l[K])
else:
    rest = ((K-init_len) % mod)
    print(l[init_len+rest])