CodeEff/ECCO huggingface dataset:
# Original solution
line1 = input().split()
n = int(line1[0])
k = int(line1[1])
a = [int(ai) for ai in input().split()]

route = [0]
for j in range(k):
  now = a[route[j]] - 1
  if now in route:
    break
  else:
    route.append(now)

l_start = route.index(now)
print((route[l_start + ((k - l_start) % (len(route) - l_start))] + 1))

# Optimized solution
line1 = input().split()
n = int(line1[0])
k = int(line1[1])
a = [int(ai) for ai in input().split()]

route = [0]
use = {0}
for j in range(k):
  now = a[route[j]] - 1
  if now in use:
    break
  else:
    route.append(now)
    use.add(now)

l_start = route.index(now)
print((route[l_start + ((k - l_start) % (len(route) - l_start))] + 1))
# Original solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
s=[]
s.append(1)
a1=1
a2 = a[a1-1]

while a2 not in s:
    s.append(a2)
    a1 = a2
    a2 = a[a1-1]

loop_num=s.index(a2)
loop=s[loop_num:]
cnt=len(loop)

if k<len(s):
    print((s[k]))
else:
    ans = (k-loop_num)%cnt
    print((loop[ans]))

# Optimized solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
s=[]
s.append(1)
a1=1
a2 = a[a1-1]
l=[0]*n
l[0]=1

while l[a2-1] != 1:
    s.append(a2)
    a1 = a2
    a2 = a[a1-1]
    l[a1-1] = 1

loop_num=s.index(a2)
loop=s[loop_num:]
cnt=len(loop)

if k<len(s):
    print((s[k]))
else:
    ans = (k-loop_num)%cnt
    print((loop[ans]))
# Original solution
def d_teleporter():
    N, K = [int(i) for i in input().split()]
    A = [int(i) - 1 for i in input().split()]  # 0-origin

    from collections import defaultdict
    visit = [0]
    visit_dict = defaultdict(list)
    visit_dict[0] = [0]
    current = 0
    is_found_first = False
    for i in range(N):
        current = A[current]
        if len(visit_dict[current]) == 1 and not is_found_first:
            count_to_loop = visit_dict[current][0]
            loop_length = i - visit_dict[current][0] + 1
            is_found_first = True
        visit.append(current)
        visit_dict[current].append(i + 1)

    if K <= N:
        return visit[K] + 1
    net_moving_time = ((K - count_to_loop) % loop_length) + count_to_loop
    # 上の visit に必要な分だけ付け足してもいいが，実行時間は変わらなかった
    current = 0
    for _ in range(net_moving_time):
        current = A[current]
    return current + 1

print((d_teleporter()))

# Optimized solution
def d_teleporter():
    N, K = [int(i) for i in input().split()]
    A = [int(i) - 1 for i in input().split()]  # 0-origin

    visit_list, visit_dict = list(), dict()
    current_city = 0
    for i in range(min(K, N)):  # N 回移動すればループはわかる
        current_city = A[current_city]
        if current_city in visit_dict:  # ループを検出した
            j = visit_list.index(current_city)  # ここからループ
            current_city = visit_list[j + (K - j - 1) % (i - j)]
            return current_city + 1
        else:
            visit_list.append(current_city)
            visit_dict[current_city] = current_city
    return current_city + 1

print((d_teleporter()))
# Original solution
n, k = list(map(int, input().split()))

a = [int(x)-1 for x in input().split()]

v = []
b = 0

for i in range(n):
    if b not in v:
        v.append(b)
        b = a[b]
    else:
        stop = v.index(b)
        j = i
        break


if k>j:
    r = (k-stop)%(j-stop)
    print((v[stop+r]+1))
else:
    print((v[k]+1))


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

a = [int(x)-1 for x in input().split()]

v = []
vc = [0]*n
b = 0
c = 0

while True:
    if vc[b]>0:
        stop = v.index(b)
        break
    v.append(b)
    vc[b] += 1
    b = a[b]
    c += 1



if k>stop:
    r = (k-stop)%(c-stop)
    print((v[stop+r]+1))
else:
    print((v[k]+1))
# Original solution
nk = input().split()
n , k = int(nk[0]), int(nk[1])
a = [int(i) for i in input().split()]

t = 1
tl = [1]

flg = False
for i in range(len(a)):
    flg = t > a[t-1]
    t  = a[t-1]
    stl = sorted(tl)
    if flg and t in stl:
        break
    else:
        tl.append(t)

