CodeEff/ECCO huggingface dataset:
# Original solution
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
# 扱いやすいので1-indexedにする
A = [0] + A
# dp[2^k][i] はiから2^k回移動したときの移動先を表す
dp = {1: A}
for k in range(1, 100):
    dp[2 ** k] = [0]  # 先頭はダミー（1-indexedにするため）
    for i in range(1, N + 1):
        # jはiから2^{i - 1}回移動した先
        j = dp[2 ** (k - 1)][i]
        # iから2^k回移動した先 = jから2^{i - 1}回移動した先
        dp[2 ** k].append(dp[2 ** (k - 1)][j])
ans = 1
for k in range(100):
    if K & 2 ** k > 0:
        ans = dp[2 ** k][ans]
        K -= 2 ** k
print(ans)


# Optimized solution
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
# 扱いやすいので1-indexedにする
A = [0] + A
# dp[k][i] はiから2^k回移動したときの移動先を表す
dp = [A]
for k in range(1, 100):
    dp.append([0])  # 先頭はダミー（1-indexedにするため）
    for i in range(1, N + 1):
        # jはiから2^{i - 1}回移動した先
        j = dp[k - 1][i]
        # iから2^k回移動した先 = jから2^{i - 1}回移動した先
        dp[k].append(dp[k - 1][j])
ans = 1
for k in range(100):
    if K & 2 ** k > 0:
        ans = dp[k][ans]
        K -= 2 ** k
print(ans)
# Original solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

ans =1
now = [1]
ed = set([1])

for i in range(n+1):
    if a[now[i]-1] in ed:
        break
    now.append(a[now[i]-1])
    ed = set(now)
    
t = len(now)-now.index(a[now[i]-1])
f = now.index(a[now[i]-1])


if k >= f + t:
    p = (k-f)%t
    k = f + p
    

print((now[k]))

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

visited = [None]*n
cur = 0
for i in range(k):
    if visited[cur] is None:
        visited[cur] = i
        cur = a[cur]-1
    else:
        break
else:
    print((cur+1))
    sys.exit()

k -= i
k %= i-visited[cur]

for i in range(k):
    cur = a[cur]-1

print((cur+1))
# Original solution
N, K = list(map(int, input().split()))
T_list = list(map(int, input().split()))
T_list = [T_list[i]-1 for i in range(N)]
count_list = []

for s_point in range(N):
  point = s_point
  count = 0
  i = 0
  while i < N:
    point = T_list[point]
    if point == s_point:
      count = i+1
      break
    i += 1
  count_list.append(count)

point = 0
while count_list[point] == 0:
  point = T_list[point]
  count += 1

L = (K-count) % count_list[point]

i = 0
while i < L:
  point = T_list[point]
  i += 1

print((point+1))

# Optimized solution
N, K = list(map(int, input().split()))
T_list = list(map(int, input().split()))
T_list = [T_list[i]-1 for i in range(N)]
point = 0
syuuki = 0

if K < N:
  i = 0
  while i < K:
    point = T_list[point]
    i += 1  

else:
  i = 0
  while i < N:
    point = T_list[point]
    if point == 0:
      syuuki = i+1
      break
    i += 1
  second_point = point
  
  if syuuki != 0:
    L = K % syuuki

  else:
    j = 0
    while j < N:
      point = T_list[point]
      if point == second_point:
        syuuki = j+1
        break
      j += 1
    L = (K-i) % syuuki

  k = 0
  while k < L:
    point = T_list[point]
    k += 1
  
print((point+1))
# Original solution
N,K=list(map(int,input().split()))
t=list(map(int,input().split()))
g=[1]
h={1}
for i in range(N-1):
  x=t[g[i]-1]
  g.append(x)
  if x in h:
    break
  h.add(x)
if K>len(g):
  a=g.index(g[-1])
  b=len(g)-a-1
  K=(K-a)%b+a
print((g[K]))

# Optimized solution
N,K=list(map(int,input().split()))
t=list(map(int,input().split()))
g=[1]
for i in range(N-1):
  g.append(t[g[i]-1])
if K>len(g):
  a=g.index(g[-1])
  b=len(g)-a-1
  K=(K-a)%b+a
print((g[K]))
# Original solution
n,k=list(map(int,input().split()))

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

visited=[0]*n
if k<=n:
    now=0
    nex=0
    loop_start=0
    target=0

    for i in range(n):
        now=nex
        visited[now]+=1
        nex=a[now]-1
        if i+1==k:
            print((nex+1))
            exit()
