CodeEff/ECCO huggingface dataset:
# Original solution
n, k = list(map(int, input().split(" ")))
a = [-1] + list(map(int, input().split(" ")))
 
i = 1
head = []
used = []
roop = []
 
inte=False
for _ in range(k):
    i = a[i]
    if i in used:
        used.append(i)
        head = used[0: used.index(i)]
        roop = used[used.index(i): -1]
        inte =True
        break
    else:
        used.append(i)
 
if inte:
    if len(head) >= k:
        print((head[k-1]))
    else:
        x = (k - 1 - len(head)) % len(roop)
        print((roop[x]))
else:
    print((used[k-1]))


# Optimized solution
import sys

n, k = list(map(int, input().split(" ")))
a = [-1] + list(map(int, input().split(" ")))

i = 1
head = []
used = {}
roop = []

index = 0
for _ in range(k):
    index += 1
    i = a[i]
    if i in used:
        head = [ k for k, v in sorted(list(used.items()), key=lambda x:x[1]) if v < used[i]]
        roop = [ k for k, v in sorted(list(used.items()), key=lambda x:x[1]) if v >= used[i]]
        if len(head) >= k:
            print((head[k-1]))
        else:
            x = (k - 1 - len(head)) % len(roop)
            print((roop[x]))
            sys.exit()
    else:
        used[i] = index

print(([ k for k, v in sorted(list(used.items()), key=lambda x:x[1])][k-1]))
# Original solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

history = []

index = 0
cnt = 0
loop_start_city_index = 0
for i in range(k+1):
    if i == k:
        print((index+1))
        exit()

    if index in history:    # すでに訪れた街にきた
        loop_start_city_index = index
        break
    history.append(index)
    index = a[index] - 1
    cnt += 1

from_start_to_loop_start = history.index(loop_start_city_index)
loop_length = len(history) - from_start_to_loop_start

print((history[from_start_to_loop_start + (k-from_start_to_loop_start)%loop_length] + 1))

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

history = []
is_visited = [False] * n

index = 0
cnt = 0
loop_start_city_index = 0
for i in range(k+1):
    if i == k:
        print((index+1))
        exit()

    if is_visited[index]:    # すでに訪れた街にきた
        loop_start_city_index = index
        break
    
    is_visited[index] = True
    history.append(index)
    index = a[index] - 1
    cnt += 1

from_start_to_loop_start = history.index(loop_start_city_index)
loop_length = len(history) - from_start_to_loop_start

print((history[from_start_to_loop_start + (k-from_start_to_loop_start)%loop_length] + 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 = [0]+list(map(int, input().split()))

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

for i in range(k):
    num = a[num]
    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]))
# Original solution
# = int(input())
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
# = [int(input()) for i in range(N)]
# = [list(map(int, input().split())) for i in range(N)]
next = 1
List = []

for i in range(K):
    List.append(next)
    next = A[next-1]
    if len(set(List)) != len(List):
        break

if len(set(List)) != len(List):
    count = [i for i, x in enumerate(List) if x == List[-1]]
    loop = count[1] - count[0]
    afterloop = (K-count[0])%loop
    print((List[count[0]+afterloop]))

else:
    print(next)


# Optimized solution
# = int(input())
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
# = [int(input()) for i in range(N)]
# = [list(map(int, input().split())) for i in range(N)]
next = 1
List = [-1]*N

for i in range(K):
    if List[next-1] == -1:
        List[next-1] = i
        next = A[next-1]

    else:
        loop = i - List[next-1]
        afterloop = (K-(i-loop))%loop
        print((List.index((i-loop) + afterloop)+1))
        exit()

print(next)
# Original solution
from collections import deque
n,k = list(map(int,input().split()))
tele = {}
a = list(map(int,input().split()))
for i in range(1,n+1):
    tele[i] = a[i-1]
lis = deque([1])
now = 1
n = 1
while tele[now] not in lis:
    lis.append(tele[now])
    now = tele[now]
p = lis.index(tele[now])
q = len(lis) - p
if k <= p:
    print((lis[k]))
