CodeEff/ECCO huggingface dataset:
# Original solution
from copy import copy
n, k = list(map(int, input().split()))
a = [int(i) - 1 for i in input().split()]
ans = 0
while k:
    if k & 1:
        ans = a[ans]
    c = [a[a[i]] for i in range(n)]
    a = copy(c)
    k >>= 1
print((ans + 1))

# Optimized solution
n, k = list(map(int, input().split()))
a = [int(i) - 1 for i in input().split()]
ans = 0
while k:
    if k & 1:
        ans = a[ans]
    c = [a[a[i]] for i in range(n)]
    a = c[:]
    k >>= 1
print((ans + 1))
# Original solution
n, k = list(map(int,input().split()))
a = [int(i) for i in input().split()]
s = []
ord = [-1] * (n+1)
c, l = 1, 0
v = 1
while (ord[v] == -1):
    ord[v] = len(s)
    s.append(v)
    v = a[v-1]
    
c = len(s) - ord[v]
l = ord[v]
print((s[k] if (k < l) else s[l + (k - l) % c]))

# Optimized solution
n, k = list(map(int, input().split()))
a = [int(i) for i in input().split()]
ord = [-1] * (n + 1)
s = []
v = 1
c, l = 1, 0
while ord[v] == -1:
    ord[v] = len(s)
    s.append(v)
    v = a[v - 1]
c = len(s) - ord[v]
l  = ord[v]
print((s[k] if k < l else s[l + (k - l) % c]))
# Original solution
import sys
n,k=list(map(int,input().split()))
A=[int(i) for i in input().split()]
d=dict()
for i,a in enumerate(A):
    d[i+1] = a
p=1
li = [1]
for i in range(k):
    #print(p,li)
    p = d[p]
    if p in li:
        pidx = li.index(p)
        cyc = i+1-pidx
        print((li[(k-pidx)%cyc+li.index(p)]))
        sys.exit()
    li.append(p)
else:
    print(p)

# Optimized solution
import sys
n,k=list(map(int,input().split()))
A=[int(i) for i in input().split()]
d=dict()
for i,a in enumerate(A):
    d[i+1] = a
p=1
li = set()
li.add(1)
for i in range(k):
    #print(p,li)
    p = d[p]
    #print(p,li)
    if p in li:
        cyc =  i + 1
        q = 1
        st=0
        while q != p:
            q = d[q]
            st+=1
        cyc -=st
        k = (k-st) % cyc + st
        r = 1
        #print(k)
        for _ in range(k):
            #print(r)
            r = d[r]
        print(r)
        sys.exit()
    li.add(p)
else:
    print(p)
# Original solution
from collections import deque

n, k = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))

loop = deque([])
visited = set()
stack = deque([1])
loop_creating = False
last = 1

while True:
  if loop_creating and len(visited) == 0:
    break

  nex = a[last - 1]

  if nex in visited:
    visited.remove(nex)
    loop.append(nex)
    loop_creating = True
  elif nex in stack:
    visited.add(nex)
  else:
    stack.append(nex)

  last = nex

if k <= len(stack):
  print((stack[k]))
elif k > len(stack):
  rem = k - len(stack)
  print((loop[rem % len(loop)]))


# Optimized solution
from collections import deque

n, k = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))

loop = []
visited = set()
s = set([1])
loop_creating = False
last = 1

while True:
  if loop_creating and len(visited) == 0:
    break
  
  if k == len(s) - 1:
    print(last)
    exit(0)

  nex = a[last - 1]

  if nex in visited:
    visited.remove(nex)
    loop.append(nex)
    loop_creating = True
  elif nex in s:
    visited.add(nex)
  else:
    s.add(nex)

  last = nex

rem = k - len(s)
print((loop[rem % len(loop)]))
# Original solution
import sys
from io import StringIO
import unittest


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

    loop_list = []
    now = 0

    if k > n:
        while 1:
            now = a[now] - 1

            if now + 1 in loop_list:
                s_index = loop_list.index(now + 1)
                before_loop = s_index
                loop_list = loop_list[s_index:]
                break
            else:
                loop_list.append(now + 1)

        l = len(loop_list)
        k -= before_loop

        # print(loop_list, l, before_loop)
        print((loop_list[k % l - 1]))
    else:
        for _ in range(k):
            now = a[now] - 1

        print((now + 1))


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4 5
3 2 4 1"""
        output = """4"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """6 727202214173249351
6 5 2 5 3 2"""
        output = """2"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()


