CodeEff/ECCO huggingface dataset:
# Original solution
n,k=list(map(int,input().split()))
a = list(map(int,input().split()))
rep = []
rep.append(1)
i = 0
cnt = 0
if n>k:
    end =k
else:
    end =n
nof = 0
while True:
    cnt+=1
    if cnt>end:
        nof=1
        break
    go = a[i]-1
    if a[i] not in rep:
        rep.append(a[i])
        i = go
    else:
        rep.append(a[i])
        break
if nof==1:
    print((rep[k]))
else:
    lr = len(rep)
    for i in range(lr):
        if rep[i]==rep[lr-1]:
            start=i
            rep2 = rep[i:lr-1]
            break
    mod = len(rep2)
    modL = [i%mod for i in range(start,start+mod)]
    resM = k%mod
    for i in range(mod):
        if resM == modL[i]:
            print((rep2[i]))

# Optimized solution
n,k=list(map(int,input().split()))
a = list(map(int,input().split()))
visit = []
visit = [0]*n
pos=0
move=[]
roop=[]
while visit[pos]!=2:
    if visit[pos]==0:
        move.append(pos)
    else:
        roop.append(pos)
    visit[pos]+=1
    pos = a[pos]-1
if len(move)>k:
    print((move[k]+1))
else:
    mod = (k-(len(move)-len(roop)))%len(roop)
    print((roop[mod]+1))
# Original solution
N, K = list(map(int, input().split()))
As = list(map(int, input().split()))
ts = []

t = 0
for i in range(N):
    ts.append(t)
    t = As[t]-1
    if t in ts:
        break

d = ts.index(t)
p = i - d + 1

if K > d:
    idx = (K - d) % p + d
else:
    idx = K

print((ts[idx]+1))


# Optimized solution
N, K = list(map(int, input().split()))
As = list(map(int, input().split()))
tl = [0]
ts = {0}

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

d = tl.index(t)
p = i - d + 1

if K > d:
    idx = (K - d) % p + d
else:
    idx = K

print((tl[idx]+1))
# Original solution
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
idx=1
li=[]
idx_li=[1]
flag=False
for i in range(k):
    idx=a[idx-1]
    if idx in idx_li:
        flag=True
        break
    idx_li.append(idx)
if flag:
    #non_loop=idx_li[:idx_li.index(idx)]
    loop = idx_li[idx_li.index(idx):]
    
    size=len(loop)
    print((loop[(k-(len(idx_li)-len(loop)))%size]))
else:
    print(idx)

# Optimized solution
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
idx=1
idx_li=[]
ord_=[-1]*n
flag=False
for i in range(k):
    idx_li.append(idx)
    ord_[idx-1]=1
    idx=a[idx-1]
    if ord_[idx-1]==1:
        flag=True
        break
if flag:
    #non_loop=idx_li[:idx_li.index(idx)]
    loop = idx_li[idx_li.index(idx):]
    
    size=len(loop)
    print((loop[(k-(len(idx_li)-len(loop)))%size]))
else:
    print(idx)
# Original solution
N, K = list(map(int, input().split()))
As = list(map(int, input().split()))

path = [1]
for _ in range(N+10):
    next_A = As[path[-1] - 1]
    if next_A in path:
        break
    else:
        path.append(next_A)

before_loop = path.index(next_A)
loop_len = len(path) - before_loop

if K <= before_loop:
    print((path[K]))
else:
    print((path[(K - before_loop) % loop_len + before_loop]))


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

appeared = [False] * N
appeared[0] = True
path = [1]
for _ in range(N+10):
    next_A = As[path[-1] - 1]
    if appeared[next_A-1]:
        break
    else:
        path.append(next_A)
        appeared[next_A-1] = True

before_loop = path.index(next_A)
loop_len = len(path) - before_loop

if K <= before_loop:
    print((path[K]))
else:
    print((path[(K - before_loop) % loop_len + before_loop]))
# Original solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

now=1
flg = 0
path = [1]
pathcheck ={1}
for i in range(k):
    now = a[now-1]
    if now in pathcheck:
        flg = -1
        path.append(now)
        break
    path.append(now)
    pathcheck.add(now)


if flg == 0:
    print(now)
    exit()

