CodeEff/ECCO huggingface dataset:
# Original solution
def main():
    n,k = list(map(int,input().split()))
    a = list(map(int,input().split()))
    from collections import deque
    q = deque()
    p = deque()
    q.append(1)
    buf = 0
    flg = True
    idx = 1
    cnt = 0
    while flg:
        buf = idx
        idx = a[idx-1]
        cnt += 1
        if cnt == k:
            print(idx)
            return 0
        if idx in q:
            flg = False
        else:
            q.append(idx)
    for i in range(len(q)):
        tmp = q.pop()
        p.appendleft(tmp)
        if tmp == idx:
            break
    if k < len(q):
        print((list(q)[k%len(q)]))
    else:
        k -= len(q)
        print((list(p)[k%len(p)]))
    return 0

if __name__ == '__main__':
    main()



# Optimized solution
def main():
    n,k = list(map(int,input().split()))
    a = list(map(int,input().split()))
    # k<=10**18 < 2**60
    r = 60
    import math
    r = math.ceil(math.log2(k))
    idx = 0
    a = [a[i]-1 for i in range(n)]
    for i in range(r):
        if (k>>i)&1 == 1:
            idx = a[idx]
        a = [a[a[j]] for j in range(n)]
    print((idx+1))    

if __name__ == '__main__':
    main()
# Original solution
# -*- coding: utf-8 -*-
# モジュールのインポート
import math

# 標準入力の取得
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

# 求解処理
to = [A]
# ダブリング表の作成
for i in range(math.ceil(math.log2(K))):
    to_i = to[i]
    to_next = []
    for j in range(N):
        to_next.append(to_i[to_i[j]-1])

    to.append(to_next)

result = 1
for i in range(math.ceil(math.log2(K))):
    if ((K >> i) & 1):
        result = to[i][result-1]


print(result)


# Optimized solution
# -*- coding: utf-8 -*-
# モジュールのインポート
import math


def get_input() -> tuple:
    """
    標準入力の取得

    Returns:\n
        tuple: 標準入力
    """
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))

    return N, K, A


def main(N: int, K: int, A: list) -> None:
    """
    求解処理.

    Args:
        N (int): 町の数
        K (int): テレポーターの使用回数
        A (list): テレポーターの転送先
    """
    # タブリング表の作成
    to = [A]
    for i in range(math.ceil(math.log2(K))):
        to_i = to[i]
        to_next = []
        for j in range(N):
            to_next.append(to_i[to_i[j]-1])

        to.append(to_next)

    result = 1
    for i in range(math.ceil(math.log2(K))):
        if ((K >> i) & 1):
            result = to[i][result-1]

    print(result)


if __name__ == "__main__":
    N, K, A = get_input()
    main(N, K, A)
# Original solution
# -*- coding: utf-8 -*-
# モジュールのインポート
import math


def get_input() -> tuple:
    """
    標準入力の取得

    Returns:\n
        tuple: 標準入力
    """
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))

    return N, K, A


def main(N: int, K: int, A: list) -> None:
    """
    求解処理.

    Args:\n
        N (int): 町の数
        K (int): テレポーターの使用回数
        A (list): テレポーターの転送先
    """
    # タブリング表の作成
    bit_num = math.ceil(math.log2(K))
    to = [A]
    for i in range(bit_num):
        to_i = to[i]
        to_next = []
        for j in range(N):
            to_next.append(to_i[to_i[j]-1])

        to.append(to_next)

    # 到着する街の計算
    result = 1
    for i in range(bit_num):
        if ((K >> i) & 1):
            result = to[i][result-1]

    # 結果出力
    print(result)


if __name__ == "__main__":
    N, K, A = get_input()
    main(N, K, A)


# Optimized solution
# -*- coding: utf-8 -*-
# モジュールのインポート
import math


def get_input() -> tuple:
    """
    標準入力の取得

    Returns:\n
        tuple: 標準入力
    """
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))

    return N, K, A


def main(N: int, K: int, A: list) -> None:
    """
    求解処理.

    Args:\n
        N (int): 町の数
        K (int): テレポーターの使用回数
        A (list): テレポーターの転送先
    """
    # タブリング表の作成
    to = [A]
    for i in range(math.ceil(math.log2(K))):
        to_i = to[i]
        to_next = []
        for j in range(N):
            to_next.append(to_i[to_i[j]-1])

        to.append(to_next)

    # 到着する町の計算
    result = 1
    for i in range(math.ceil(math.log2(K))):
        if ((K >> i) & 1):
            result = to[i][result-1]

    # 結果出力
    print(result)