now=0
nex=0
loop_start=0
target=0
lis=[]
flag=False

for i in range(2*n):
    now=nex
    visited[now]+=1
    nex=a[now]-1
    if flag:
        if now+1 not in lis:
           lis.append(now+1)
           continue
        else:
            break
    if visited[now]==2:
        loop_start=i
        target=now+1
        lis.append(now+1)
        flag=True


dif=len(lis)
first=loop_start-dif
k-=first
print((lis[k%dif]))






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

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

visited=[0]*n
if k<=n:
    now=0
    nex=0
    loop_start=0
    target=0

    for i in range(n):
        now=nex
        visited[now]+=1
        nex=a[now]-1
        if i+1==k:
            print((nex+1))
            exit()
now=0
nex=0
loop_start=0
target=0
lis=[]
s=set()
flag=False

for i in range(2*n):
    now=nex
    visited[now]+=1
    nex=a[now]-1
    if flag:
        if now+1 not in s:
           lis.append(now+1)
           s.add(now+1)
           continue
        else:
            break
    if visited[now]==2:
        loop_start=i
        target=now+1
        lis.append(now+1)
        s.add(now+1)
        flag=True


dif=len(lis)
first=loop_start-dif
k-=first
print((lis[k%dif]))
# Original solution
N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
B=[0]*(N+1)
B[0]=1;c=0;d=0
if B[0] in A:c=B[0]
for i in range(1,N+1):
  B[i]=A[B[i-1]-1]
#  if c==B[i]:break
#  if A.count(B[i])>1 and d==0:c=B[i];d+=1

d=B.index(B[-1])+1
#print(B.index(B[N-1]))

#print(B,d)
#print(d+1+(K-d-1)%(N-d-1))
#print(B[K],K,d,(K+1-d),(N+1-d))
if K<=N:print((B[K]));exit()
if (K+1-d)%(N+1-d)==0:print((B[-1]))
else:print((B[d-1+(K+1-d)%(N+1-d)]))
#print(B[d+1+(K-d-1)%(N-d-1)]-1)

# Optimized solution
N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
B=[0]*(N+1)
B[0]=1;c=0;d=0
if B[0] in A:c=B[0]
for i in range(1,N+1):
  B[i]=A[B[i-1]-1]

d=B.index(B[-1])+1
if K<=N:print((B[K]));exit()
#if (K+1-d)%(N+1-d)==0:print(B[-1])
print((B[d-1+(K+1-d)%(N+1-d)]))
# Original solution
import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = MAP()
A = [a-1 for a in LIST()]

MAX = 64
nxt = list2d(MAX, N, -1)
for i, a in enumerate(A):
    nxt[0][i] = a

for k in range(1, MAX):
    for i in range(N):
        nxt[k][i] = nxt[k-1][nxt[k-1][i]]

ans = 0
for k in range(MAX-1, -1, -1):
    if K >> k & 1:
        ans = nxt[k][ans]
print((ans+1))


# Optimized solution
import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def doubling(MAX, A):
    """ ダブリング """

    N = len(A)
    nxt = list2d(MAX, N, -1)
    for i, a in enumerate(A):
        nxt[0][i] = a
    for k in range(1, MAX):
        for i in range(N):
            nxt[k][i] = nxt[k-1][nxt[k-1][i]]
    return nxt

N, K = MAP()
A = [a-1 for a in LIST()]
MAX = 64

nxt = doubling(MAX, A)

ans = 0
for k in range(MAX-1, -1, -1):
    if K >> k & 1:
        ans = nxt[k][ans]
print((ans+1))
# Original solution
N,K = list(map(int,input().split(' ')))
A = list(map(int,input().split(' ')))

# 戻ってくるまでのリスト
R=[]
Now=1
cnt=0
for i in range(N):
    if Now in R:
        Start=R.index(Now)
        End = cnt
        break
    else:
        R.append(Now)
        Now=A[Now-1]
        cnt+=1

if K < End:
    ans=R[K]
else:
    key=Start+(K-Start)%(End-Start)
    ans=R[key]

print(ans)
#print(Start,End)
#print(R)
#Start+K(End-Start)

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

# 戻ってくるまでのリスト
R=[]
Rs=set()
Now=1
cnt=0
for i in range(N):
    if Now in Rs:
        Start=R.index(Now)
        End = cnt
        break
    else:
        R.append(Now)
        Rs.add(Now)
        Now=A[Now-1]
        cnt+=1