tl2 = tl[tl.index(t):len(tl)]
         
# print(tl)
# print(tl2)

ans = 0

if k <= len(tl):
    ans = tl[k]
else:
    k = k - len(tl)
    j = k % len(tl2)
    ans = tl2[j]

print(ans)

# Optimized solution
nk = input().split()
n , k = int(nk[0]), int(nk[1])
a = [int(i) for i in input().split()]

t = 1
tl = [1]
ts = {1}


for i in range(len(a)):
    
    t = a[t-1]
    if t in ts:
        break
    else:
        tl.append(t)
        ts.add(t)

tl2 = tl[tl.index(t):len(tl)]
         
# print(tl)
# print(tl2)

ans = 0

if k < len(tl):
    ans = tl[k]
else:
    k = k - len(tl)
    j = k % len(tl2)
    ans = tl2[j]

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

D = 60 #10**18は60桁なので2の59乗まで見る
MAX_N = 200005 #頂点番号
to = [[0 for _ in range(MAX_N)] for _ in range(D)]

#to[i][j]:jから2^i個先の頂点
for i in range(n):
    to[0][i] = A[i]
    to[0][i] -= 1

#ダブリングの表
for i in range(D-1):
    for j in range(n):
        to[i+1][j] = to[i][to[i][j]]

v = 0
for i in range(D-1, -1, -1):
    l = 1<<i #2^i個先を見る
    if l <= k:
        v = to[i][v]
        k -= l

print((v+1))

# Optimized solution
def main():
  ## IMPORT MODULE
  #import sys

  #sys.setrecursionlimit(100000)
  #input=lambda :sys.stdin.readline().rstrip()

  #f_inf=float("inf")
  #MOD=10**9+7
  
  if 'get_ipython' in globals(): 
    ## SAMPLE INPUT
    n, k = 4, 5
    A = [3, 2, 4, 1]

  else:
    ##INPUT 
    #n = input()
    n, k = list(map(int, input().split()))
    A = list(map(int, input().split()))

  ## SUBMITION CODES HERE
  Town = []
  rec = [-1] * (n+1) # The town we haven't visited yet is -1
  idx = 1

  # Loop to the cycle
  while (rec[idx] == -1):
    rec[idx] = len(Town)
    Town.append(idx)
    idx = A[idx-1]

  # Pre-cycle
  Exce = rec[idx]
  # Intra-cycle
  Cycl = len(Town) - Exce

  if Exce > k:
    print((Town[k]))
  else:
    Warp = (k - Exce) % Cycl
    print((Town[Exce+Warp]))
  
main()
# Original solution
#Doubling O(log k)
def main():
  ## IMPORT MODULE
  #import sys

  #sys.setrecursionlimit(100000)
  #input=lambda :sys.stdin.readline().rstrip()

  #f_inf=float("inf")
  #MOD=10**9+7
  
  if 'get_ipython' in globals(): 
    ## SAMPLE INPUT
    n, k = 4, 5
    A = [3, 2, 4, 1]

  else:
    ##INPUT 
    #n = input()
    n, k = list(map(int, input().split()))
    A = list(map(int, input().split()))

  ## SUBMITION CODES HERE
  D = 60 # 10^18 is 60 digits, so we'll see to the 59th power of 2.
  MAX_N = 200005 #Namber of nodes
  to = [[0 for _ in range(MAX_N)] for _ in range(D)]

  # to[i][j]:nodes to 2^i away from j  
  for i in range(n):
    to[0][i] = A[i]
    to[0][i] -= 1

  # Doubling matrix
  for i in range(D-1):
    for j in range(n):
        to[i+1][j] = to[i][to[i][j]]

  v = 0
  for i in range(D-1, -1, -1):
    l = 1<<i # Look go to 2^i
    if l <= k:
        v = to[i][v]
        k -= l

  print((v+1))
    
main()