# Optimized solution
import sys
from io import StringIO
import unittest


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

    loop_list = []
    loop_set = set()
    now = 0

    if k > n:
        for _ in range(k):
            now = a[now] - 1

            if now + 1 in loop_set:
                s_index = loop_list.index(now + 1)
                before_loop = s_index
                loop_list = loop_list[s_index:]
                break
            else:
                loop_list.append(now + 1)
                loop_set.add(now + 1)

        l = len(loop_list)
        k -= before_loop

        # print(loop_list, l, before_loop)
        print((loop_list[k % l - 1]))
    else:
        for _ in range(k):
            now = a[now] - 1

        print((now + 1))


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4 5
3 2 4 1"""
        output = """4"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """6 727202214173249351
6 5 2 5 3 2"""
        output = """2"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()
# Original solution
import sys
input = sys.stdin.readline

def main():
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    B = [1]
    for i in range(1,K + 1):
        a = A[B[i - 1] - 1]
        if a in B:
            x = B.index(a)
            y = i - x
            z = (K - x) % y
            print((B[x + z]))
            exit()
        B.append(a)
    if len(B) == K + 1:
        print((B[K]))
if __name__ == '__main__':
    main()

# Optimized solution
import sys
input = sys.stdin.readline

def main():
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    B = [1]
    a = 1
    check = [0] * len(A)
    while True:
        a = A[a - 1]
        if check[a - 1] == 1:
            x = B.index(a)
            roop = B[x:]
            print((roop[(K - x) % len(roop)]))
            exit()
        B.append(a)
        check[a - 1] += 1
        if len(B) >= K + 1:
            print((B[K]))
            exit()
if __name__ == '__main__':
    main()
# Original solution
import sys
input = sys.stdin.readline

class Doubling:
    def __init__(self, A, K_max, decrement=True):
        """
        :param A: リスト。i から A[i] へと移動する
        :param K_max: K_max = 2**(k_max) まで参照する可能性がある
        :param decrement: True = A の要素の decrement が必要
        """
        self.k_max = K_max.bit_length()
        self.n = len(A)
        self.decrement = decrement
        self.doubling = [[-1] * self.n for _ in range(self.k_max)]
        for i, a in enumerate(A):
            self.doubling[0][i] = a - self.decrement

        for i in range(1, self.k_max):
            for j in range(self.n):
                if self.doubling[i - 1][j] != -1:
                    self.doubling[i][j] = self.doubling[i - 1][self.doubling[i - 1][j]]

    def apply(self, start, K):
        """
        :param start: スタート地点
        :param K: K回進む
        :return:
        """
        i = start - self.decrement
        for k in range(K.bit_length()):
            m = 1 << k
            if m & K:
                i = self.doubling[k][i]
            if i is None:
                break
        return i + self.decrement


######################################################################################


N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
doubling = Doubling(A, 10**18 + 1, decrement=True)
print((doubling.apply(1, K)))

# Optimized solution
def cycle_getter(N, start):
    """
    :param N: 移動回数
    :param start:　初期条件
    :return front: cycleまでの要素のリスト
            cycle: cycle内の要素のリスト
            end: cycle後の余った部分の要素のリスト
            cnt: cycle回数
    """
    p = start
    front, cycle, end = [], [], []
    cnt = 0
    visit = {p:0}
    L, R = N, -1
    P = [p]
    for i in range(1,N):
        p = lift(p)
        if p in visit:
            """
            (L, R) = (サイクルに入るまでに移動した回数, サイクルの終端に着くまでに移動した回数)
            [6,2,3,4,0,1,2] ⇒ (L, R) = (1, 6)
            """
            L, R = visit[p], i
            period = R-L
            break
        visit[p] = i
        P.append(p)
    front = P[:L]
    if L != N:
        cycle, end = P[L:R], P[L:L+(N-L)%period]
        cnt = (N-L)//period
    return front, cycle, end, cnt

################################################################################


import sys
input = sys.stdin.readline

def lift(x): return A[x-1]

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
front, cycle, end, cnt = cycle_getter(K+1,1)
# print(front, cycle, end, cnt)
print(((front+cycle+end)[-1]))
# Original solution
# Problem D - Teleporter

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

# initialization
visit_list = [0]*N
visit_list[0] = 1
pos = a_nums[0]
visit_count = 1

while visit_list[pos-1]==0 and visit_count<K:
    visit_count += 1
    visit_list[pos-1] = visit_count
    pos = a_nums[pos-1]
visit_count += 1

# calc 1
if visit_count<K:
    tmp_1 = 0
    if pos==1:
        tmp_1 = visit_count - visit_list[pos-1] # 周期
    else:
        tmp_1 = visit_count - visit_list[pos-1]
    tmp_2 = (K-visit_count) % tmp_1 # 何周するのか
    # tmp_2回分ループ処理を行う
    for i in range(tmp_2+1):
        pos = a_nums[pos-1]