if K < End:
    ans=R[K]
else:
    key=Start+(K-Start)%(End-Start)
    ans=R[key]

print(ans)
#print(Start,End)
#print(R)
#Start+K(End-Start)
# Original solution
N,K = list(map(int, input().split()))
List = list(map(int, input().split()))

a=[-1]*N
b=1
for i in range(N):
    if not b in a:
        a[i]=b
        b=List[b-1]
    else:
        c=i
        break
#print(a)
#print(b,c)
num=a.index(b)
#print(T,num)
T=c-num
if K<num:
    print((a[K]))
else:
    print((a[num:][(K-num)%T]))

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

a=[]

for i in range(N):
    a.append(-1)
#print(a)
b=1
for i in range(N):
    if a[b-1]==-1:
        a[b-1]=i+1
        b=List[b-1]
        
    else:
        c=i
        break
#print(a)
#print(b,c)

num=a[b-1]-1
T=c-num
#print(T)
e=1
if K<num:
    for i in range(K):
        e=List[e-1]
    print(e)
else:
    qw=(K-num)%T
    List_new=[]
    for i in range(T):
        List_new.append(b)
        b=List[b-1]
    print((List_new[qw]))
# Original solution
def resolve():
    '''
    code here
    '''
    import collections
    N, K = [int(item) for item in input().split()]
    A_list = [int(item) for item in input().split()]

    bit_k = bin(K)[2:]

    telepo_list = collections.deque([A_list])

    for _ in range(len(bit_k)-1):
        temp_list = telepo_list[-1]
        new_telep = collections.deque([])
        for i in temp_list:
            new_telep.append(temp_list[i-1])

        telepo_list.append(new_telep)

    # print(telepo_list)

    goto = 1
    for i in range(len(bit_k)):
        temp_list = telepo_list.popleft()
        if ((K >> i) & 1): 
            goto = temp_list[goto -1]
            # print(i, goto)

    print(goto)

if __name__ == "__main__":
    resolve()


# Optimized solution
def resolve():
    '''
    code here
    '''
    N, K = [int(item) for item in input().split()]
    A_list = [int(item) for item in input().split()]

    bit_k = bin(K)[2:]

    telepo_list = [A_list]

    for _ in range(len(bit_k)-1):
        temp_list = telepo_list[-1]
        new_telep = [temp_list[i-1] for i in temp_list]
        telepo_list.append(new_telep)

    # print(telepo_list)

    goto = 1
    for i in range(len(bit_k)):
        temp_list = telepo_list[i]
        if ((K >> i) & 1):
            goto = temp_list[goto - 1]
            # print(i, goto)

    print(goto)


if __name__ == "__main__":
    resolve()
# Original solution
#4問目
import sys
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

#すでに移動したことのある場所
already = [False] * N
step = 0
now = 1
count = 0
while(already[now-1] != True):
    already[now - 1] = True
    now = A[now - 1]
    count += 1
    if count == K:
      print(now)
      exit(0)
# count はstartから2度現れる点までの移動数

#ループに入るまで何回ステップがあったか調べる
# loop := ループに含まれる辺の個数
loop = 0
cycle = 0
if(now == 1):
    step = 0
    cycle = count
    loop = K % cycle
else:
    step = 0
    temp = 1
    while(temp != now):
        temp = A[temp - 1]
        step += 1
    cycle = count - step
    loop= (K - step) % cycle

temp = 1
for i in range(step + loop):
    temp = A[temp - 1]
print(temp)


# Optimized solution
#4問目
import sys
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

#すでに移動したことのある場所
already = [False] * N
step = 0
now = 1
count = 0
while(already[now-1] != True):
    already[now - 1] = True
    now = A[now - 1]
    count += 1
    if count == K: # ループに入る前にKになったらそれで終了
      print(now)
      exit(0)
# now := ループの開始地点
# count :=  地点 1 から now までの移動数
# step := ループに入るまでにワープする回数
# cycle := ループに含まれる辺の個数
# loop := (K-step) % cycle 
#        （残りの(K-step)回ループの中で回したときに ループしないで終わる回数）  
loop = 0
cycle = 0
if(now == 1):
    step = 0
    cycle = count
    loop = K % cycle
else:
    step = 0
    temp = 1
    while(temp != now):
        temp = A[temp - 1]
        step += 1
    cycle = count - step
    loop= (K - step) % cycle