# Optimized solution
#1 Doubling: O(log k)
# Python (3.8.2): TLE
# PyPy3
def main():
  ## IMPORT MODULE
  #import sys

  #sys.setrecursionlimit(100000)
  #input=lambda :sys.stdin.readline().rstrip()

  #f_inf=float("inf")
  #MOD=10**9+7
  
  if 'get_ipython' in globals(): 
    ## SAMPLE INPUT
    n, k = 4, 5
    A = [3, 2, 4, 1]

  else:
    ##INPUT 
    #n = input()
    n, k = list(map(int, input().split()))
    A = list(map(int, input().split()))

  ## SUBMITION CODES HERE
  D = 60 # 10^18 is 60 digits, so we'll see to the 59th power of 2.
  MAX_N = 200005 #Namber of nodes
  to = [[0 for _ in range(MAX_N)] for _ in range(D)]

  # to[i][j]:nodes to 2^i away from j  
  for i in range(n):
    to[0][i] = A[i]
    to[0][i] -= 1

  # Doubling matrix
  for i in range(D-1):
    for j in range(n):
        to[i+1][j] = to[i][to[i][j]]

  v = 0
  for i in range(D-1, -1, -1):
    l = 1<<i # Look go to 2^i
    if l <= k:
        v = to[i][v]
        k -= l

  print((v+1))
    
main()
# Original solution
import sys
readline = sys.stdin.readline

def main():
    N, K = list(map(int, readline().rstrip().split()))
    A = list(map(int, readline().rstrip().split()))
    d = {i+1: a for i, a in enumerate(A)}
    if K <= N:
        pre = 1
        for _ in range(K):
            pre = d[pre]
        print(pre)
        return

    pre = 1
    visited = set([1])
    visited_l = [1]
    for i in range(N):
        pre = d[pre]
        if pre in visited:
            break
        visited.add(pre)
        visited_l.append(pre)
    
    loop_start = visited_l.index(pre)
    loop = i - loop_start + 1

    num_loop = (K - loop_start) // loop
    now = loop_start + num_loop * loop
    for i in range(now+1, K+1):
        pre = d[pre]
    
    print(pre)

if __name__ == '__main__':
    main()

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

def main():
    N, K = list(map(int, readline().rstrip().split()))
    d = list(map(int, readline().rstrip().split()))
    # d = {i+1: a for i, a in enumerate(A)}
    if K <= N:
        pre = 1
        for _ in range(K):
            pre = d[pre-1]
        print(pre)
        return

    pre = 1
    visited = set([1])
    visited_l = [1]
    for i in range(N):
        pre = d[pre-1]
        if pre in visited:
            break
        visited.add(pre)
        visited_l.append(pre)
    
    loop_start = visited_l.index(pre)
    loop = i - loop_start + 1

    num_loop = (K - loop_start) // loop
    now = loop_start + num_loop * loop
    for i in range(now+1, K+1):
        pre = d[pre-1]
    
    print(pre)

if __name__ == '__main__':
    main()
# Original solution
n, k = list(map(int, input().strip().split()))
L = list(map(int, input().strip().split()))
L = [v - 1 for v in L]

seen = set()
cur = 0
untillBackToIntersec = 0

while cur not in seen:
    seen.add(cur)
    untillBackToIntersec += 1
    cur = L[cur]

intersec = cur
cur = 0
beforeLoop = 0

while cur != intersec:
    cur = L[cur]
    beforeLoop += 1
if k<=beforeLoop:
    proceed=k
else:
    loopSize = untillBackToIntersec - beforeLoop
    proceed=(k - beforeLoop) % (loopSize) + beforeLoop
ans = 0

for _ in range(proceed):
    ans = L[ans]

print((ans + 1))

# Optimized solution
n, k = list(map(int, input().strip().split()))
L = list(map(int, input().strip().split()))
L = [v - 1 for v in L]
 
seen = set()
intersec = 0
route=[]
while intersec not in seen:
    seen.add(intersec)
    route.append(intersec)
    intersec = L[intersec]

intersecInd = route.index(intersec)

if k<=intersecInd:
    ans=route[k]
else:
    loopSize = len(route) - intersecInd
    ans = route[(k - intersecInd) % (loopSize) + intersecInd]
 
print((ans + 1))
# Original solution

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

route = [0]
now_i = 0
loop_len = 0
for i in range(1, N):
    next_i = A[now_i]-1
    if next_i in route:
        loop_len = i - route.index(next_i)
        break
    route.append(next_i)
    now_i = next_i


base = len(route) - loop_len

if K < len(route):
    print((route[K]+1))
else:
    K -= base
    m = K % loop_len
    idx = route[base:][m]
    print((idx+1))



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

route = [0]
routeset = {0}
now_i = 0
loop_len = 0
for i in range(1, N):
    next_i = A[now_i]-1
    if next_i in routeset:
        loop_len = i - route.index(next_i)
        break
    routeset.add(next_i)
    route.append(next_i)
    now_i = next_i


