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

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

L = []
L.append(1)
f = 1
memo = []
flg = 0
leng = 0

while 1:
    L.append(A[f-1])
    memo.append(f)
    f = A[f-1]
    leng += 1
    for i in range(leng):
        if f == memo[i]:
            loop = leng - i
            flg = 1
            break
    if flg == 1:
        break

#print(L)
#NL = L[0:-1]
#print(NL)
#print(memo)
#print(loop)

#a = K % len(NL)
#print(NL[a])

cycle = memo[-loop:]
#print(cycle)

if K <= len(memo) - loop:
    print(memo[K])
else:
    K -= len(memo) - loop
    a = K % len(cycle)
    print(cycle[a])

Fixed submission:
N, K = map(int, input().split())

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

L = []
L.append(1)
f = 1
memo = [-1] * N
memo[0] = 0
cnt = 1

while 1:
    L.append(A[f-1])
    f = A[f-1]
    if memo[f-1] != -1:
        loop = cnt - memo[f-1]
        break
    memo[f-1] = cnt
    cnt += 1

#print(L)
#print(loop)
NL = L[-(loop+1):-1]
#print(NL)

if K <= (len(L) - 1) - loop:
    print(L[K])
else:
    K -= (len(L) - 1) - loop
    a = K % len(NL)
    print(NL[a])

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

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

L = []
L.append(1)
f = 1
memo = []
flg = 0

while 1:
    L.append(A[f-1])
    memo.append(f)
    f = A[f-1]
    leng = len(memo)
    for i in range(leng):
        if f == memo[i]:
            loop = leng - i
            flg = 1
            break
    if flg == 1:
        break

#print(L)
#NL = L[0:-1]
#print(NL)
#print(memo)
#print(loop)

#a = K % len(NL)
#print(NL[a])

cycle = memo[-loop:]
#print(cycle)

if K <= len(memo) - loop:
    print(memo[K])
else:
    K -= len(memo) - loop
    a = K % len(cycle)
    print(cycle[a])

Fixed submission:
N, K = map(int, input().split())

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

L = []
L.append(1)
f = 1
memo = [-1] * N
memo[0] = 0
cnt = 1

while 1:
    L.append(A[f-1])
    f = A[f-1]
    if memo[f-1] != -1:
        loop = cnt - memo[f-1]
        break
    memo[f-1] = cnt
    cnt += 1

#print(L)
#print(loop)
NL = L[-(loop+1):-1]
#print(NL)

if K <= (len(L) - 1) - loop:
    print(L[K])
else:
    K -= (len(L) - 1) - loop
    a = K % len(NL)
    print(NL[a])

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

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

L = []
L.append(1)
f = 1
memo = []
flg = 0

while 1:
    L.append(A[f-1])
    memo.append(f)
    f = A[f-1]
    leng = len(memo)
    for i in range(leng):
        if f == memo[i]:
            loop = leng - i
            flg = 1
            break
    if flg == 1:
        break

#print(L)
#NL = L[0:-1]
#print(NL)
#print(memo)
#print(loop)

#a = K % len(NL)
#print(NL[a])

cycle = memo[-loop:]
#print(cycle)

if K <= len(memo) - loop:
    print(memo[K])
else:
    K -= len(memo) - loop
    a = K % len(cycle)
    print(cycle[a])

Fixed submission:
N, K = map(int, input().split())

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

L = []
L.append(1)
f = 1
memo = [-1] * N
memo[0] = 0
cnt = 1

while 1:
    L.append(A[f-1])
    f = A[f-1]
    if memo[f-1] != -1:
        loop = cnt - memo[f-1]
        break
    memo[f-1] = cnt
    cnt += 1

#print(L)
#print(loop)
NL = L[-(loop+1):-1]
#print(NL)

if K <= (len(L) - 1) - loop:
    print(L[K])
else:
    K -= (len(L) - 1) - loop
    a = K % len(NL)
    print(NL[a])