else:
    print((lis[(k-p)%q + p]))





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

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

l=[1]
di={}
di[1]=1

for i in a:
    b=a[l[-1]-1]
    if b in di:
        x=b
        break
    l.append(b)
    di[b]=1
t1=0
for j in l:
    if j==x:
        break
    else:
        t1+=1

t2=len(l)-t1
if k<=t1:
    print((l[k]))

else:
    aa=(k-t1)%t2
    print((l[t1+aa]))
# Original solution
N,K = list(map(int, input().split()))
A = [*[int(x)-1 for x in input().split()]]

cnt = [0]*N
pre = 0
for _ in range(N*2):
    cnt[pre] += 1
    now = A[pre]
    pre = now

one = 0
loops = 0
for c in cnt:
    if c == 1: one += 1
    elif c > 1: loops += 1

k = max(0, K-one) % loops + min(K, one)

pre = 0
for _ in range(k):
    now = A[pre]
    pre = now

ans = pre + 1
print(ans)


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

cnt, pre = [0]*N, 0
for _ in range(N*2):
    cnt[pre] += 1
    pre = A[pre]

one, loops = 0, 0
for c in cnt:
    if c == 1: one += 1
    elif c > 1: loops += 1

k = max(0, K-one) % loops + min(K, one)

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

pre = 0
index = [-1]*N
path_cnt = 0
for i in range(N):
    if index[pre] >= 0:
        break
    else:
        index[pre] = i
        path_cnt += 1
    pre = A[pre]

loops = path_cnt - index[pre]
one = path_cnt - loops
k = max(0, K-one) % loops + min(K, one)

pre = 0
for _ in range(k): pre = A[pre]
print((pre + 1))


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

index = [-1]*N
pre, path_cnt = 0, 0
for i in range(N):
    if index[pre] >= 0: break
    else:
        index[pre] = i
        path_cnt += 1
    pre = A[pre]

loops = path_cnt - index[pre]
one = index[pre]
k = max(0, K-one) % loops + min(K, one)

pre = 0
for _ in range(k): pre = A[pre]
print((pre + 1))
# Original solution
N,K = list(map(int, input().split()))
A = [*[int(x)-1 for x in input().split()]]
index = [-1]*N
path = []
pre = 0
while index[pre] < 0:
    index[pre] = len(path)
    path += [pre]
    pre = A[pre]
one = index[pre]
loops = len(path) - one

k = max(0, K-one) % loops + min(K, one)
pre = 0
for _ in range(k): pre = A[pre]
print((pre + 1))

# if K <= one: print(path[K])
# else: print(path[(K-one) % loops + one]+1)


# Optimized solution
N,K = list(map(int, input().split()))
A = [*[int(x)-1 for x in input().split()]]
index = [-1]*N
path = []
pre = 0
while index[pre] < 0:
    index[pre] = len(path)
    path += [pre]
    pre = A[pre]
one = index[pre]
loops = len(path) - one
if K < one: print((path[K]+1))
else: print((path[(K-one) % loops + one]+1))
# Original solution
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

order = []
ans = A[0] - 1
start = 0
stop = 0


for i in range(K):
    order.append(ans)
    if len(set(order)) != i+1:
        stop = i - 1
        start = order.index(ans)
        new = order[start:stop+1]
        break
    else:
        if i == K - 1:
            print((ans+1))
            exit()
        else:
            ans = A[ans] - 1


print((new[(K-start) % (stop-start+1)-1]+1))


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

n = 1
order = [n]
order_set = set([n])

for i in range(1, K + 1):
    n = A[n - 1]
    if n in order_set:
        Li = order.index(n)
        Ri = len(order)
        idx = Li + (K - Li) % (Ri - Li)
        print((order[idx]))
        exit()
    else:
        order.append(n)
        order_set.add(n)
        continue
print(n)
# Original solution
from itertools import count

N, K = list(map(int, input().split()))
As = [int(n) for n in input().split()]

visited = []
now = 0
num = 0
idx = 0
for i in count():
    if now in visited:
        idx = visited.index(now)
        num = len(visited) - idx
        break
    visited.append(now)
    now = As[now] - 1