base = len(route) - loop_len

if K < len(route):
    print((route[K]+1))
else:
    K -= base
    m = K % loop_len
    idx = route[base:][m]
    print((idx+1))
# Original solution
import time
N,K=list(map(int,input().split()))

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

A=list(array)
A.insert(0,0)

visit=[]
visited=1
once=0
while True:
    visit.append(visited)
    once+=1
    if A[visited] in visit:
        break
    visited=A[visited]

roop=visit[visit.index(A[visited]):]
if K<once:
    print((visit[K]))
else:
    order=K-once
    print((roop[order%len(roop)]))

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

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

A=list(array)
A.insert(0,0)

visit=0
root=[]
visited=1
once=0
while True:
    if visit & (0b1<<visited):
        break
    root.append(visited)
    visit|=0b1<<visited
    once+=1
    visited=A[visited]

roop=root[root.index(visited):]
if K<once:
    print((root[K]))
else:
    order=K-once
    print((roop[order%len(roop)]))
# Original solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

a = [i-1 for i in a]

p = 0
b = []
while p not in b:
    b.append(p)
    p = a[p]
avant = b.index(p)
loop_count = len(b) - avant
c = b[avant:]

if k <= avant:
    print((b[k]+1))
else:
    k -= avant
    k %= loop_count
    print((c[k]+1))


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

a = [i-1 for i in a]

d = [1]*n
p = 0
b = []
while d[p]:
    b.append(p)
    d[p] = 0
    p = a[p]
avant = b.index(p)
loop_count = len(b) - avant
c = b[avant:]

if k <= avant:
    print((b[k]+1))
else:
    k -= avant
    k %= loop_count
    print((c[k]+1))
# Original solution
import sys
import math
import itertools

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

  now = 0
  times = 0
  visit =[[0],[0]]

  for _ in range(k):
    times += 1
    now = a[now] - 1
    roop = 0
    roop_start = 0

    if now in visit[0]:
      roop_start = visit[1][visit[0].index(now)] #ループが始めった待ちの最初に訪れた時に何回ループしていたかをroop_startに代入
      roop = times - roop_start #何個の町でループしてるかをroopに代入
      break
    else:
      visit[0].append(now)
      visit[1].append(times)

  if roop > 0:
    k = (k - roop_start) % roop

  # print(roop_start)
  # print(roop)
  # print(k)
  # print(visit[0])
  print((visit[0][roop_start + k] + 1))

  # now = 0
  # for _ in range(roop_start):
  #   now = a[now] - 1

  # for _ in range(k):
  #   now = a[now] -1

  # print(now+1)


if __name__ == '__main__':
  main()

# Optimized solution
import sys
import math
import itertools

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

  now = 0
  times = 0
  visit =[[0],[-1 for _ in range(n)]]
  visit[1][0] = 0
  roop = 0
  roop_start = 0

  for _ in range(k):
    times += 1
    now = a[now] - 1

    if visit[1][now] != -1:
      roop_start = visit[1][now] #ループが始めった待ちの最初に訪れた時に何回ループしていたかをroop_startに代入
      roop = times - roop_start #何個の町でループしてるかをroopに代入
      break
    else:
      visit[0].append(now)
      visit[1][now] = len(visit[0]) - 1

  if roop > 0:
    k = (k - roop_start) % roop

  # print(roop_start)
  # print(roop)
  # print(k)
  # print(visit[0])
  print((visit[0][roop_start + k] + 1))

  # now = 0
  # for _ in range(roop_start):
  #   now = a[now] - 1

  # for _ in range(k):
  #   now = a[now] -1

  # print(now+1)


if __name__ == '__main__':
  main()
# Original solution
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

INF = 10 ** 10

N, K  = [int(x) for x in stdin.readline().rstrip().split()]
A = [0] + [int(x) for x in stdin.readline().rstrip().split()]

stack = [1]
nextt = A[1]

while True:
    if not nextt in stack:
        stack.append(nextt)
    else:
        stack.append(nextt)
        break
    nextt = A[nextt]

    if len(stack) > K:
        print((stack[K]))
        exit()

num1 = stack.pop()
length = len(stack)
tmp = []
while True:
    num2 = stack.pop()
    if num1 != num2:
        tmp.append(num2)
    else:
        tmp.append(num2)
        break

tmp = tmp[::-1]

index = (K - length) % len(tmp)

print((tmp[index]))
    


# Optimized solution
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