if __name__ == "__main__":
    N, K, A = get_input()
    main(N, K, A)
# Original solution
import sys
from io import StringIO
import unittest

sys.setrecursionlimit(10**5*2)

def search(u, n, g, disc, k):
    if disc[u] != None:
        circles = n - disc[u]
        v = (k - disc[u]) % circles
        ans = u
        if n > k:
            ans = 0
            for _ in range(k):
                ans = g[ans]
            return ans + 1
        for i in range(v):
            ans = g[ans]
        return ans + 1
    disc[u] = n
    return search(g[u], n+1, g, disc, k)

def resolve():
    n, k = list(map(int, input().split()))
    g = list([int(x) - 1 for x in input().split()])
    disc = [None]*n
    v = search(0, 0, g, disc, k)
    print(v)
resolve()

# Optimized solution
def resolve():
    n, k = list(map(int, input().split()))
    g = list(map(int, input().split()))
    disc = [-1]*(n+1)
    s = []
    v = 1
    c = 0
    while disc[v] == -1:
        disc[v] = len(s)
        s.append(v)
        v = g[v-1]
        c = len(s) - disc[v]
    l = len(s)
    if l > k:
        print((s[k]))
    else:
        r = (k - disc[v]) % c
        for i in range(r):
            v = g[v-1]
        print(v)
resolve()
# Original solution
import sys
from io import StringIO
import unittest

sys.setrecursionlimit(10**6)
def search(n, step, rec, seq, k):
	if rec[n] != -1:
		c = step - rec[n]
		sim = (k - rec[n]) % c
		v = n
		for i in range(sim):
			v = seq[v-1]
		print(v)
		return
	rec[n] = step
	if step == k:
		print(n)
		return
	search(seq[n-1], step + 1, rec, seq, k)
def resolve():
	n, k = list(map(int, input().split()))
	seq = list(map(int, input().split()))
	rec = [-1]*(n+1)
	search(1, 0, rec, seq, k)

resolve()

# Optimized solution
def resolve():
	n, k = list(map(int, input().split()))
	a = list(map(int, input().split()))
	rec = [True]*(n+2)
	now = 0
	stp = [1]

	while rec[now]:
		rec[now] = False
		now = a[now] - 1
		stp.append(now+1)
	before_loop = stp.index(now+1)
	loop = stp[before_loop:-1]
	if k <= (before_loop + 1):
		print((stp[k]))
	else:
		r = (k - before_loop) % len(loop)
		print((loop[r]))
resolve()
# Original solution
def resolve():
	n, k = list(map(int, input().split()))
	a = list(map(int, input().split()))
	rec = [True]*(n+2)
	now = 0
	stp = [1]

	while rec[now]:
		rec[now] = False
		now = a[now] - 1
		stp.append(now+1)
	before_loop = stp.index(now+1)
	loop = stp[before_loop:-1]
	if k <= (before_loop + 1):
		print((stp[k]))
	else:
		r = (k - before_loop) % len(loop)
		print((loop[r]))
resolve()

# Optimized solution
def resolve():
	n, k = list(map(int, input().split()))
	a = list(map(int, input().split()))
	rec = [True]*(n+2)
	now = 0
	stp = [1]

	while rec[now]:
		rec[now] = False
		now = a[now] - 1
		stp.append(now+1)
	before_loop = stp.index(now+1)
	loop = stp[before_loop:-1]
	if k <= (before_loop):
		print((stp[k]))
	else:
		r = (k - before_loop) % len(loop)
		print((loop[r]))
resolve()
# Original solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = {i:[] for i in range(n)}
ans = 0
for i in range(k):
    b[a[ans]-1].append(ans)
    if len(b[a[ans]-1]) == 2:
        c = [ans]
        ans = a[ans]-1
        while ans != c[0]:
            c.append(ans)
            ans = a[ans]-1
        ans = c[(k-i) % len(c)]
        break
    ans = a[ans]-1
print((ans+1))

# Optimized solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
s = [-1]*n
c = []
ans = 0
for i in range(k):
    s[ans] = i
    c.append(ans)
    ans = a[ans] - 1
    if s[ans] >= 0:
        t = i - s[ans] + 1
        ans = c[(k-i-1) % t + s[ans]]
        break