if K <= idx:
    print((visited[K] + 1))
else:
    print((visited[idx + (K - idx) % num] + 1))

# Optimized solution
from itertools import count

N, K = list(map(int, input().split()))
As = [int(n) for n in input().split()]

visited = []
visited_set = set()
now = 0
num = 0
idx = 0
for i in count():
    if now in visited_set:
        idx = visited.index(now)
        num = len(visited) - idx
        break
    visited_set.add(now)
    visited.append(now)
    now = As[now] - 1

if K <= idx:
    print((visited[K] + 1))
else:
    print((visited[idx + (K - idx) % num] + 1))
# Original solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

loop = 0
use = [0] * n
now = 0
use[now] = 1
x = [1]
for _ in range(n):
   next = a[now] - 1
   x.append(next+1)
   if use[next] == 0:
      use[next] = 1
      now = next
   else:
      break

y = []
while x.count(x[0]) == 1:
   tmp = x.pop(0)
   y.append(tmp)

x.pop(-1)

if k < len(y):
   kk = k % len(y)
   print((y[kk]))
else:
   k -= len(y)
   kk = k % len(x)
   print((x[kk]))


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

loop = 0
use = [0] * n
now = 0
use[now] = 1
x = [1]
for _ in range(n):
   next = a[now] - 1
   x.append(next+1)
   if use[next] == 0:
      use[next] = 1
      now = next
   else:
      break
loop = x.pop(-1)

tmp = x.index(loop)
y = x[:tmp]
x = x[tmp:]
#print(y, x)

if k < len(y):
   kk = k % len(y)
   print((y[kk]))
else:
   k -= len(y)
   kk = k % len(x)
   print((x[kk]))
# Original solution
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
visited = [-1]*(N+1)
path = []
at = 1
count = 0
while True:
    if visited[at] != -1:
        loop = count-visited[at]
        if K < count: # (K-visited[at])//loopが1以上であることを保証
            print((path[K])) # 条件 K < visited[at]なら、(K-visited[at])//loopが0以上であることを保証。どっちでもよさそう。
        else:
            print((path[(K-visited[at])%loop+visited[at]]))
        break
    visited[at] = count
    count += 1
    path.append(at)
    at = A[at-1]

# Optimized solution
n, k = list(map(int, input().split()))
q = list(map(int, input().split()))
v = [-1]*(n+1)
p = []
a = 1
c = 0
while 1:
    if v[a] != -1:
        l = c-v[a]
        if k < c:
            print((p[k]))
        else:
            print((p[(k-v[a])%l+v[a]]))
        break
    v[a] = c
    c += 1
    p.append(a)
    a = q[a-1]
# Original solution
n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
A = [x-1 for x in A]

town = 0
cnt = 0

# memo = []
# visited = [False] * n
# visited[0] = -1

# start = 0
# loop = 10**8
# start_cnt = 0
# for i in range(n*2):
#     town = A[town]
#     memo.append(town)
#     if visited[town] != False:
#         start_cnt = i + 1
#         loop = i - visited[town]
#         break
#     else:
#         visited[town] = i
#
# for i in range((k-start_cnt)%loop):
#     town = A[town]
#
# print(town+1)

visited = [False] * n
visited[0] = 0

for i in range(n):
    town = A[town]
    if visited[town]:
        good_town = town
        break
    visited[town] = i+1

town = good_town

for i in range(n):
    town = A[town]
    if town == good_town:
        loop = i + 1

if k >= visited[good_town]:
    k = ((k-visited[good_town])%loop)+visited[good_town]


town = 0
for i in range(k):
    town = A[town]
print((town+1))

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

town = 0
cnt = 0

visited = [False] * n
visited[0] = -1

start_cnt = 0
for i in range(n*2):
    town = A[town]
    if visited[town] != False:
        start_cnt = i + 1
        loop = i - visited[town]
        break
    else:
        visited[town] = i

town = 0

if k >= start_cnt:
    k = (k-start_cnt)%loop+start_cnt

