Buggy python submission (verdict: Time Limit Exceeded):
def doubling(n,k,a):
    logk = 1
    beki = 2
    while beki < k:
        logk += 1
        beki = beki << 1
    dp = [[0 for _ in range(n)] for _ in range(logk)]
    for i in range(n):
        dp[0][i] = a[i]
    for i in range(logk-1):
        for j in range(n):
            dp[i+1][j] = dp[i][dp[i][j]]
    now = 0
    index = 0
    while k > 0:
        if k&1:
            now = dp[index][now]
        k = k >> 1
        index += 1
    return now+1


def main():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    for i in range(n):
        a[i] -= 1
    print(doubling(n,k,a))    

if __name__ == "__main__":
    main()

Fixed submission:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n,k = inpm()
    a = inpl()
    past = [0 for _ in range(n)]
    past[0] = 1
    root = [0]
    pre = 0
    while True:
        Next = a[pre]-1
        if past[Next]:
            break
        root.append(Next)
        pre = Next
        past[Next] = 1
    rev = []
    i = 0
    while root[len(root)-1-i] != Next:
        rev.append(root[len(root)-1-i])
        i += 1
    rev.reverse()
    rever = [Next]
    for i in range(len(rev)):
        rever.append(rev[i])
    n1 = len(root)
    n2 = len(rever)
    if n1-1 >= k:
        print(root[k]+1)
        return
    if rever[0] != 0:
        k -= n1-n2
    print(rever[(k)%n2]+1)
    
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def doubling(n,k,a):
    logk = 1
    while pow(2,logk) < k:
        logk += 1
    dp = [[0 for _ in range(n)] for _ in range(logk)]
    for i in range(n):
        dp[0][i] = a[i]
    for i in range(logk-1):
        for j in range(n):
            dp[i+1][j] = dp[i][dp[i][j]]
    now = 0
    index = 0
    while k > 0:
        if k&1:
            now = dp[index][now]
        k = k >> 1
        index += 1
    return now+1


def main():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    for i in range(n):
        a[i] -= 1
    print(doubling(n,k,a))    

if __name__ == "__main__":
    main()

Fixed submission:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n,k = inpm()
    a = inpl()
    past = [0 for _ in range(n)]
    past[0] = 1
    root = [0]
    pre = 0
    while True:
        Next = a[pre]-1
        if past[Next]:
            break
        root.append(Next)
        pre = Next
        past[Next] = 1
    rev = []
    i = 0
    while root[len(root)-1-i] != Next:
        rev.append(root[len(root)-1-i])
        i += 1
    rev.reverse()
    rever = [Next]
    for i in range(len(rev)):
        rever.append(rev[i])
    n1 = len(root)
    n2 = len(rever)
    if n1-1 >= k:
        print(root[k]+1)
        return
    if rever[0] != 0:
        k -= n1-n2
    print(rever[(k)%n2]+1)
    
if __name__ == "__main__":
    main()