Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))

now = 1
path = [1]

for i in range(N + 2):
    now = A[now]
    path.append(now)

if i >= K:
    print(path[K])
    quit()

now = path.pop()
lo_start = i - 1
while path[lo_start] != now:
    lo_start -= 1

lo_len = i - lo_start
loop = path[lo_start:]

ans = loop[(K - path.index(lo_start)) % lo_len]

print(ans)

Fixed submission:
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))

now = 1
path = [1]

for i in range(N + 2):
    now = A[now]
    path.append(now)

if i >= K:
    print(path[K])
    quit()

now = path.pop()
lo_start = i - 1
while path[lo_start] != now:
    lo_start -= 1

loop = path[lo_start:]

ans = loop[(K - path.index(now)) % len(loop)]

print(ans)

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))

now = 1
path = [1]

for i in range(N + 2):
    now = A[now]
    i += 1
    if now in path: break
    path.append(now)

if i >= K:
    print(path[K])
    quit()

lo_start = path.index(now)
lo_len = i - lo_start
loop = path[lo_start:]

ans = loop[(K - lo_start) % lo_len]

print(ans)

Fixed submission:
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))

now = 1
path = [1]

for i in range(N + 2):
    now = A[now]
    path.append(now)

if i >= K:
    print(path[K])
    quit()

now = path.pop()
lo_start = i - 1
while path[lo_start] != now:
    lo_start -= 1

loop = path[lo_start:]

ans = loop[(K - path.index(now)) % len(loop)]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))

now = 1
path = [1]

for i in range(N + 2):
    now = A[now]
    i += 1
    if now in path: break
    path.append(now)

if i >= K:
    print(path[K])
    quit()

lo_start = path.index(now)
lo_len = i - lo_start
loop = path[lo_start:]

ans = loop[(K - lo_start) % lo_len]

print(ans)

Fixed submission:
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))

now = 1
path = [1]

for i in range(N + 2):
    now = A[now]
    path.append(now)

if i >= K:
    print(path[K])
    quit()

now = path.pop()
lo_start = i - 1
while path[lo_start] != now:
    lo_start -= 1

loop = path[lo_start:]

ans = loop[(K - path.index(now)) % len(loop)]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))

now = 1
path = [1]

i = 0
while i < N + 2:
    now = A[now]
    i += 1
    if now in path: break
    path.append(now)

if i >= K:
    print(path[K])
    quit()

lo_start = path.index(now)
lo_len = i - lo_start
loop = path[lo_start:]

ans = loop[(K - lo_start) % lo_len]

print(ans)

Fixed submission:
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))

now = 1
path = [1]

for i in range(N + 2):
    now = A[now]
    path.append(now)

if i >= K:
    print(path[K])
    quit()

now = path.pop()
lo_start = i - 1
while path[lo_start] != now:
    lo_start -= 1

loop = path[lo_start:]

ans = loop[(K - path.index(now)) % len(loop)]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))

now = 1
path = [1]
cnt = 1


i = 0
while i < min(N + 2, K):
    now = A[now]
    i += 1
    if now in path: break
    path.append(now)

if i == K:
    print(now)
    quit()

lo_start = path.index(now)
lo_len = i - lo_start
loop = path[lo_start:]

ans = loop[(K - lo_start) % lo_len]

print(ans)

Fixed submission:
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))

now = 1
path = [1]

for i in range(N + 2):
    now = A[now]
    path.append(now)

if i >= K:
    print(path[K])
    quit()

now = path.pop()
lo_start = i - 1
while path[lo_start] != now:
    lo_start -= 1

loop = path[lo_start:]

ans = loop[(K - path.index(now)) % len(loop)]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))

now = 1
path = [1]
cnt = 1


i = 0
while i < min(N + 2, K):
    now = A[now]
    i += 1
    if now in path: break
    path.append(now)

if i == K:
    print(now)
    quit()

lo_start = path.index(now)
lo_len = i - lo_start
loop = path[lo_start:]

print(loop[(K - lo_start) % lo_len])

Fixed submission:
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))

now = 1
path = [1]

for i in range(N + 2):
    now = A[now]
    path.append(now)

if i >= K:
    print(path[K])
    quit()

now = path.pop()
lo_start = i - 1
while path[lo_start] != now:
    lo_start -= 1

loop = path[lo_start:]

ans = loop[(K - path.index(now)) % len(loop)]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = list(map(int, input().split()))

here = 1
log = [1]
i = K
while i > 0:
    if i >= 2:
        here = A[here - 1]
        log.append(here)
        here = A[here - 1]

        if here in log:
            loopuntil = log.index(here)
            mod = len(log[loopuntil:])
            i = (K - loopuntil) % mod
            log = []


        else:
            log.append(here)
            i = i - 2

    if i == 1:
        here = A[here - 1]
        i = 0

print(here)

Fixed submission:
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))

now = 1
path = [1]

for i in range(N + 2):
    now = A[now]
    path.append(now)

if i >= K:
    print(path[K])
    quit()

now = path.pop()
lo_start = i - 1
while path[lo_start] != now:
    lo_start -= 1

loop = path[lo_start:]

ans = loop[(K - path.index(now)) % len(loop)]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = list(map(int, input().split()))

here = 1
log = [1]
i = K
while i > 0:
    here = A[here-1]

    if here in log:
        loopuntil = log.index(here)
        mod = len(log[loopuntil:])
        i = (K - loopuntil) % mod
        log = []


    else:
        log.append(here)
        i = i - 1

print(here)

Fixed submission:
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))

now = 1
path = [1]

for i in range(N + 2):
    now = A[now]
    path.append(now)

if i >= K:
    print(path[K])
    quit()

now = path.pop()
lo_start = i - 1
while path[lo_start] != now:
    lo_start -= 1

loop = path[lo_start:]

ans = loop[(K - path.index(now)) % len(loop)]

print(ans)