INF = 10 ** 10

N, K  = [int(x) for x in stdin.readline().rstrip().split()]
A = [0] + [int(x) for x in stdin.readline().rstrip().split()]

stack = [1]
nextt = A[1]
visited_dict = {1: True}

while True:
    if not visited_dict.get(nextt, False):
        stack.append(nextt)
        visited_dict[nextt] = True
    else:
        stack.append(nextt)
        break
    nextt = A[nextt]

    if len(stack) > K:
        print((stack[K]))
        exit()

num1 = stack.pop()
length = len(stack)
tmp = []
while True:
    num2 = stack.pop()
    if num1 != num2:
        tmp.append(num2)
    else:
        tmp.append(num2)
        break

tmp = tmp[::-1]

index = (K - length) % len(tmp)

print((tmp[index]))
# Original solution
n, k = list(map(int, input().split()))

a = [0] + list(map(int, input().split()))
a1 = [0] * (n+1)

step = 1
start = 0
while(True):
    start += 1
    a1[step] += 1
    step = a[step]
    if a1[step] == 1:
        break
c = step
gap = 0
while(True):
    gap += 1
    step = a[step]
    if step == c:
        break
result = 1
if k > start:
    k = (k-start) % gap
    result = c
for _ in range(k):
    result = a[result]

print(result)


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

repeat = [0] * (n+1)
circle = [1]
point = 1
flag = 0
for _ in range(k):
    circle.append(a[point])
    repeat[point] = 1
    point = a[point]
    if(repeat[point] == 1):
        flag = 1
        break

if flag == 0:
    print((circle[-1]))
else:
    index = circle.index(point)
    circle_cut = circle[index:-1]
    k = (k-index) % len(circle_cut)
    print((circle_cut[k]))
# Original solution
from sys import stdin
from sys import setrecursionlimit
setrecursionlimit(10 ** 7)

n,k = list(map(int,stdin.readline().rstrip().split()))
a = list(map(int,stdin.readline().rstrip().split()))
a = [0]+a
bunki = 0
now = a[1]
lip = [1]
while True:
    bunki += 1
    if now in lip:
        break
    lip.append(now)
    now = a[now]

tenki = lip.index(now)

lis = lip[tenki:]

roop = bunki-tenki

if k < tenki: 
    print((lip[k]))
    exit()
k -= tenki

print((lis[k%roop]))

# Optimized solution
from sys import stdin
from sys import setrecursionlimit
setrecursionlimit(10 ** 7)

n,k = list(map(int,stdin.readline().rstrip().split()))
a = list(map(int,stdin.readline().rstrip().split()))
a = [0]+a
bunki = 0
now = a[1]
lip = [1]
se = set()
while True:
    bunki += 1
    if now in se:
        break
    se.add(now)
    lip.append(now)
    now = a[now]

tenki = lip.index(now)
lis = lip[tenki:]
roop = bunki-tenki

if k < tenki: 
    print((lip[k]))
    exit()
k -= tenki

print((lis[k%roop]))
# Original solution
import sys
input = sys.stdin.readline

def main():
    n, k = list(map(int, input().split()))
    a = list(map(int, input().split()))
    l = [-1]*n
    now = 0
    l[0] = 0
    ans = -100

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

    for i in range(n*3):
        k -= 1
        now = a[now] #転送された場所
        l[now] += 1
        if l[now] == 2: #3ループ目の頭
            loop = l.count(1) + 1
            k %= loop
        if k == 0:
            ans = now + 1
            break

    print((max(ans, -1)))

main()

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

def main():
    n, k = list(map(int, input().split()))
    a = list(map(int, input().split()))
    l = [-1]*n
    now = 0
    order = 0
    l[0] = 0
    s = {}
    ans = -100

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

    while k > 0:
        k -= 1
        now = a[now]
        order += 1
        if l[now] != -1: #既に訪問済の場合
            loop = order - l[now]
            k %= loop
        if k == 0:
            ans = now + 1
            break
        l[now] = order #転送順で何番目の町かを記録

    print((max(ans, -1)))

main()
# Original solution
N, K = list(map(int, input().split()))
D = K.bit_length()
to = [[0] * D for _ in range(N)]

for i, a in enumerate(map(int, input().split())):
    to[i][0] = a - 1

for j in range(D - 1):
    for i in range(N):
        to[i][j+1] = to[to[i][j]][j]