# output
print(pos)


# Optimized solution
# Problem D - Teleporter

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

# initialization
visit_list = [0] * N
pos = a_list[0]
visit_list[pos-1] = 1
visit_count = 1
remain = K - 1
is_ok = True
ans = 0

# count
while remain>0 and is_ok:
    pos = a_list[pos-1]
    remain -= 1
    visit_count += 1
    if pos==1 or not visit_list[pos-1]==0:
        tmp_1 = visit_count - visit_list[pos-1]
        tmp_2 = remain % tmp_1
        for t in range(tmp_2):
            pos = a_list[pos-1]
        is_ok = False
        break
    else:
        if not pos==1 and visit_list[pos-1]==0:
            visit_list[pos-1] = visit_count

# output
print(pos)
# Original solution
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
checked = [0]
next_num = a[0]-1
i = 0
while True:
    if i == 0:
        i += 1
    elif next_num in checked:
        loop = checked[checked.index(next_num):]
        break
    checked.append(next_num)
    next_num = a[next_num]-1
dif = len(checked) - len(loop)
if k <= dif:
    print((checked[k]+1))
else:
    k = k-dif
    print((loop[k%len(loop)]+1))

# Optimized solution
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
checked = [0]
next_num = a[0]-1
check = [0]*n
i = 0
while True:
    if i == 0:
        i += 1
    elif check[next_num] == 1:
        loop = checked[checked.index(next_num):]
        break
    checked.append(next_num)
    check[next_num] = 1
    next_num = a[next_num]-1
dif = len(checked) - len(loop)
if k <= dif:
    print((checked[k]+1))
else:
    k = k-dif
    print((loop[k%len(loop)]+1))
# Original solution
n, k, *a = list(map(int, open(0).read().split()))
a = [0] + a
p = 1
while k:
    if k % 2:
        p = a[p]
    a = [a[b] for b in a]
    k //= 2
print(p)

# Optimized solution
n,k,*a=list(map(int,open(0).read().split()));a=[0]+a;p=1
while k:p=k%2and a[p]or p;a=[a[b]for b in a];k//=2
print(p)
# Original solution
data = input().rstrip().split(" ")
tele = input().rstrip().split(" ")
telepo = [int(i) for i in tele]

max = int(data[1])
limit = max
times = 0
pos = 1
experience = [1]
while max > 0:
    pos = telepo[pos - 1]
    max -= 1
    times += 1
    #print(pos)
    if pos in experience:
        length = len(experience) - experience.index(pos)
        max = max % length
        if max == 0:
            break
        while max > 0:
            pos = telepo[pos - 1]
            max -= 1
            times += 1
            #print(pos)
        break
    experience.append(pos)
print(pos)

# Optimized solution
data = input().rstrip().split(" ")
tele = input().rstrip().split(" ")
telepo = [int(i) for i in tele]

max = int(data[1])
limit = max
times = 0
pos = 1
experience = [1]
check = {1}
before = 0
while max > 0:
    pos = telepo[pos - 1]
    max -= 1
    times += 1
    before = len(check)
    check.add(pos)
    if before == len(check):
        length = len(experience) - experience.index(pos)
        max = max % length
        if max == 0:
            break
        while max > 0:
            pos = telepo[pos - 1]
            max -= 1
            times += 1
            #print(pos)
        break
    experience.append(pos)
print(pos)
# Original solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
a = [[i - 1, j - 1] for i, j in enumerate(a)]
g = [[0 for _ in range(n)] for _ in range(n)]
x = [0]

i = 0
j = a[0][1]
while g[i][j] != 1:
    x.append(j)
    g[i][j] = 1
    i = j
    j = a[j][1]

tmp = x.index(x[-1])
if tmp >= k:
    print((x[k] + 1))
else:
    x = x[tmp:-1]
    k = (k - tmp) % len(x)
    print((x[k] + 1))

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

i = 0
j = 0
while g[j] == -1:
    if i == k:
        print((j + 1))
        exit()
    g[j] = i
    i += 1
    j = a[j]

for i in range((k - g[j]) % (i - g[j])):
    j = a[j]
print((j + 1))
# Original solution
n,k = list(map(int,input().split()))
a = [int(i) - 1 for i in input().split()]
r = []
nx = 0
for i in range(n):
    r.append(nx)
    if k < i:
        print((r[k] + 1))
        break
    if a[nx] in r:
        m = r.index(a[nx])
        d = i - m + 1
        k = k - m
        k = k % d
        print((r[k + m] + 1))
        break  
    nx = a[nx]