x1 = path.index(now)
x2 = len(path)-1
x3 = (k-x1)%(x2-x1)
print((path[x1+x3]))

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

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

    now=1
    flg = 0
    path = [1]
    pathcheck ={1}
    for i in range(k):
        now = a[now-1]
        if now in pathcheck:
            flg = -1
            path.append(now)
            break
        path.append(now)
        pathcheck.add(now)


    if flg == 0:
        print(now)
        exit()

    x1 = path.index(now)
    x2 = len(path)-1
    x3 = (k-x1)%(x2-x1)
    print((path[x1+x3]))

if __name__ == '__main__':
    main()
# Original solution
n, k = list(map(int, input().split()))
an = list(map(int, input().split()))
for i in range(n):
    an[i] -= 1
    
import math
dv = []
dv.append(an)
K = k
N = n
for _ in range(1, int(math.log2(K)) + 1):
    l = [0] * N
    dv.append(l)

for k in range(1, int(math.log2(K)) + 1):
    for n in range(N):
        dv[k][n] = dv[k - 1][dv[k - 1][n]]
        
# N 回目を 2 ^ t で表すためにビット演算を用いる
# a : dv の何行目を用いるかを格納
a = []
for i in range(int(math.log2(K)) + 1):
    if K >> i & 1:
        a.append(i)
        
now = 0
for i in a:
    now = dv[i][now]
        
print((now + 1))


# Optimized solution
N, K = list(map(int, input().split()))
ai = list(map(int, input().split()))
for i in range(N):
    ai[i] -= 1
    
import math
dv = []
dv.append(ai)
for k in range(1, int(math.log2(K)) + 1):
    l = [0] * N
    dv.append(l)
    for n in range(N):
        dv[k][n] = dv[k - 1][dv[k - 1][n]]
        
a = []
for i in range(int(math.log2(K)) + 1):
    if K >> i & 1:
        a.append(i)
        
now = 0
for i in a:
    now = dv[i][now]
print((now + 1))
# Original solution
N, K = list(map(int, input().split()))
ai = list(map(int, input().split()))
for i in range(N):
    ai[i] -= 1
    
import math
dv = []
dv.append(ai)
for k in range(1, int(math.log2(K)) + 1):
    l = [0] * N
    dv.append(l)
    for n in range(N):
        dv[k][n] = dv[k - 1][dv[k - 1][n]]
        
a = []
for i in range(int(math.log2(K)) + 1):
    if K >> i & 1:
        a.append(i)
        
now = 0
for i in a:
    now = dv[i][now]
print((now + 1))

# Optimized solution
N, K = list(map(int, input().split()))
ai = list(map(int, input().split()))
for i in range(N):
    ai[i] -= 1
    
import math
dv = []
dv.append(ai)
for k in range(1, int(math.log2(K)) + 1):
    l = [0] * N
    dv.append(l)
    for n in range(N):
        dv[k][n] = dv[k - 1][dv[k - 1][n]]
        
#a = []
now = 0
for i in range(int(math.log2(K)) + 1):
    if K >> i & 1:
        #a.append(i)
        now = dv[i][now]
print((now + 1))
# Original solution
import sys
input = sys.stdin.readline
from collections import *

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
log_size = 100
dp = [[0]*N for _ in range(log_size)]

for i in range(N):
    dp[0][i] = A[i]-1
    
for i in range(1, log_size):
    for j in range(N):
        dp[i][j] = dp[i-1][dp[i-1][j]]

c = 0
cur = 0
    
while K>0:
    if K&1:
        cur = dp[c][cur]
        
    c += 1
    K >>= 1

print((cur+1))

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

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
l = [0]
s = {0}

for i in range(K):
    nex = A[l[-1]]-1
    
    if nex in s:
        for i in range(len(l)):
            if l[i]==nex:
                mark = i
                break
        
        t = len(l)-mark
        print((l[mark+(K-mark)%t]+1))
        exit()
    
    l.append(nex)
    s.add(nex)

print((nex+1))
# Original solution
n,k = list(map(int,input().split()))
a = [0]+list(map(int,input().split()))

def bin(arr,val):
    left,right = 0,len(arr)-1
    while left<=right:
        mid = (left+right)//2
        if arr[mid]==val: return 1
        elif arr[mid]<val: left = mid + 1
        else: right = mid - 1
    return 0

know = []
now = 1
while not bin(know, now):
    know.append(now)
    now = a[now]