temp = 1
for i in range(step + loop):
    temp = A[temp - 1]
print(temp)
# Original solution
import heapq

def binary_search(data, value):
    left = 0            # 探索する範囲の左端を設定
    right = len(data) - 1            # 探索する範囲の右端を設定
    while left <= right:
        mid = (left + right) // 2            # 探索する範囲の中央を計算
        if data[mid] == value:
            # 中央の値と一致した場合は位置を返す
            return mid
        elif data[mid] < value:
            # 中央の値より大きい場合は探索範囲の左を変える
            left = mid + 1
        else:
            # 中央の値より小さい場合は探索範囲の右を変える
            right = mid - 1
    return -1            # 見つからなかった場合

n, k = list(map(int, input().split()))
a = list([int(x) - 1 for x in input().split()])
way = [0]
hq = [0]
heapq.heapify(hq)
posi = 0
cycle = 0
while True:
    if a[posi] in way:
        rest = way.index(a[posi])
        cycle -= (rest - 1)
        break
    way.append(a[posi])
    heapq.heappush(hq, a[posi])
    posi = a[posi]
    cycle += 1
# print(way)
# print(cycle)
if k < len(way):
    print((way[k] + 1))
else:
    print((way[(k-rest) % cycle + rest] + 1))

# Optimized solution
import heapq

def binary_search(data, value):
    left = 0            # 探索する範囲の左端を設定
    right = len(data) - 1            # 探索する範囲の右端を設定
    while left <= right:
        mid = (left + right) // 2            # 探索する範囲の中央を計算
        if data[mid] == value:
            # 中央の値と一致した場合は位置を返す
            return mid
        elif data[mid] < value:
            # 中央の値より大きい場合は探索範囲の左を変える
            left = mid + 1
        else:
            # 中央の値より小さい場合は探索範囲の右を変える
            right = mid - 1
    return -1            # 見つからなかった場合

n, k = list(map(int, input().split()))
a = list([int(x) - 1 for x in input().split()])
way = [0]
hq = [0]
heapq.heapify(hq)
posi = 0
cycle = 0
while True:
    if binary_search(hq, a[posi]) != -1:
        rest = way.index(a[posi])
        cycle -= (rest - 1)
        break
    way.append(a[posi])
    heapq.heappush(hq, a[posi])
    posi = a[posi]
    cycle += 1
# print(way)
# print(cycle)
if k < len(way):
    print((way[k] + 1))
else:
    print((way[(k-rest) % cycle + rest] + 1))
# Original solution
NK = list(map(int, input().split()))
N = NK[0]
K = NK[1]
A = list(map(int, input().split()))

done = [1]
now = 1
for i in range(1,N+1):
    nexttown = A[now-1]
    #print(now, "->", nexttown)
    if nexttown not in done:
        done.append(nexttown)
        now = nexttown
    else:
        break

start = done.index(nexttown)
end = len(done)-1
before = start #ループに入るまでの転移数
loop = done[start:]

if K < before:
    saisyuu = done[K]
else:
    nokori = K-before
    n_loop = int(nokori/len(loop))
    hasuu = nokori%len(loop)
    loopfirst = A[done[-1]-1]
    saisyuu = loop[hasuu]
    #print("1から",before,"回転移して",loopfirst,"に到着した後、大きさ",loop,"のループを",n_loop,"回転移して",loopfirst,"にいます。")
    #print("その後、",hasuu,"回の転移をして",saisyuu,"に到着します。")
print(saisyuu)


# Optimized solution
NK = list(map(int, input().split()))
N = NK[0]
K = NK[1]
A = list(map(int, input().split()))

done = [1]
donedic = {}
donedic[1] = 1
now = 1
for i in range(1,N+1):
    nexttown = A[now-1]
    #print(now, "->", nexttown)
    try:
        a = donedic[nexttown]
    except:
        donedic[nexttown] = 1
        done.append(nexttown)
        now = nexttown
    else:
        break

start = done.index(nexttown)
end = len(done)-1
before = start #ループに入るまでの転移数
loop = done[start:]

if K < before:
    saisyuu = done[K]
else:
    nokori = K-before
    n_loop = int(nokori/len(loop))
    loopfirst = done[start]
    hasuu = nokori%len(loop)
    saisyuu = loop[hasuu]
    #print("1から",before,"回転移して",loopfirst,"に到着します。")
    #print("その後、大きさ",len(loop),"のループを",n_loop,"回転移して",loopfirst,"にいます。")
    #print("その後、",hasuu,"回の転移をして",saisyuu,"に到着します。")
