Buggy python submission (verdict: Runtime Error):
temp = input()
temp = temp.split()
town_num = int(temp[0])
move = int(temp[1])

town = input()
town = town.split()
town = [int(s) for s in town]
data = [0] * 1000

position = 1

if move < 1000:
    for i in range(move):
      position = town[position-1]

else:
    for i in range(town_num):
        data[i] = town[position-1]
        position = town[position-1]
        if data[i] == data[4] and i > 4:
            count = i
            break
    cycle = count - 4
    try:
        period = (move-4)%cycle
        position = data[period+4]
        position = town[position-1]-1
    except ZeroDivisionError:
        a = 1

print(position)

Fixed submission:
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))

journey = [1]

for i in range(2*n + 100):
    nxt = a[journey[-1]]
    journey.append(nxt)
    
if k < n + 1:
    print(journey[k])
    exit()

loop_end = n

loop_start = n-1

while(journey[loop_start] != journey[loop_end]):
    loop_start -= 1

period = loop_end - loop_start

k %= period
while k<= n:
    k += period

print(journey[k])

Buggy python submission (verdict: Runtime Error):
temp = input()
temp = temp.split()
town_num = int(temp[0])
move = int(temp[1])

town = input()
town = town.split()
town = [int(s) for s in town]
data = [0] * 1000

position = 1

if move < 1000:
    for i in range(move):
      position = town[position-1]

else:
    for i in range(town_num):
        data[i] = town[position-1]
        position = town[position-1]
        if data[i] == data[4] and i > 4:
            count = i
            break
    cycle = count - 4
    try:
        period = (move-4)%cycle
        position = data[period+4]
        position = town[position-1]-1
    except ZeroDivisionError:
        a = 1


print(position)

Fixed submission:
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))

journey = [1]

for i in range(2*n + 100):
    nxt = a[journey[-1]]
    journey.append(nxt)
    
if k < n + 1:
    print(journey[k])
    exit()

loop_end = n

loop_start = n-1

while(journey[loop_start] != journey[loop_end]):
    loop_start -= 1

period = loop_end - loop_start

k %= period
while k<= n:
    k += period

print(journey[k])

Buggy python submission (verdict: Runtime Error):
temp = input()
temp = temp.split()
town_num = int(temp[0])
move = int(temp[1])

town = input()
town = town.split()
town = [int(s) for s in town]
data = [0] * 1000

position = 1

if move < 1000:
    for i in range(move):
      position = town[position-1]

else:
    for i in range(town_num):
        data[i] = town[position-1]
        position = town[position-1]
        if data[i] == data[4] and i > 4:
            count = i
            break
    cycle = count - 4
    try:
        period = (move-4)%cycle
        position = data[period+4]
        position = town[position-1]-1
    except ZeroDivisionError:
        a = 1


print(position)

Fixed submission:
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))

journey = [1]

for i in range(2*n + 100):
    nxt = a[journey[-1]]
    journey.append(nxt)
    
if k < n + 1:
    print(journey[k])
    exit()

loop_end = n

loop_start = n-1

while(journey[loop_start] != journey[loop_end]):
    loop_start -= 1

period = loop_end - loop_start

k %= period
while k<= n:
    k += period

print(journey[k])