start = a[know[-1]]
for i,num in enumerate(know):
    if num == start: loop = know[i:] ; break

#print(know,start,loop)

if k < len(know):
    ans = know[k]
else:
    k -= len(know)
    ans = loop[k%len(loop)]

print(ans)


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

def bin(arr,val):
    left,right = 0,len(arr)-1
    while left<=right:
        mid = (left+right)//2
        if arr[mid]==val: return 1
        elif arr[mid]<val: left = mid + 1
        else: right = mid - 1
    return 0

know = []
now = 1
while not bin(know, now):
    know.append(now)
    now = a[now]

start = a[know[-1]]
for i,num in enumerate(know):
    if num == start: loop = know[i:] ; break

ans = know[k] if k<len(know) else loop[(k-len(know))%len(loop)]
print(ans)
# Original solution
N, K = list(map(int, input().split()))
towns = list(map(int, input().split()))
towns.insert(0, -1)
p = 1
tmp_move, first_move, roop_move = [1], [], []
res = -1

while True:
    if towns[p] in tmp_move:
        roop_move = tmp_move[tmp_move.index(towns[p]):]
        first_move = tmp_move[:tmp_move.index(towns[p])]
        break
    else:
        tmp_move.append(towns[p])
        p = towns[p]

if K < len(first_move):
    res = first_move[K]
else:
    index = (K - len(first_move)) % len(roop_move)
    res = roop_move[index]

print(res)

# Optimized solution
N, K = list(map(int, input().split()))
towns = list(map(int, input().split()))
towns.insert(0, -1)
p = 1
tmp_move, first_move, roop_move = [1], [], []
visited = [False for _ in range(N + 1)]
res = -1

while not visited[p]:
    visited[p] = True
    tmp_move.append(towns[p])
    p = towns[p]

index = tmp_move.index(p)
roop_move = tmp_move[index:-1]
first_move = tmp_move[:index]

if K < len(first_move):
    res = first_move[K]
else:
    index = (K - len(first_move)) % len(roop_move)
    res = roop_move[index]

print(res)
# Original solution
import sys
input = sys.stdin.readline
from collections import Counter
def main():
    n, k = list(map(int, input().split()))
    a = list(map(int, input().split()))
    a = [i - 1 for i in a]
    ac = Counter(a)
    kaiten = [0] * n

    for i1 in range(n):
        if ac[i1] >= 1:
            kikan = n
            current = i1
            count = 0
            while kikan > 0:
                current = a[current]
                count += 1
                if current == i1:
                    kaiten[i1] = count
                    break
                kikan -= 1
    #print(kaiten)
    koko = 0
    go = 0
    while True:
        go += 1
        koko = a[koko]
        if kaiten[koko] > 0:
            break

    remain_go = (k - go) % kaiten[koko]
    while remain_go > 0:
        koko = a[koko]
        remain_go -= 1

    print((koko + 1))

if __name__ == '__main__':
    main()


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

def main():
    n, k = list(map(int, input().split()))
    a = [0]
    ta = list(map(int, input().split()))
    a += ta
    kaiten = [0] * (n + 1)
    count = 0
    koko = 1
    kaitenkikan = 0
    while k > 0:
        koko = a[koko]
        count += 1
        k -= 1
        if kaiten[koko] > 0:
            kaitenkikan = abs(count - kaiten[koko])
            break
        else:
            kaiten[koko] = count
    if k == 0:
        print(koko)
        sys.exit()
    k_remain = k % kaitenkikan
    if k_remain < 0:
        k_remain += kaitenkikan

    while k_remain > 0:
        koko = a[koko]
        k_remain -= 1

    print(koko)

if __name__ == '__main__':
    main()
# Original solution
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import defaultdict
def main():
    n, k, *a = list(map(int, read().split()))

    cur_town = 0
    transits = set()
    tran_time = defaultdict(int)
    cnt = 0
    while True:
        if k == 0:
            print((cur_town + 1))
            sys.exit()
        if cur_town in transits:
            break
        else:
            transits.add(cur_town)
            tran_time[cur_town] = cnt
            cur_town = a[cur_town] - 1
            cnt += 1
            k -= 1
    k2 = k % (cnt - tran_time[cur_town])
    while k2 > 0:
        k2 -= 1
        cur_town = a[cur_town] - 1
    print((cur_town + 1))