# Optimized solution
n,k = list(map(int,input().split()))
a = [int(i) - 1 for i in input().split()]
r = []
nx = 0
sr = set([])
for i in range(n):
    r.append(nx)
    sr.add(nx)
    if k < i:
        print((r[k] + 1))
        break
    tnx = a[nx]
    if tnx in sr:
        m = r.index(a[nx])
        d = i - m + 1
        k = k - m
        k = k % d
        print((r[k + m] + 1))
        break  
    nx = tnx
# Original solution
N,K=list(map(int,input().split()))
T=str(bin(K))
S=len(T)-2
A = list(map(int,input().split()))
lst=[[0 for e in range(N+1)] for f in range(S)]
for i in range(1,N+1):
 lst[0][i]=A[i-1]
for j in range(1,S):
 for i in range(1,N+1):
  lst[j][i]=lst[j-1][lst[j-1][i]]
ans=[[e for e in range(N+1)] for f in range(S+1)]
for k in range(S):
 if int(T[k+2])==1:
  for l in range(1,N+1):
   ans[k+1][l]=lst[S-1-k][ans[k][l]]
 else:
  for l in range(1,N+1):
   ans[k+1][l]=ans[k][l]
print((ans[S][1]))


# Optimized solution
N,K=list(map(int,input().split()))
T=str(bin(K))
S=len(T)-2
A = list(map(int,input().split()))
lst=[[0 for e in range(N+1)] for f in range(S)]
for i in range(1,N+1):
 lst[0][i]=A[i-1]
for j in range(1,S):
 for i in range(1,N+1):
  lst[j][i]=lst[j-1][lst[j-1][i]]
ans=1
for k in range(S):
 if int(T[k+2])==1:
  ans=lst[S-1-k][ans]
print(ans)
# Original solution
N, K = list(map(int, input().split()))
A = tuple([int(n) - 1 for n in input().split()])

costs = [-1] * N
loc = 0

if K <= N:
    for i in range(K):
        loc = A[loc]
    print((loc + 1))

else:
    costs[loc] = 0
    for i in range(1, K):
        loc = A[loc]

        if costs[loc] >= 0:
            break

        costs[loc] = i

    offset = costs[loc]
    loop = i - offset

    K -= i
    K %= loop

    for _ in range(K):
        loc = A[loc]
    print((loc + 1))

# Optimized solution
N, K = list(map(int, input().split()))
# A = tuple(map(lambda n: int(n) - 1, input().split()))
A = tuple(map(int, input().split()))

costs = [-1] * N
loc = 1

if K <= N:
    for i in range(K):
        loc = A[loc - 1]
    print(loc)

else:
    costs[loc - 1] = 0
    for i in range(1, K):
        loc = A[loc - 1]

        if costs[loc - 1] >= 0:
            break

        costs[loc - 1] = i

    offset = costs[loc - 1]
    loop = i - offset

    K -= i
    K %= loop

    for _ in range(K):
        loc = A[loc - 1]
    print(loc)
# Original solution
N,K=list(map(int,input().split()))
*A,=list(map(int,input().split()))

point=[0]*N
count_X=0
count_Y=0
i=0
while all(j<3 for j in point):
    if all(j<2 for j in point):
        count_X+=1
    point[i]+=1
    i=A[i]-1
    if any(k>1 for k in point):
        count_Y+=1
count_Y-=1

if K>=count_X:
    Z=count_X+(K-count_X)%count_Y
else:
    Z=K

i=0
ans=1
while i<Z:
    ans=A[ans-1]
    i+=1

print(ans)
    

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

point=[0]*N
count_X=0
count_Y=0
i=0
while point[i]!=3:
    if point[i]<2:
        count_X+=1
    point[i]+=1
    i=A[i]-1
    if point[i]>1:
        count_Y+=1
count_Y-=1

if K>=count_X:
    Z=count_X+(K-count_X)%count_Y
else:
    Z=K

i=0
ans=1
while i<Z:
    ans=A[ans-1]
    i+=1

print(ans)
# Original solution
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

l = [A[0]]
a = A[0] - 1
for _ in range(K):
    if A[a] not in l:
        l.append(A[a])
        a = A[a] - 1
    else:
        a = A[a]
        break
n = len(l)
b = 0
for i in range(n):
    if l[i] == a:
        del l[0:i]
        b = i
        break
T = len(l)
S = (K - b) % T
ans = l[S - 1]

print(ans)


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

l = [A[0]]
s = {A[0]}
a = A[0] - 1
for _ in range(K):
    if A[a] not in s:
        l.append(A[a])
        s.add(A[a])
        a = A[a] - 1
    else:
        a = A[a]
        break