print(saisyuu)
# Original solution
n,k = list(map(int,input().split()))
a = [0] + list(map(int,input().split())) #1_indexed
tele = [1]
appear = []
while True:
    next_number = a[tele[len(tele)-1]]
    if not next_number in appear:
        tele.append(next_number)
        appear.append(next_number)
    else:
        repeat = tele.index(next_number)
        tele_1 = tele[:repeat]
        tele_2 = tele[repeat:]
        break

if k < len(tele_1):
    print((tele_1[k]))
else:
    k_new = (k-len(tele_1)+1) % len(tele_2)
    print((tele_2[k_new-1]))

# Optimized solution
n,k = list(map(int,input().split()))
a = [0] + list(map(int,input().split())) #1_indexed
tele = [1]
used = [False for i in range(n)]
while True:
    next_number = a[tele[len(tele)-1]]
    if not used[next_number-1]:
        tele.append(next_number)
        used[next_number-1] = True
    else:
        repeat = tele.index(next_number)
        tele_1 = tele[:repeat]
        tele_2 = tele[repeat:]
        break

if k < len(tele_1):
    print((tele_1[k]))
else:
    k_new = (k-len(tele_1)+1) % len(tele_2)
    print((tele_2[k_new-1]))
# Original solution
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
d = {}
for num,i in enumerate(a):
  d[num+1] =  i
t = []
i = 1
j = 1
while(i not in t):
  t.append(i)
  i = d[i]
for num,j in enumerate(t):
  if j == i:
    b = num
    break
a = 1
if k < b+1:
  ans = t[k]
else:
  k = (k-b)%(len(t)-b)
  ans = t[b+k]
print(ans)

# Optimized solution
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
d = {}
for num,i in enumerate(a):
  d[num+1] =  i
t = {}
t_ = {}
i = 1
j = 0
while(i not in t):
  t[i] = j
  t_[j] = i
  i = d[i]
  j += 1
a = 1
b = t[i]
if k < b+1:
  ans = t_[k]
else:
  k = (k-b)%(len(list(t.keys()))-b)
  ans = t_[b+k]
print(ans)
# Original solution
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
A = [A[k]-1 for k in range(N)]
now = 0
for k in range(1000):
    if (K>>k)&1 == 1:
        now = A[now]
    A = [A[A[l]] for l in range(N)]
print((now+1))


# Optimized solution
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
A = [A[k]-1 for k in range(N)]
now = 0
for k in range(70):
    if (K>>k)&1 == 1:
        now = A[now]
    A = [A[A[l]] for l in range(N)]
print((now+1))
# Original solution
n, k = list(map(int, input().split()))
a = list([int(x) - 1 for x in input().split()])

c = [None] * 200000
p = 0
st = 0
ed = 0
i = 0
while True:
    c[i] = p
    i += 1
    p = a[p]
    if p in c:
        st = c.index(p)
        ed = i
        break
if k < ed:
    print((c[k] + 1))
else:
    k -= st
    k %= ed - st
    print((c[st + k] + 1))

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

c = [None] * 200000
cc = set()
p = 0
st = 0
ed = 0
i = 0
while True:
    c[i] = p
    cc.add(p)
    i += 1
    p = a[p]
    if p in cc:
        st = c.index(p)
        ed = i
        break
if k < ed:
    print((c[k] + 1))
else:
    k -= st
    k %= ed - st
    print((c[st + k] + 1))
# Original solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

num = 1
li = [1]
flag = [True] * n
flag[0] = False

for i in range(k):
    num = a[num-1]
    if flag[num-1]:
        li.append(num)
        flag[num-1] = False
    else:
        break

d = li.index(num)
ans = (k-d)%(len(li)-d)+d

print((li[ans]))

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

tt = [0] * n
tt[0] = 1
ans_l = [1]
i = 1

for j in range(k):
    i = a[i-1]
    if tt[i-1] == False:
        tt[i-1] = 1
        ans_l.append(i)
    else:
        break

d = ans_l.index(i)
k -= d
ans = k % (len(ans_l)-d)
print((ans_l[ans+d]))
# Original solution
n, k = list(map(int, input().split()))
A = [0] + list(map(int, input().split()))

pos = 1
seen = [1]
while True:
	if A[pos] in seen:
		seen.append(A[pos])
		break
	seen.append(A[pos])
	pos = A[pos]