if __name__ == '__main__':
    main()

# Optimized solution
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import defaultdict
def main():
    n, k, *a = list(map(int, read().split()))

    cur_town = 0
    tran_time = defaultdict(int)
    cnt = 0
    while True:
        if k == 0:
            print((cur_town + 1))
            sys.exit()
        if cur_town in tran_time:
            break
        else:
            tran_time[cur_town] = cnt
            cur_town = a[cur_town] - 1
            cnt += 1
            k -= 1
    k2 = k % (cnt - tran_time[cur_town])
    while k2 > 0:
        k2 -= 1
        cur_town = a[cur_town] - 1
    print((cur_town + 1))

if __name__ == '__main__':
    main()
# Original solution
n,k = [int(i) for i in input().split()]
data = [int(i) for i in input().split()]

d = 1
count = 0
loop = []
route = [-1] * n
data_len = len(data)

for i in range(data_len):
    route[i] = d
    d = data[d-1]
    if d in route:
        loop = route[route.index(d):route.index(-1)]
        break
loop_len = len(loop)
if loop_len > 0:
    a = len(route[:route.index(-1)]) - loop_len
    if k < a:
        print((route[k]))
    else:
        print((loop[(k-a)%loop_len]))
else:
    print((route[k]))

# Optimized solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
visited = [0]
for _ in range(n):
    now = a[visited[-1]]-1
    visited.append(now)

if k < n:
    print((visited[k]+1))
else:
    p = visited.index(visited[-1])
    loop = visited[::-1][1:].index(visited[-1])+1
    x = (k-p-1)%loop
    print((visited[p+x+1]+1))
# Original solution
def main():
    N, K, *A = list(map(int, open(0).read().split()))
    A = [0] + A
    cur = 1
    visited = [-1] * (N + 1)
    visited[cur] = 0
    for i in range(1, K + 1):
        next = A[cur]
        if cur == next:
            print(next)
            return
        elif (j := visited[next]) != -1:
            print((visited.index((K - j) % (i - j) + j)))
            break
        else:
            visited[next] = visited[cur] + 1
            cur = next
    else:
        print(next)
        return


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    N, K, *A = list(map(int, open(0).read().split()))
    A = [0] + A
    cur_pos = 1
    last_visit = [-1] * (N + 1)
    last_visit[cur_pos] = 0
    for i in range(1, K + 1):
        nxt_pos = A[cur_pos]
        if cur_pos == nxt_pos:
            print(nxt_pos)
            return 
        elif (j := last_visit[nxt_pos]) != -1:
            print((last_visit.index((K - j) % (i - j) + j)))
            return 
        else:
            last_visit[nxt_pos] = last_visit[cur_pos] + 1
            cur_pos = nxt_pos
    print(cur_pos)


if __name__ == "__main__":
    main()
# Original solution
n, k = list(map(int, input().split()))

to = [0]
to += list(map(int, input().split()))


circle = [1]
st = -1
while True:
  point = to[circle[-1]]
  if point in circle:
    st = point
    break
  circle.append(point)

l1 = circle.index(st)
l2 = len(circle) - l1

if k < l1:
  print((circle[k]))
elif k < l1 + l2:
  print((circle[k]))
else:
  div = (k - l1) % l2
  print((circle[l1+div]))

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

to = [0]
to += list(map(int, input().split()))

passed = [0] * (n + 1)

circle = [1]
st = -1
while True:
  nx = to[circle[-1]]
  if passed[nx] == 1:
    st = nx
    break
  circle.append(nx)
  passed[nx] = 1
  

l1 = circle.index(st)
l2 = len(circle) - l1

if k < l1:
  print((circle[k]))
elif k < l1 + l2:
  print((circle[k]))
else:
  div = (k - l1) % l2
  print((circle[l1+div]))
# Original solution
def calc(i, k):
    ans_list = [i]
    count = 1
    i = a[i]
    while (i not in ans_list) & (count < n):
        ans_list.append(i)
        count += 1
        i = a[i]
    index = ans_list.index(i)
    ans_list.append(i)
    return count, ans_list

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

count, ans_list = calc(0, k)
if count < k:
    index = ans_list.index(ans_list[-1])
    k = k - index
    ans_list = ans_list[index + 1:]
    print((ans_list[k % len(ans_list) - 1] + 1))