v = 0
for i in reversed(list(range(D))):
    if (K >> i) & 1:
        v = to[v][i]

print((v + 1))

# Optimized solution
def main():
    N, K = list(map(int, input().split()))
    D = K.bit_length()
    to = [[0] * D for _ in range(N)]

    for i, a in enumerate(map(int, input().split())):
        to[i][0] = a - 1

    for j in range(D - 1):
        for i in range(N):
            to[i][j+1] = to[to[i][j]][j]

    v = 0
    for i in reversed(list(range(D))):
        if (K >> i) & 1:
            v = to[v][i]

    print((v + 1))


if __name__ == "__main__":
    main()
# Original solution
n, k = list(map(int, input().split()))
*A, = list(map(int, input().split()))
A = [0] + A
DP = [[None for j in range(60)] for i in range(n + 1)]
for i in range(n + 1):
    DP[i][0] = A[i]
for j in range(1, 60):
    for i in range(n + 1):
        DP[i][j] = DP[DP[i][j - 1]][j - 1]
K = list(bin(k)[2:][::-1])
i = 1
for j, b in enumerate(K):
    if b == '1':
        i = DP[i][j]
print(i)


# Optimized solution
n, k = list(map(int, input().split()))
*A, = list(map(int, input().split()))
A = [0] + A
bit = list(bin(k)[2:][::-1])
m = len(bit)
DP = [[None for j in range(m)] for i in range(n + 1)]
for i in range(n + 1):
    DP[i][0] = A[i]
for j in range(1, m):
    for i in range(n + 1):
        DP[i][j] = DP[DP[i][j - 1]][j - 1]
i = 1
for j, b in enumerate(bit):
    if b == '1':
        i = DP[i][j]
print(i)
# Original solution
import math
from collections import deque
import sys
sys.setrecursionlimit(10**9)

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

    graph = [0] * (N+1)
    for i in range(N+1): #1-indexed
        f = i
        t = A[i-1]
        graph[f] = t


    def solve():
        visited = []
        stack = deque([1])
        x = 1
        count = 0
        while len(stack) > 0:
            x = stack.pop()
            if x in visited:
                break

            visited.append(x)
            stack.append(graph[x])
            count += 1

            if count == K:
                print((stack.pop()))
                exit()

        loop_head = x
        head_index = visited.index(loop_head)
        loop_len = len(visited) - head_index

        r = (K-head_index)%loop_len
        print((visited[head_index:][r]))


    solve()


if __name__ == "__main__":
  main()


# Optimized solution
from collections import deque
import sys
sys.setrecursionlimit(10**9)

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

    graph = [0] * (N+1)
    for i in range(N+1): #1-indexed
        f = i
        t = A[i-1]
        graph[f] = t


    def solve():
        visited_set = set([])
        visited = []
        stack = deque([1])
        x = 1
        count = 0
        while len(stack) > 0:
            x = stack.pop()
            if x in visited_set:
                break

            visited_set.add(x)
            visited.append(x)
            stack.append(graph[x])
            count += 1

            if count == K:
                print((stack.pop()))
                exit()

        loop_head = x
        head_index = visited.index(loop_head)
        loop_len = len(visited) - head_index

        r = (K-head_index)%loop_len
        print((visited[head_index:][r]))



    solve()


if __name__ == "__main__":
  main()
# Original solution
#!/usr/bin/env python3

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

tele = [1,A[0]]
B = [0]
z = 0
s = 0
for i in range(N):
    if z in B and len(B) != 1:
        s = B.index(z)
        break
    tele.append(A[tele[-1]-1])
    B.append(tele[-2]-1)
    z = tele[-1]-1

if K <= s:
    print((tele[K]))
else:
    K -= s
    amari = K%(len(tele[s:len(tele)-1]))
    tele = tele[s:len(tele)-1]*2
    print((tele[amari]))


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

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

for i in range(N):
    A[i] -= 1

tele = [0]
#tele = [0,A[0]-1]
z = 0
s = 0
#B = {0:0,A[0]-1:1}
B = {0:0}
for i in range(N):
    if A[tele[-1]] in B and len(B) != 1:
        s = B[A[tele[-1]]]
        break
    B[A[tele[-1]]] = i+1
    tele.append(A[tele[-1]])
    z = tele[-1]

if K <= s:
    print((tele[K]+1))
else:
    K -= s
    amari = K%(len(tele[s:len(tele)]))
    tele = tele[s:len(tele)]*2
    print((tele[amari]+1))