Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
b = [1]
c = a[1]
d = 0
e=1
length = 0
first = 0
ans = 0


while c not in b:
    b += [c]
    c = a[c]
    d = c
    e+=1

if d != 0:
    length = e - b.index(d)
    first = b.index(d)
if k <= first:
    ans = b[k]
elif d == 0:
    ans = 1
else:
    ans = b[(k - first) % length + first]
print(ans)


Fixed submission:
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
t = [0 for i in range(n+1)]
b = [1]
c = a[1]
d = 0
e=1
length = 0
first = 0
ans = 0


while t[d] < 1:
    t[d]=e
    b += [c]
    c = a[c]
    d = c
    e+=1

if d != 0:
    length = e - t[d]
    first = b.index(d)
if k <= first:
    ans = b[k]
elif d == 0:
    ans = 1
else:
    ans = b[(k - first) % length + first]
print(ans)

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
b = [1]
c = a[1]
d = 0
e=0
length = 0
first = 0
ans = 0


while c not in b:
    b += [c]
    c = a[c]
    d = c
    e+=1

if d != 0:
    length = e - b.index(d)
    first = b.index(d)
if k <= first:
    ans = b[k]
elif d == 0:
    ans = 1
else:
    ans = b[(k - first) % length + first]
print(ans)


Fixed submission:
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
t = [0 for i in range(n+1)]
b = [1]
c = a[1]
d = 0
e=1
length = 0
first = 0
ans = 0


while t[d] < 1:
    t[d]=e
    b += [c]
    c = a[c]
    d = c
    e+=1

if d != 0:
    length = e - t[d]
    first = b.index(d)
if k <= first:
    ans = b[k]
elif d == 0:
    ans = 1
else:
    ans = b[(k - first) % length + first]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
b = [1]
c = a[1]
d = 0
length = 0
first = 0
ans = 0

while c not in b:
    b += [c]
    c = a[c]
    d = c
    
if d != 0:
    length = len(b) - b.index(d)
    first = b.index(d)
if k <= first:
    ans = b[k]
elif d == 0:
    ans = 1
else:
    ans = b[(k - first) % length + first]
print(ans)


Fixed submission:
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
t = [0 for i in range(n+1)]
b = [1]
c = a[1]
d = 0
e=1
length = 0
first = 0
ans = 0


while t[d] < 1:
    t[d]=e
    b += [c]
    c = a[c]
    d = c
    e+=1

if d != 0:
    length = e - t[d]
    first = b.index(d)
if k <= first:
    ans = b[k]
elif d == 0:
    ans = 1
else:
    ans = b[(k - first) % length + first]
print(ans)

Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))
b = [1]
c = a[1]
d = 0
length = 0
first = 0
ans = 0

while c not in b:
    b += [c]
    c = a[c]
    d = c

length = len(b)-b.index(d)
first = b.index(d)
if k <= first:
    ans = b[k]
elif length == 0:
    ans = 1
else:
    ans = b[(k-first)%length+first]
print(ans)


Fixed submission:
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
t = [0 for i in range(n+1)]
b = [1]
c = a[1]
d = 0
e=1
length = 0
first = 0
ans = 0


while t[d] < 1:
    t[d]=e
    b += [c]
    c = a[c]
    d = c
    e+=1

if d != 0:
    length = e - t[d]
    first = b.index(d)
if k <= first:
    ans = b[k]
elif d == 0:
    ans = 1
else:
    ans = b[(k - first) % length + first]
print(ans)

Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))
b = [1]
c = a[1]
d = 0
length = 0
first = 0
ans = 0

while c not in b:
    b += [c]
    c = a[c]
    d = c

length = len(b)-b.index(d)
first = b.index(d)
if k <= first:
    ans = b[k]
else:
    ans = b[(k-first)%length+first]
print(ans)

Fixed submission:
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
t = [0 for i in range(n+1)]
b = [1]
c = a[1]
d = 0
e=1
length = 0
first = 0
ans = 0


while t[d] < 1:
    t[d]=e
    b += [c]
    c = a[c]
    d = c
    e+=1

if d != 0:
    length = e - t[d]
    first = b.index(d)
if k <= first:
    ans = b[k]
elif d == 0:
    ans = 1
else:
    ans = b[(k - first) % length + first]
print(ans)

Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))
b = [1]
c = a[1]
d = 0
length = 0
first = 0
ans = 0

while c not in b:
    b += [c]
    c = a[c]
    d = c

length = len(b)-b.index(d)
first = b.index(d)
ans = b[(k-first)%length+first]
print(ans)

Fixed submission:
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
t = [0 for i in range(n+1)]
b = [1]
c = a[1]
d = 0
e=1
length = 0
first = 0
ans = 0


while t[d] < 1:
    t[d]=e
    b += [c]
    c = a[c]
    d = c
    e+=1

if d != 0:
    length = e - t[d]
    first = b.index(d)
if k <= first:
    ans = b[k]
elif d == 0:
    ans = 1
else:
    ans = b[(k - first) % length + first]
print(ans)