else:
    print((ans_list[k] + 1))

# Optimized solution
n, k = list(map(int, input().split()))
a = [int(i) - 1 for i in input().split()]
visit = {0}
tmp = 0
a_list = [0]
ans = True
for i in range(k):
    tmp = a[tmp]
    if tmp in visit:
        left = a_list.index(tmp)
        ans = False
        print((a_list[left + (k - left) % (i + 1 - left)] + 1))
        break
    else:
        visit.add(tmp)
        a_list.append(tmp)
if ans:
    print((tmp + 1))
# Original solution
n,k = list(map(int,input().split()))
mp = list(map(int,input().split()))
save = []

s_queue = [[1,0]]
while(len(s_queue)>0):
    i,cnt = s_queue.pop()
    if(cnt==k):
        break
    if(mp[i-1] in save):
        last=mp[i-1]
        break
    save.append(mp[i-1])
    s_queue.append([mp[i-1],cnt+1])


ls = len(save)

if(k<=ls):
    print((save[k-1]))
else:
    for i in range(ls):
        if(save[i]==last):
            si = i
    k -= si
    save = save[si:]
    ls = len(save)
    amari = k%ls
    print((save[amari-1]))


# Optimized solution
n,k = list(map(int,input().split()))
mp = list(map(int,input().split()))
save = []
isSaved = [False]*(n+1)
s_queue = [[1,0]]
while(len(s_queue)>0):
    i,cnt = s_queue.pop()
    if(cnt==k):
        break
    if(isSaved[i]):
        last=mp[i-1]
        break
    nexti = mp[i-1]
    isSaved[i] = True
    save.append(nexti)
    s_queue.append([nexti,cnt+1])


ls = len(save)

if(k<=ls):
    print((save[k-1]))
else:
    for i in range(ls):
        if(save[i]==last):
            si = i
    k -= si
    save = save[si:]
    ls = len(save)
    amari = k%ls
    print((save[amari-1]))
# Original solution
n,k = list(map(int,input().split()))
mp = list(map(int,input().split()))
save = []
isSaved = [False]*(n+1)
s_queue = [[1,0]]
while(len(s_queue)>0):
    i,cnt = s_queue.pop()
    if(cnt==k):
        break
    if(isSaved[i]):
        last=mp[i-1]
        break
    nexti = mp[i-1]
    isSaved[i] = True
    save.append(nexti)
    s_queue.append([nexti,cnt+1])


ls = len(save)

if(k<=ls):
    print((save[k-1]))
else:
    for i in range(ls):
        if(save[i]==last):
            si = i
    k -= si
    save = save[si:]
    ls = len(save)
    amari = k%ls
    print((save[amari-1]))


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

isSearched = [False]*n
isSearched[0] = True
ans = []
def f():
    global k
    now = 0
    nex = A[0]
    ans.append(now)
    for i in range(k):
        now = nex
        ans.append(now)
        isSearched[now] = True
        nex = A[nex]
        if(isSearched[nex]):
            break

    if((len(ans)-1)==k):
        print((ans[-1]+1))
        return
    for i in range(len(ans)):
        if(ans[i]==nex):
            savei = i
    k -= savei
    mod = len(ans) - savei
    print((ans[savei+k%mod]+1))

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

start=1
list=[1]
kenti=[0 for i in range(n)]
kenti[0]=1

zan=-1

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

  if kenti[start-1]!=0:
    zan=k-(i+1)
    inx=list.index(start)
    list=list[inx:]
    break
  else:
    list.append(start)
    kenti[start-1]+=1

if zan==-1:
  print((list[-1]))
  exit()
elif zan==0:
  print((list[0]))
  exit()

#zan-=1
#print(list,zan)
zan%=len(list)
#print(zan)
'''
if zan==0:
  print(list[-1])
else:
  print(list[zan])
'''
print((list[zan]))

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

start=1
list=[1]
kenti=[0 for i in range(n)]
kenti[0]=1

zan=-1

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

  if kenti[start-1]!=0:
    zan=k-(i+1)
    inx=list.index(start)
    list=list[inx:]
    break
  else:
    list.append(start)
    kenti[start-1]+=1

if zan==-1:
  print((list[-1]))
  exit()
elif zan==0:
  print((list[0]))
  exit()