print((ans+1))
# Original solution
n, k = list(map(int, input().split()))
a = [int(_) for _ in input().split()]
a = [0] + a

rep_check = 0
now = 1
place = []

if now == a[1]:
    print((1))
    exit()

while now not in place:
    place.append(now)
    now = a[now]
    rep_check += 1
    if rep_check == k:
        print(now)
        exit()

rep_start = 0
for i in enumerate(place):
    if i[1] == now:
        rep_start = i[0]
        break

k -= rep_start
rep_cnt = len(place) - rep_start

place = place[rep_start:]
k %= rep_cnt
print((place[k]))

# Optimized solution
n, k = list(map(int, input().split()))
a = [int(_) for _ in input().split()]
a = [0] + a

rep_check = 0
now = 1
place = dict()
place2 = []

if now == a[1]:
    print((1))
    exit()

while now not in place:
    place[now] = 1
    place2.append(now)
    now = a[now]
    rep_check += 1
    if rep_check == k:
        print(now)
        exit()

rep_start = 0
for i in enumerate(place):
    if i[1] == now:
        rep_start = i[0]
        break

k -= rep_start
rep_cnt = len(place) - rep_start

place2 = place2[rep_start:]
k %= rep_cnt
print((place2[k]))
# Original solution
import sys
sys.setrecursionlimit(10**7) #再帰回数の上限を変更
from collections import deque

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

L=[]
chk=[0 for _ in range(N)]
tmp=0
while(1):
    if chk[tmp]==1:
        break
    chk[tmp]=1
    L.append(tmp)
    tmp=A[tmp]-1

loop_s=A[L[-1]-1]-1
loop_g=L[-1]-1
one_to_s=0
tmp=0
while(1):
    if tmp==loop_s:
        break
    tmp=A[tmp]-1
    one_to_s+=1

loop_l=0
tmp=loop_s
Loop=[loop_s]
while(1):
    if tmp==loop_g:
        break
    tmp=A[tmp]-1
    Loop.append(tmp)
    loop_l+=1
loop_l+=1

if K<=one_to_s:
    tmp=0
    cnt=0
    while(1):
        if cnt==K:
            break
        tmp=A[tmp]-1
        cnt+=1
    ans=tmp + 1
else:
    K-=one_to_s
    num=K%loop_l
    ans=Loop[num]+1

print(ans)

# Optimized solution
import sys
sys.setrecursionlimit(10**7) #再帰回数の上限を変更
from collections import deque

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

L=[]
chk=[0 for _ in range(N)]
tmp=0
while(1):
    if chk[tmp]==1:
        break
    chk[tmp]=1
    L.append(tmp)
    tmp=A[tmp]-1
#print(L)
loop_s=A[L[-1]]-1
loop_g=L[-1]
one_to_s=0
tmp=0
#print(loop_s)
#print(loop_g)

while(1):
    if tmp==loop_s:
        break
    tmp=A[tmp]-1
    #print(tmp)
    one_to_s+=1

loop_l=0
tmp=loop_s
Loop=[loop_s]
while(1):
    if tmp==loop_g:
        break
    tmp=A[tmp]-1
    Loop.append(tmp)
    loop_l+=1
loop_l+=1
#print(one_to_s)
if K<=one_to_s:
    tmp=0
    #print("ON")
    cnt=0
    while(1):
        if cnt==K:
            break
        tmp=A[tmp]-1
        cnt+=1
    ans=tmp + 1
else:
    K-=one_to_s
    num=K%loop_l
    ans=Loop[num]+1

print(ans)
# Original solution
N, K = [int(x) for x in input().split()]
A = [int(x)-1 for x in input().split()]
past = 0
history = [0]
while True:
    na = A[past]
    if na in history: 
        break
    history.append(na)
    past = na
s = history.index(na)
e = len(history)
loop = e - s
if K <= s: # ループ前終了
    print((history[K]+1))
else:
    k = K - s
    print((history[s+(k%loop)]+1))


# Optimized solution
N, K = [int(x) for x in input().split()]
A = [int(x)-1 for x in input().split()]
past = 0
history = [0]
s = set()
while True:
    na = A[past]
    if na in s: 
        break
    s.add(na)
    history.append(na)
    past = na
s = history.index(na)
e = len(history)
loop = e - s
if K <= s: # ループ前終了
    print((history[K]+1))
else:
    k = K - s
    print((history[s+(k%loop)]+1))