for i in range(k):
    town = A[town]

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

for i in range(k):
    now = a[now-1]
    if now in set(path):
        now = path[path.index(now) + (k-i-1)%(len(path) - path.index(now))]
        break
    path.append(now)

print(now)


# Optimized solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
alr = [0 for i in range(n)]
now = 1
path = [now]

for i in range(k):
    now = a[now-1]
    # if now in path:
    #     now = path[path.index(now) + (k-i-1)%(len(path) - path.index(now))]
    #     break
    if alr[now-1] != 0:
        now = path[path.index(now) + (k-i-1)%(len(path) - path.index(now))]
        break
    path.append(now)
    alr[now-1] += 1

print(now)
# Original solution
N, K = list(map(int, input().split(" ")))
routs = {}
location = [0 for i in range(N + 1)]
tereporter = list(map(int, input().split()))
for i in range(N):
  routs[i] = tereporter[i] - 1
present_location = 0
n = 0
for i in range(K):
  n += 1
  present_location = routs[present_location]
  if location[present_location] == 0:
    location[present_location] = n
  else:
    loop = n - location[present_location]
    if (K - location[present_location]) % loop == 0:
      break
    else:
      for i in range((K - location[present_location]) % loop):
        present_location = routs[present_location]
      break
print((present_location + 1))

# Optimized solution
N, K = list(map(int, input().split()))
teleporters = list(map(int, input().split()))
n = 0
present = 0
praces = [-1 for i in range(N)]
praces[0] = 0
for i in range(K):
  present = teleporters[present] - 1
  n += 1
  if praces[present] == -1:
    praces[present] = n
  else:
    before = praces[present]
    loop = n - before
    remainder = (K - before) % loop
    present = praces.index(before + remainder)
    break
print((present + 1))
# Original solution
N, K = list(map(int, input().split()))
teleporters = list(map(int, input().split()))
n = 0
present = 0
praces = [-1 for i in range(N)]
praces[0] = 0
for i in range(K):
  present = teleporters[present] - 1
  n += 1
  if praces[present] == -1:
    praces[present] = n
  else:
    before = praces[present]
    loop = n - before
    remainder = (K - before) % loop
    present = praces.index(before + remainder)
    break
print((present + 1))

# Optimized solution
from sys import exit
n, K = list(map(int, input().split()))
A = list([int(x) - 1 for x in input().split()])
done = [-1 for _ in range(n)]
tmp = 0
done[0] = 0
for k in range(1, K + 1):
    tmp = A[tmp]
    if done[tmp] >= 0:
        for i in range((K - done[tmp]) % (k - done[tmp])):
            tmp = A[tmp]
        print((tmp + 1))
        exit()
    else:
        done[tmp] = k
print((tmp + 1))
# Original solution
N, K = list(map(int, input().split()))

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

visitcount = dict()
n_point = list()

x = 1
count = 0
while(x not in set(visitcount)):
  visitcount[x] = count
  n_point.append(x)
  x = A[x-1]
  count += 1
  
period = count - visitcount[x]

if (K < visitcount[x]):
  print((n_point[K]))
else:
  y = (K-visitcount[x]) % period
  print((n_point[visitcount[x] + y]))


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

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

visitset = set()
visitcount = dict()
n_point = dict()

x = 1
count = 0
while(x not in visitset):
  visitset.add(x)
  visitcount[x] = count
  n_point[count] = x
  x = A[x-1]
  count += 1


period = count - visitcount[x]

if (K < visitcount[x]):
  print((n_point[K]))
else:
  y = (K-visitcount[x]) % period
  print((n_point[visitcount[x] + y]))
# Original solution
n,k,*a=list(map(int,open(0).read().split()))
d={}
s=1
while k:k-=1;s=a[s-1];k%=d.get(s,2**61)-k;d[s]=k
print(s)

# Optimized solution
n,k,*a=list(map(int,open(0).read().split()))
d=[2**61]*-~n
s=1
while k:k-=1;s=a[s-1];k%=d[s]-k;d[s]=k
print(s)
# Original solution
N, K = list(map(int, input().split()))
A = list([int(x) - 1 for x in input().split()])