zan%=len(list)
print((list[zan]))
# Original solution
from sys import stdin
nii=lambda:list(map(int,stdin.readline().split()))
lnii=lambda:list(map(int,stdin.readline().split()))

n,k=nii()
a=lnii()

x=0
x_list=[1]

for i in range(k):
  nx=a[x]

  if nx in x_list:
    inx=x_list.index(nx)
    loop=x_list[inx:]
    zan=k-i

    q=zan%len(loop)

    nx=loop[q-1]
    break
  else:
    x=nx-1
    x_list.append(nx)

print(nx)

# Optimized solution
from sys import stdin
nii=lambda:list(map(int,stdin.readline().split()))
lnii=lambda:list(map(int,stdin.readline().split()))

n,k=nii()
a=lnii()

x=0
x_list=[1]
x_table=[0 for i in range(n+1)]
x_table[1]=1

for i in range(k):
  nx=a[x]

  if x_table[nx]!=0:
    inx=x_list.index(nx)
    loop=x_list[inx:]
    zan=k-i

    q=zan%len(loop)

    nx=loop[q-1]
    break

  else:
    x=nx-1
    x_list.append(nx)
    x_table[nx]+=1

print(nx)
# Original solution
import sys
n,k=list(map(int,input().split()))
a=[int(x) for x in input().split()]
vis=[0]
now=0
for i in range(1,k+1):
  now=a[now]-1
  if now in vis:
    bgn=vis.index(now)
    print((vis[bgn+((k-bgn)%(len(vis)-bgn))]+1))
    sys.exit()
  else:
    vis.append(now)
print((vis[-1]+1))

# Optimized solution
import sys
n,k=list(map(int,input().split()))
a=[int(x) for x in input().split()]
vis_ti=[-1]*n
vis_at=[0]
now=0
for i in range(1,k+1):
  now=a[now]-1
  if vis_ti[now]>=0:
    bgn=vis_ti[now]
    print((vis_at[bgn+(k-bgn)%(i-bgn)]+1))
    sys.exit()
  vis_ti[now]=i
  vis_at.append(now)
print((vis_at[-1]+1))
# Original solution
n, k = list(map(int, input().split()))
port = list(map(int, input().split()))
visited = [1]
current = 1

t = k

while port[current-1] not in visited and t > 0:
    visited.append(port[current-1])
    current = port[current-1]
    t -= 1

if t > 0:
    s = visited.index(port[current-1])
    bubun = visited[s:]
    g = (k-s) % len(bubun)
    print((bubun[g]))
else:
    print(current)


# Optimized solution
n, k = list(map(int, input().split()))
port = list(map(int, input().split()))
visited = [1]
flag = [False for i in range(n)]
current = 1

t = k

while flag[port[current-1]-1] == False and t > 0:
    visited.append(port[current-1])
    flag[port[current-1]-1] = True
    current = port[current-1]
    t -= 1

if t > 0:
    s = visited.index(port[current-1])
    bubun = visited[s:]
    g = (k-s) % len(bubun)
    print((bubun[g]))
else:
    print(current)
# Original solution
N,K=list(map(int,input().split()))
A=list(map(int,input().split()))

genzaichi=1
rireki=[]
houmon=[0]*(N+1)
flag=0

for i in range(1,K+1):
    genzaichi=A[genzaichi-1]
    if genzaichi not in rireki:
        houmon[genzaichi]=i
        rireki.append(genzaichi)
    else:
        roop_from=genzaichi
        x=houmon[roop_from]
        roop=i-x
        flag=1
        break

if flag==0:
    print(genzaichi)
else:
    genzaichi=roop_from
    K=(K-x)-((K-x)//roop)*roop
    for i in range(K):
        genzaichi=A[genzaichi-1]
    print(genzaichi)

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

genzaichi=1
rireki=set()
houmon=[0]*(N+1)
flag=0

for i in range(1,K+1):
    genzaichi=A[genzaichi-1]
    if genzaichi not in rireki:
        houmon[genzaichi]=i
        rireki.add(genzaichi)
    else:
        roop_from=genzaichi
        x=houmon[roop_from]
        roop=i-x
        flag=1
        break

if flag==0:
    print(genzaichi)
else:
    genzaichi=roop_from
    K=(K-x)-((K-x)//roop)*roop
    for i in range(K):
        genzaichi=A[genzaichi-1]
    print(genzaichi)