# Original solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))

ans=0
now=1
i=1
result=[1]
while i<=k:
    now=a[now-1]
    if now in result:
        x=result.index(now)
        length=i-x
        ans=result[(k-x)%length+x]
        break
    result.append(now)
    i+=1
    
if ans!=0:
    print(ans)
else:
    print((result[-1]))

# Optimized solution
n,k=list(map(int,input().split()))
a=list([int(x)-1 for x in input().split()])

ans=-1
l=[-1]*n
now=0
i=1
result=[0]
while i<=k:
    now=a[now]
    if l[now]!=-1:
        
        length=i-l[now]
        d=(k-i)%length+l[now]
        ans=l.index(d)
        break
    l[now]=i
    i+=1
    
if ans!=-1:
    print((ans+1))
else:
    print((now+1))
# Original solution
n, k = list(map(int,input().split()))

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

tel_list = []

next_city = 0

city_have_been_to = set([0])

way = [0]

while k > 0:
    k -= 1
    temp = city_have_been_to.copy()

    next_city = a[next_city] - 1
    city_have_been_to.add(next_city)
    way.append(next_city)

    if temp == city_have_been_to:
        break

if k == 0:
    print((way[-1] + 1))
else:
    temp_index = way.index(next_city)
    print((way[temp_index:-1][k % len(way[temp_index:-1])] + 1))


# Optimized solution
n, k = list(map(int,input().split()))

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

tel_list = []

next_city = 0

city_have_been_to = set([0])

way = [0]

while k > 0:
    k -= 1
    temp = len(city_have_been_to)

    next_city = a[next_city] - 1
    city_have_been_to.add(next_city)
    way.append(next_city)

    if temp == len(city_have_been_to):
        break

if k == 0:
    print((way[-1] + 1))
else:
    temp_index = way.index(next_city)
    print((way[temp_index:-1][k % len(way[temp_index:-1])] + 1))
# Original solution
#!/usr/bin/env python3
import sys


input = sys.stdin.readline
def IS(cb): return cb(input().strip())
def IL(cb): return [cb(s) for s in input().strip().split()]
def IR(cb, rows): return [IS(cb) for _ in range(rows)]
def ILL(cb, rows): return [IL(cb) for _ in range(rows)]


def solve():
    N, K = IL(int)
    A = IL(lambda x: int(x) - 1)
    memo = [0]
    i = 0
    while(not A[i] in memo):
        memo.append(A[i])
        idx = i
        i = A[i]
    if K < len(memo):
        print((memo[K] + 1))
    else:
        loop = memo[memo.index(A[i]):]
        print((loop[(K - (len(memo) - len(loop))) % len(loop)] + 1))


solve()


# Optimized solution
#!/usr/bin/env python3
import sys


input = sys.stdin.readline
def IS(cb): return cb(input().strip())
def IL(cb): return [cb(s) for s in input().strip().split()]
def IR(cb, rows): return [IS(cb) for _ in range(rows)]
def ILL(cb, rows): return [IL(cb) for _ in range(rows)]


def solve():
    N, K = IL(int)
    A = IL(lambda x: int(x) - 1)
    memo = [0]
    visited = [False] * N
    i = 0
    while(not visited[A[i]]):
        memo.append(A[i])
        visited[A[i]] = True
        i = A[i]
    if K < len(memo):
        print((memo[K] + 1))
    else:
        loop = memo[memo.index(A[i]):]
        print((loop[(K - (len(memo) - len(loop))) % len(loop)] + 1))


solve()
# Original solution
cin = open(0).read().strip().split('\n')

n,k = list(map(int, cin[0].split(' ')))
a = list(map(int, cin[1].split(' ')))