R = []
done = set()
todo = [0]
done = set()
bp = None
while todo:
    p = todo.pop()
    R.append(p)
    done.add(p)
    np = A[p]
    if np in done:
        bp = np
    else:
        todo.append(np)
b = R.index(bp)
if K < b:
    ans = R[K] + 1
else:
    K -= b
    R = R[b:]
    ans = R[K%len(R)] + 1
print(ans)

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

done = {0}
rt = [0]
loop_point = 0
while True:
    p = rt[-1]
    np = A[p]
    if np in done:
        loop_point = np
        break
    done.add(np)
    rt.append(np)
if K < len(rt):
    ans = rt[K] + 1
else:
    K -= len(rt)
    rt = rt[rt.index(loop_point):]
    ans = rt[K%len(rt)] + 1
print(ans)
# Original solution
N, K = list(map(int, input().split()))
where_go = list(map(int, input().split()))
now = 0
PATH = []
start = 0
roop = 0
count = 0

while now not in PATH and count < K:
    PATH.append(now)
    now = where_go[now] -  1
    count += 1

if count < K:
    start = PATH.index(now)
    roop = len(PATH) - start
    K = (K - start) % roop
    count = 0
    while count < K:
        now = where_go[now] -  1
        count += 1

print((now+1))

# Optimized solution
N, K = list(map(int, input().split()))
A = list(map(int, input().split())) #どこに行くかのリスト
now = 0 #どこにいるか
LOG = {}
roop = 0
count = 0

while now not in LOG and count < K:
    LOG.setdefault(now, count)
    now = A[now] -  1
    count += 1

if count < K:
    roop = len(LOG) - LOG[now]
    K = (K - count) % roop
    count = 0
    while count < K:
        now = A[now] -  1
        count += 1

print((now+1)) #行った場所を保存
# Original solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
pl=1
visit=[1]
loop=0
q=True
v=True
cnt=0
br=False
while q:
    cnt+=1
    pl=a[pl-1]
    for i in visit:
        if pl==i:
            loop=visit.index(pl)
            br=not br
            break
    if br:
        break
    visit.append(pl)
    if cnt==k:
        print(pl)
        v=not v
        break
    if cnt==10**7:
        q=not q
if v:
    loop_len=cnt-loop
    q=True
    k=(k-loop)%loop_len
    cnt=0
    if k==0:
        print(pl)
    else:
        while q:
            cnt+=1
            pl=a[pl-1]
            if cnt==k:
                print(pl)
                break
            if cnt==10**7:
                q=not q

# Optimized solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
pl=1
visit=[-1]*n
visit[0]=0
loop=0
q=True
v=True
cnt=0

while q:
    cnt+=1
    pl=a[pl-1]
    if visit[pl-1]!=-1:
        loop=visit[pl-1]
        break
    visit[pl-1]=cnt
    if cnt==k:
        print(pl)
        v=not v
        break
    if cnt==10**7:
        q=not q
if v:
    loop_len=cnt-loop
    q=True
    k=(k-loop)%loop_len
    cnt=0
    if k==0:
        print(pl)
    else:
        while q:
            cnt+=1
            pl=a[pl-1]
            if cnt==k:
                print(pl)
                break
            if cnt==10**7:
                q=not q
# Original solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
pl=1
visit=[-1]*n
visit[0]=0
loop=0
q=True
v=True
cnt=0

while q:
    cnt+=1
    pl=a[pl-1]
    if visit[pl-1]!=-1:
        loop=visit[pl-1]
        break
    visit[pl-1]=cnt
    if cnt==k:
        print(pl)
        v=not v
        break
    if cnt==10**7:
        q=not q
if v:
    loop_len=cnt-loop
    q=True
    k=(k-loop)%loop_len+loop
    print((visit.index(k)+1))

# Optimized solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
pl=1
visit=[-1]*n
visit[0]=0
loop=0
cnt=0