n = len(l)
b = 0
for i in range(n):
    if l[i] == a:
        del l[0:i]
        b = i
        break
T = len(l)
S = (K - b) % T
ans = l[S - 1]

print(ans)
# Original solution
import sys
sys.setrecursionlimit(1000000000)
import math
from math import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations, chain
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(eval(input()))
mis = lambda: list(map(int, input().split()))
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    # 10**6
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     # 10**6
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):   
    return fs[n] * invs[n-k] * invs[k] % modp

#

class UnionFindEx:
    def __init__(self, size):
        #正なら根の番号、負ならグループサイズ
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   #負なら根
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    #サイズ更新
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'


def main():
    N, K = mis()
    A = [a-1 for a in mis()]
    B = [None]*N
    now = 0
    B[now] = 0
    for i in range(1, K+1):
        now = A[now]
        K -= 1
        if B[now] is not None:
            K %= (i - B[now])
            break
        B[now] = i
    for _ in range(K):
        now = A[now]
    print((now+1))

main()



# Optimized solution
'''
・基本的には素直なシミュレーションを行う
・ループ分のシミュレーションを省略することでTLEを回避
・Kは「残りのワープ数」と考える

K回のワープ中にループが含まれる場合、
1. 素直なシミュレーションを行う。このとき、Kを減算していく。
2. ループを検出したら、Kからループ長をできるだけ多く引くことで、計算を省略する。
3. 残りのステップ数、再度シミュレーションを行う
という手順で答えを求められる。
なお、ループが含まれない場合も1のみで答えを求められる。
'''

def main():
    N, K = list(map(int, input().split()))
    A = [a-1 for a in map(int, input().split())]    # 0-indexed に変換
    B = [None]*N    # 何ステップ目に到達したかを記録。Noneなら未到達を表す
    now = 0         # 現在地
    step = 0        # ステップ数
    B[now] = 0      # 開始地点は、0ステップ目に到達済みとする
    while K:
        now = A[now]    # 現在地を更新 
        step += 1
        K -= 1
        if B[now] is not None:  # ループを検出したら...
            K %= (step - B[now])      # Kからループ長さを引けるだけ引く
        else:
            B[now] = step  # 到達したステップ数を記録
    print((now+1))    # 1-indexed に再変換

main()
# Original solution
from collections import deque
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
g = [i for i in range(n)]
for i in range(n):
    g[i] = a[i]-1
dist =[-1]*n
dist[0] = 0
q = deque([0])
hajimari = -1
loopnum = -1
while q:
    v = q.popleft()
    u = g[v]
    if dist[u] == -1:
        dist[u] = dist[v]+1
        q.append(u)
    else:
        hajimari = u
        loopnum = dist[v] + 1 - dist[u]
        break
    if hajimari != -1:
        break
if k <= dist[hajimari]:
    now = 0
    for _ in range(k):
        now = g[now]
    print((now+1))
else:
    now = hajimari
    for _ in range((k-dist[hajimari])%loopnum):
        now = g[now]
    print((now+1))

# Optimized solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
dist = [-1] * n
dist[0] = 0
now = 0
while True:
    next = a[now] - 1
    if dist[next] == -1:
        dist[next] = dist[now] + 1
    else:
        hajimari = next
        loopnum = dist[now] + 1 - dist[hajimari]
        town = 0
        if k <= dist[hajimari]:
            for _ in range(k):
                town = a[town] - 1
        else:
            town = hajimari
            for _ in range((k - dist[hajimari]) % loopnum):
                town = a[town] - 1
        print((town + 1))
        exit()
    now = next
# Original solution
n,k = (int(x) for x in input().split())
a =tuple([int(x) for x in input().split()])
 
l = [1]
now = 1
notloop = 0
dupl = -1

for i in range(k):
  now = a[now - 1]
  
  if now in l:
    dupl = now
    break
  else:
    l.append(now)
    
if(dupl == -1):
  print(now)
else:
  notloop = l.index(dupl)
  length = len(l)-notloop
  print((l[(k-notloop)%length+notloop]))

# Optimized solution
n,k = (int(x) for x in input().split())
a = tuple([int(x) for x in input().split()])
 
c = [0] + [-1]*(n)
l = [1]
now = 1
dupl = -1

for i in range(k):
  now = a[now - 1]
  c[now - 1] += 1
   
  if c[now - 1] == 1:
    dupl = now
    break
  else:
    l.append(now)
    
if dupl == -1:
  print(now)
  exit()
       
notloop = l.index(dupl)
length = len(l)-notloop
print((l[(k-notloop)%length+notloop]))