place = 1
history = []
rule = []
while len(rule) == 0:
    history.append(place)
    place = a[place-1]
    for i in range(1,len(history)//2+1):
        aset = set(history[len(history)-i*2:len(history)-i])
        bset = set(history[len(history)-i:])
        # print(i, history, aset, bset)
        if aset == bset:
            rule = history[len(history)-i:]
    if len(history) == (k+1):
        break

if len(rule) == 0:
    print((history[k]))
else:
    pre = len(history)-len(rule)*2
    mod = (k-(pre-1)) % len(rule)
    print((rule[mod-1]))

# Optimized solution
cin = open(0).read().strip().split('\n')

n,k = list(map(int, cin[0].split(' ')))
a = list(map(int, cin[1].split(' ')))

history = [1]
for i in range(n):
    history.append(a[history[i]-1])

if n >= k:
    print((history[k]))
else:
    times = [-1] * n
    for i in range(n):
        if times[history[i]-1] == -1:
            times[history[i]-1] = i
        else:
            cycle = i-times[history[i]-1]
            pre = times[history[i]-1]
            mod = (k-pre)%cycle
            print((history[pre+mod]))
            break
# Original solution
N, K = list(map(int, input().split()))
A = list([int(x)-1 for x in input().split()])

town = 0
visited = [town]
loop = None
while K > 0:
    K -= 1
    town = A[town]
    if town in visited:
        loop = visited[visited.index(town):]
        break
    visited.append(town)

if loop:
    K %= len(loop)
    print((loop[K]+1))
else:
    print((town+1))


# Optimized solution
N, K = list(map(int, input().split()))
A = list([int(x)-1 for x in input().split()])

town = 0
visited = [town]
index = 0
visited_index = {town: index}
loop = None
while K > 0:
    K -= 1
    town = A[town]
    if town in visited_index:
        loop = visited[visited_index[town]:]
        break
    visited.append(town)
    index += 1
    visited_index[town] = index

if loop:
    K %= len(loop)
    print((loop[K]+1))
else:
    print((town+1))
# Original solution
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

loop_checker = [1]
before_loop_len = 0
loop_len = 0

now = 0
while True:
    if A[now] in loop_checker:
        before_loop_len = loop_checker.index(A[now])
        loop_slice = loop_checker[before_loop_len:]
        loop_len = len(loop_slice)
        break
    else:
        loop_checker.append(A[now])
        now = A[now] - 1
if K <= before_loop_len:
    print((loop_checker[K]))
else:
    checkK_prep = (K - before_loop_len)
    checkK = checkK_prep % loop_len
    print((loop_slice[checkK]))


# Optimized solution
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

flag = [False] * N
counted = [1]

nextT = A[0]
flag[0] = True
for x in range(N):
    if flag[nextT - 1] is False:
        counted.append(nextT)
        flag[nextT - 1] = True
        nextT = A[nextT - 1]
    else:
        break
before_loop_len = counted.index(nextT)
loop_len = x - before_loop_len + 1

if K <= before_loop_len:
    print((counted[K]))
else:
    checkK_prep = K - before_loop_len
    checkK = checkK_prep % loop_len
    print((counted[before_loop_len + checkK]))
# Original solution
n,k = list(map(int, input().split()))
A = list(map(int, input().split()))
now = 1
f = 0
past = [0]*n
if n>k:
	for i in range(k):
		now = A[now-1]
	print(now)
	exit()
c = 0
for i in range(n):
	past[c] = now
	now = A[now-1]
	c+=1
	if now in past:
		f = 1
		break


loop_end = c
loop_start = past.index(now)
#print(past, now, loop_end, loop_start)
res = (k-loop_start)%(loop_end-loop_start)
now = past[loop_start]
for i in range(res):
	now = A[now-1]
print(now)


# Optimized solution
n,k = list(map(int, input().split()))
b = list(map(int, input().split()))
a=0
for i in range(n):
    b[i] -= 1
if k<n:
    for i in range(k):
        a = b[a]
    print((a+1))
    exit()
c = [-1]*n
cc = 0
se = set()
while a not in se:
    c[a] = cc
    cc += 1
    se.add(a)
    a = b[a]
for i in range((k-c[a])%(cc-c[a])):
    a = b[a]
print((a+1))
# Original solution
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = list(map(int, input().split()))
    A = list(map(int, input().split()))

    tree = [[] for _ in range(n)]
    for idx, a in enumerate(A):
        tree[a - 1].append(idx)
        tree[idx].append(a - 1)

    start = []
    depth = [0 for _ in range(n)]

    def dfs(v, p, d):
        depth[v] = d
        for u in tree[v]:
            if u == p:
                continue

            if visited[u]:
                start.append([u + 1, depth[u]])
                return
            else:
                visited[u] = True
                dfs(u, v, d + 1)
        return

    visited = [False for _ in range(n)]
    visited[0] = True
    dfs(0, -1, 0)
    
    for i in range(n):
        if i + 1 not in set(A):
            depth[i] = 0

    if k <= start[0][1]:
        print((depth.index(k) + 1))
    elif len(start) == 1:
        print((start[0][0]))
    else:
        cnt = (k - start[0][1]) % (start[-1][1] - start[0][1] + 1)
        res = start[0][1] + cnt
        print((depth.index(res) + 1))


if __name__ == '__main__':
    resolve()


# Optimized solution
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = list(map(int, input().split()))
    A = list(map(int, input().split()))

    tree = [[] for _ in range(n)]
    for idx, a in enumerate(A):
        tree[idx].append(a - 1)

    start = []
    depth = [0 for _ in range(n)]

    def dfs(v, p, d, cnt):
        depth[v] = d
        for u in tree[v]:
            if visited[u]:
                start.append([u + 1, depth[u], cnt + 1])
                return
            else:
                visited[u] = True
                dfs(u, v, d + 1, cnt + 1)
        return

    visited = [False for _ in range(n)]
    visited[0] = True
    dfs(0, -1, 0, 0)

    if k <= start[0][1]:
        print((depth.index(k) + 1))
    else:
        cnt = (k - start[0][1]) % (start[0][2] - start[0][1])
        res = start[0][1] + cnt
        print((depth.index(res) + 1))


if __name__ == '__main__':
    resolve()
# Original solution
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = list(map(int, input().split()))
    A = list(map(int, input().split()))

    tree = [[] for _ in range(n)]
    for idx, a in enumerate(A):
        tree[idx].append(a - 1)

    start = []
    depth = [0 for _ in range(n)]

    def dfs(v, p, d, cnt):
        depth[v] = d
        for u in tree[v]:
            if visited[u]:
                start.append([u + 1, depth[u], cnt + 1])
                return
            else:
                visited[u] = True
                dfs(u, v, d + 1, cnt + 1)
        return

    visited = [False for _ in range(n)]
    visited[0] = True
    dfs(0, -1, 0, 0)

    if k <= start[0][1]:
        print((depth.index(k) + 1))
    else:
        cnt = (k - start[0][1]) % (start[0][2] - start[0][1])
        res = start[0][1] + cnt
        print((depth.index(res) + 1))


if __name__ == '__main__':
    resolve()


# Optimized solution
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = list(map(int, input().split()))
    A = list(map(int, input().split()))

    tree = [[] for _ in range(n)]
    for idx, a in enumerate(A):
        tree[idx].append(a - 1)

    def dfs(pre, dist):
        depth[pre] = dist
        for to in tree[pre]:
            if depth[to] != f_inf:
                cycle.append([depth[to], depth[pre] + 1])
                return
            else:
                dfs(to, dist + 1)

    cycle = []
    depth = [f_inf for _ in range(n)]
    dfs(0, 0)

    cycle_start = cycle[0][0]
    cycle_end = cycle[0][1]
    if k <= cycle_start:
        print((depth.index(k) + 1))
    else:
        remainder = (k - cycle_start) % (cycle_end - cycle_start)
        res = cycle_start + remainder
        print((depth.index(res) + 1))


if __name__ == '__main__':
    resolve()
# Original solution
def main():
    n, k = list(map(int, input().split())) 
    al = list(map(int, input().split())) 
    city_l = [1]
    city_s = set([1])
    loop_city_l = []
    curr_city = 1
    cnt = 0
    for i in range(1,k+1):
        next_c = al[curr_city-1]
        if next_c in city_s:
            cnt = i
            first_city = city_l.index(next_c)
            loop_city_l = city_l[first_city:]
            break
        else:
            city_s.add(next_c)
            city_l.append(next_c)
        curr_city = next_c
    else:
        print(curr_city)
        return

    # print(loop_city_l)
    remained = k-cnt
    last_pos = remained%len(loop_city_l)
    print((loop_city_l[last_pos]))

if __name__ == "__main__":
    main()

# Optimized solution
n,k = list(map(int, input().split()))
al = list(map(int, input().split()))   

cnt = 0
cl = [-1]*(n+1)
cll = [1]
curr = 1
for i in range(min(n,k)):
    # print(curr)
    next_c = al[curr-1]
    if cl[next_c] != -1:
        # loop_start = (i+1)
        loop_start = cl[next_c]
        rem = k - (i+1)
        break
    else:
        cl[next_c] = i + 1
        cll.append(next_c)
    curr = next_c

else:
    print(curr)
    exit()

# print(rem)
loop_c = cll[loop_start:]
# print(loop_c)
last_ind = rem%len(loop_c)
ans = loop_c[last_ind]
print(ans)