while 1:
    cnt+=1
    pl=a[pl-1]
    if visit[pl-1]!=-1:
        loop=visit[pl-1]
        break
    visit[pl-1]=cnt
    if cnt==k:
        print(pl)
        exit()

loop_len=cnt-loop
q=True
k=(k-loop)%loop_len+loop
print((visit.index(k)+1))
# Original solution
n, k = list(map(int, input().split()))

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

explored = [0] * (n+1)
t = 1
cycle = 0
while True:
    if explored[t] == 2:
        k %= cycle
        explored[t] += 1
    elif explored[t] == 1:
        explored[t] += 1
        cycle += 1
    elif explored[t] == 0:
        explored[t] += 1
    k -= 1

    if k == 0:
        break
    else:
        t = a[t]
    

print((a[t]))

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

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

explored = [0] * (n+1)
t = 1
cycle = 0
while True:
    if explored[t] == 2:
        k %= cycle
        if k == 0:
            k = cycle
        explored[t] += 1
    elif explored[t] == 1:
        explored[t] += 1
        cycle += 1
    elif explored[t] == 0:
        explored[t] += 1

    k -= 1
    if k == 0:
        break
    else:
        t = a[t]
    

print((a[t]))
# Original solution
n, k, *A = list(map(int, open(0).read().split()))

#n, k = map(int, input().split())
#A = [int(a) for a in input().split()]
Jump = [None] * n
Jump[0] = 1
next = 0
for i in range(1,n+1):
    to = A[next]
    if to in Jump:
        start = Jump.index(to)
        end = i
        dis = end - start

        roop = (k-start) % dis
        print((Jump[start+roop]))
        break

    Jump[i] = to
    next = to-1
    if i ==k:
        print(to)
        break


# Optimized solution
n, k = list(map(int, input().split()))
A = [int(a) for a in input().split()]
Jump = [None] * n
Jump[0] = 1
Rireki = [-1] * (n+1)
Rireki[0] = 0
next = 0
for i in range(1,n+1):
    to = A[next]

    if Rireki[to] >= 0:
        start = Jump.index(to)
        end = i
        dis = end - start

        roop = (k-start) % dis
        print((Jump[start+roop]))
        break
    Rireki[to] = i
    Jump[i] = to
    next = to-1
    if i ==k:
        print(to)
        break
# Original solution
n,k = list(map(int,input().split()))
s = list(map(int,input().split()))
# print(s)
now_city = 1
s_l_city = 1
t = [1] #何回そこを通ったか
f = [0] #何回目にそこを通ったか
a=0
b = 0#サイクルありなし
f_l=0
for i in range(n-1):
    t.append(0)
    f.append(-1)
# print(s)
sycle = 0
for i in range(1,n+1):
    now_city = s[now_city-1]
    t[now_city-1]+=1
    for j in range(n):
        if t[j]==2:
        #  print(i,f[j])
         sycle=i-f[j]
         f_l=f[j]
         a = 1
         b = 1
         s_l_city=j+1
         break
    if a==1:
        break
    f[now_city-1]=i
# print(t)
# print(f_l,sycle,s_l_city)
if b==1:
    now_city=s_l_city
    for j in range((k-f_l)%sycle):
        now_city = s[now_city-1]
    print(now_city)
else:
    for j in range(k):
        now_city=s[now_city]
    print(now_city)

# Optimized solution
N, K = list(map(int, input().split()))
A = input().split()
A = [int(a) for a in A]
S = [1]
a = 1
number = 0
numbers=[-1 for _ in range(N)]
for _ in range(N + 1):
    b = A[a - 1]
    if numbers[b-1] != -1:
      c=numbers[b-1]          #1回目にループが現れた回数-1
    #   print("ループの最初の数字",b,"最初は"+str(c+1)+"回目","周期は",number-c)
      break
    numbers[b-1]=number
    number += 1
    S.append(b)
    a = b
T = S[c+1:number+1]
if K <= number:
  print((S[K ]))
else:
  z = K - c-1
  y = z % (number - c)
#   print(y)
  print((T[y]))