length = len(seen)

initial, loop = 0, 0
for i in range(length):
	if seen[i] == seen[-1]:
		initial = i
		loop = length - 1 - initial
		break

if k < length:
	print((seen[k]))
	exit()

kk = (k - initial) % loop
print((seen[initial + kk]))

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

order = [-1] * (n + 1)

s = []
v = 1
while order[v] == -1:
	order[v] = len(s)
	s.append(v)
	v = a[v - 1]
c = len(s) - order[v]
l = order[v]

if k < l:
	print((s[k]))
else:
	k -= l
	k %= c
	print((s[l + k]))
# Original solution
import sys

def mink(numk,l):
    x = 1
    for _ in range(k):
        x = a[x-1]
    return x

input = sys.stdin.readline
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))

# kが小さい時
if k <= 10**5:
    print((mink(k,a)))
    sys.exit()

# ans = 0

z = 1
p = [1]
for _ in range(k):
    z = a[z-1]
    if z in p:
        p.append(z)
        break
    p.append(z)

# 周期の来る前の回数
num = p.index(p[-1])

p = p[num:]

# 周期
pn = len(p)-1

k -= num

# 回数/周期のあまり
anum = k%pn
print((p[anum]))


# Optimized solution
import sys

input = sys.stdin.readline
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))

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

if k <= n:
    print((p[k]))
else:
    # 周期の来る前の回数
    num = p.index(p[-1])
    p = p[num:]

    # 周期
    pn = len(p)-1
    k -= num

    # 回数/周期のあまり
    ans = k%pn
    print((p[ans]))
# Original solution
N, K = list(map(int, input().split()))

A = list(map(int, input().split()))
past = [0]*min(K, 2*10**5)
now = 1
step = 0
for i in range(K):
    past[i] = now
    now = A[now-1]
    if now in past:
        step += 1
        break

if step > 0:
    id = past.index(now)
    step = i+1 - id
    p = (K-id) % step
    now = past[id+p]

print(now)

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

A = list(map(int, input().split()))
past = [0]*min(K, 2*10**5)
ps = set()
now = 1
step = 0
for i in range(K):
    past[i] = now
    ps.add(now)
    now = A[now-1]
    if now in ps:
        step += 1
        break

if step > 0:
    id = past.index(now)
    step = i+1 - id
    p = (K-id) % step
    now = past[id+p]

print(now)
# Original solution
from collections import deque

N, K = list(map(int, input().split()))
K -= 1
A = list([int(x) - 1 for x in input().split()])
seen = deque()
now = 0
for i in range(N):
    seen.append(now)
    to = A[now]
    now = to
    if now in seen:
        seen.append(now)
        break
loop_s = seen.index(now)
loop_end = seen.index(now, loop_s + 1)
size = loop_end - loop_s
if K <= loop_s:
    print((seen[K+1] + 1))
else:
    K -= loop_s
    print((seen[loop_s + 1 + K % size] + 1))

# Optimized solution
N, K = list(map(int, input().split()))
K -= 1
A = list([int(x) - 1 for x in input().split()])
seen = [None]*N
seen_set = set()
now = 0
for i in range(N):
    seen[i] = now
    seen_set.add(now)
    to = A[now]
    now = to
    if now in seen_set:
        seen[i+1] = now
        loop_s = seen.index(now)
        loop_end = i + 1
        size = loop_end - loop_s
        break
if K <= loop_s:
    print((seen[K+1] + 1))
else:
    K -= loop_s
    print((seen[loop_s + 1 + K % size] + 1))
# Original solution
N, K = list(map(int, input().split()))
A = list([int(x) - 1 for x in input().split()])

now = 0
seen = []
while True:
    seen.append(now)
    teleporter = A[now]
    A[now] = 'used'
    if teleporter == 'used':
        break
    else:
        now = teleporter  

loop_s = seen.index(seen[-1])
size = len(seen) - loop_s - 1

if K <= loop_s:
    print((seen[K] + 1))
else:
    print((seen[loop_s +(K - loop_s) % size] + 1))

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

now = 0
seen = []
while True:
    seen.append(now)
    teleporter = A[now]
    A[now] = 'used'
    if teleporter == 'used':
        break
    else:
        now = teleporter  

loop_s = seen.index(now)
size = len(seen) - loop_s - 1

if K <= loop_s:
    print((seen[K] + 1))
else:
    print((seen[loop_s +(K - loop_s) % size] + 1))