peterverebics/CodeNet_Python_118 huggingface dataset:
N, K = map(int, input().split())
a = list(map(int, input().split()))
chk = [0] * N
start = -1
ido = [0]
chk[0] = 1
index = 0
cnt = 0
while True:
    index = a[index] - 1
    if chk[index] == 0:
        chk[index] += 1
        cnt += 1
        ido.append(index)
    else:
        start = index
        break

if cnt >= K:
    print(a[ido[K]])
    exit()
tmp = ido.index(start)
ido = ido[tmp:]
K -= tmp
print(ido[K % len(ido)] + 1)
N, K = map(int, input().split())
A = list(map(int, input().split()))

roop_num = 1
city = 0
first_city = 0

while True:
    next_city = A[city] - 1
    if next_city == first_city:
        break
    if roop_num == K:
        print(city + 1)
        exit()
    city = next_city
    roop_num += 1

rem_num = K % roop_num
city = 0
num = 0

while True:
    next_city = A[city] - 1
    if num == rem_num:
        print(city + 1)
        exit()
    city = next_city
    num += 1
#N=int(input())
N,K = map(int,input().split())
#N=int(input())
A = list(map(int,input().split()))
#S=str(input())

now=1
root=[1]
for i in range(N):
    now=A[now-1]
    if now in root:
        index=root.index(now)
        num=len(root[index:])
        root=root[index:]
        break
    else:
        root.append(now)

an = (K-i) - (K-i)//num * num
print(root[an-1])
N, K = map(int, input().split())
A = list(map(int, input().split()))

ans = 0
ans_list = []
end = 0
stpos = 0


for i in range(K):
    ans = A[ans] - 1
    ans_list.append(ans)
    if len(set(ans_list)) != i+1:
        end = i - 1
        stpos = ans_list.index(ans)
        break
    if i == K:
        print(ans)
        exit()


print(ans_list[stpos:end+1][(K-(stpos+1)) % (end-stpos+1)]+1)
#D - Teleporter
N,K = map(int, input().split())
A = list(map(int, input().split()))
DP = []
DP.append(A[0])
for i in range(N):
    DP.append(A[DP[i]-1])

print(DP[K%len(DP)-1])
N,K=map(int,input().split())
a=list(map(int,input().split()))
cycle=[0]
s_cyc=set()
s_cyc.add(0)
flag=0
id=0
for i in range(N):
    a[i]-=1
while N>0:
    if a[id] in s_cyc:
        node=cycle.index(a[id])
        break
    else:
        cycle.append(a[id])
        s_cyc.add(a[id])
        id=a[id]
    N-=1
if N==0:
    print(a[id]+1)
else:
    K-=node
    cycle=cycle[node::]
    q=K%len(cycle)
    print(cycle[q]+1)
import numpy as np
import math 
N, K=map(int,input().split())
A2 = np.zeros(N)
A = list(map(int,input().split()))
A = list(map(lambda x: x -1 , A))
current_town = 0



i=0
while i<K:
    current_town = A[current_town]
    i=i+1
    if  A2[current_town] != 0:
        cycle = i - A2[current_town] 
        i = K - (K-i) % cycle
        break
    else:
        A2[current_town] = i


while i<K:
    current_town = A[current_town]
    i=i+1

print(current_town+1)
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

#loop check
gone = []
pos = 0
i = 0
while pos not in gone:
    gone.append(pos)
    pos = A[pos] - 1
    i += 1
    if (pos in gone):
        loop_flag = True
        loop_pos = gone.index(pos)
        loop_len = len(gone)-loop_pos



if (loop_flag):
    pos = loop_pos
    for i in range(((K - loop_pos) % loop_len)-1):
        pos = A[pos]-1
    
else:
    pos = 0
    for i in range(K):
        pos = A[pos]-1

print(pos+1)
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A = [i-1 for i in A]

loc = 0
history = []

for current_idx in range(K):
    loc = A[loc]
    if loc in history:
        hist_idx = history.index(loc)
        syuuki = current_idx - hist_idx
        loc = history[hist_idx + ((K - current_idx) % syuuki)]
        break
    history.append(loc)
        
print(loc+1)
N,K=map(int,input().split())
A=list(map(int,input().split()))

C=[1]
p=0

for i in range(N):
    C.append(A[C[i]-1])
    p+=1
    if C[i+1]==1:
        break

print(C[K%p])
from math import log2
n, k = map(int, input().split())
To = []
A = list(map(int, input().split()))
A = list(map(lambda x: x - 1, A))
To.append(A)
for i in range(int(log2(k))):
    N = [0 for i in range(n)]
    for j in range(n):
        N[j] = To[i][To[i][j]]
    To.append(N)

v = 0
for i in reversed(range(int(log2(k)) + 1)):
    l = 1 << i
    print(i)
    if l <= k:
        v = To[i][v]
        k -= l
    else:
        pass

print(v + 1)
import itertools
n,k=map(int,input().split())
a=list(map(int,input().split()))
pas=[1]
p=1
for i in range(n):
	p=a[p-1]
	pas.append(p)
	if len(pas)!=len(set(pas)):
		nloop=pas.index(p)
		loop=len(pas)-1-nloop
		break
last=(k-nloop)%loop
print(pas[nloop+last])
N, K = [int(i) for i in input().split()]
A = [0] + [int(i) for i in input().split()]
B = [1]
while(True):
    tmp = A[B[-1]]
    if not tmp in B:
        B += [tmp]
    else:
        head = B.index(tmp)
        break
K = (K - len(B[:head]))  % len(B[head:])
print(B[head:][K])
n,k = map(int,input().split())
L = list(map(int,input().split()))
L2 = []

t = 1
r = 0
i = 0
c = 0
for j in range(n*2):
    #print(L2)
    t = L[t-1]
    if ｔ in L2:
        i = L2.index(t)
        r = len(L2) - i
        break
    else:
        L2.append(t)
#print(r)
#print(i)

k = (k - i)%r
#print(k)
print(L2[i+k-1])
paramN, paramK = list(map(lambda x: int(x), input().split(' ')))
paramA = list(map(lambda x: int(x), input().split(' ')))

route = [0] * (paramN + 1)
route[0] = 1
for i in range(1, paramN + 1):
    route[i] = paramA[route[i - 1] - 1]

loop_start = 0
history = [False] * paramN
for i, r in enumerate(route):
    if history[r - 1]:
        loop_start = i
        break
    else:
        history[r - 1] = True

x = route[loop_start]
loop_start_0 = route.index(x)

y = paramK - loop_start_0
z = y % (loop_start - loop_start_0)
print(route[loop_start_0 + z])
from collections import OrderedDict

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

returned = None
i = 0
aa = 0
visited = OrderedDict([(0, 0)])
while i < n:
    aa = a[aa] - 1
    i = i + 1
    if aa in visited:
        returned = (visited[aa], i)
        break
    visited[aa] = i

loop = returned[1] - returned[0]

if k >= len(visited):
    k = k % (returned[1] - returned[0])
    if k < returned[0]:
        k = k + returned[0]

print(list(visited.keys())[k] + 1)
n,k=map(int,input().split())
A=list(map(int,input().split()))
A=[0]+A
B=[]
C=[0]+[-1]*n
now_city=1
seen=set()

#【ループ回数】200000
for i in range(1,len(A)):
    print("Baah")
from collections import deque, defaultdict
d = deque()
di = defaultdict(int)
N, K = map(int, input().split())
A = list(map(int, input().split()))


d.append(0)
for i in range(N * 2):
    p = d.pop()
    d.append(p)
    d.append(A[p] - 1)


P = []
k = 0
n = 0
for i, v in enumerate(d):
    if (v in P):
        k = i
        n = v
        break
    P.append(v)
    di[v] = i

le = k - di[n]
amari = K % le

print(d[le + amari] + 1)
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = 1
c = [-1]*n
c[0] = 0
for i in range(n):
    b = a[b-1]
    if c[b-1] == -1:
        c[b-1] = i+1
    else:
        d = i+1-c[b-1]
        break;
b = 1
for i in range(c[b-1]+k%d):
    b = a[b-1]
print(b)
N, K = map(int, input().split())
A = list(map(int, input().split()))

now = 1
temp_l = [0]*N
temp_count = [-1]*N
temp_count[0] = 0
for i in range(N):
    pre = now
    now = A[pre-1]
    if temp_l[now-1] == 1:
        end = now
        temp = i+1
        break
    else:
        temp_l[now-1] = 1
        temp_count[now-1] = i+1

s_index = temp_count[end-1]
repeat = temp - s_index
K -= s_index
K = K % repeat

print(temp_count.index(K+s_index)+1)
N,K= map(int, input().split())
A = list(map(int,input().split()))
if(A[0] == 1):
    print(1)
    exit(0)
p = 1
P = []
start,stop = 0, 0
for i in range(N):
    p = A[p-1]
    if p in P:
        start = P.index(p)
        break
    P.append(p)
stop = len(P)-1
if len(P) == K:
    print(B[-1])
else:
    del P[0:start]
    Z = (K-start-1)%((stop-start+1))
    print(P[Z])
N, K = map(int, input().split())
As = list(map(int, input().split()))

cycle = []
pos = 1
for i in range(N):
    cycle.append(pos)
    pos = As[pos-1]
    if pos == 1:
        break

#print(cycle)

mod = K % len(cycle)
print(cycle[mod])
N,K=map(int, input().split())
A=list(map(int, input().split()))
sumi=[0]*N
root=[0]*N
root[0]=1
k=0
idx=0
for i in range(1,N+1):
    chk=root[i-1]-1
    nxt=A[chk]
    if sumi[chk]==0:
        root[i]=nxt
        sumi[chk]=1
    elif sumi[chk]!=0:
        k=i-1
        break
idx1=root.index(nxt)
ans=root[idx1:k+1]
print(ans[(K-(idx1-1))%len(ans)-1])
def main():
    n, k = map(int, input().split())
    a = tuple(map(int, input().split()))
    l = []
    t = 1
    for i in range(n):
        l.append(t)
        if a[t - 1] in l:
            k -= (i+1)
            break
        t = a[t - 1]
    i = l.index(a[t - 1])
    k = k % (len(l) - i)
    print(l[k + i])


if __name__ == '__main__':
    main()
n, k = map(int, input().split())
l = [int(i) for i in input().split()]
check = [0]*n
count = 0
now = 1
mod = 0
start = 0
loop = []
while True:
  count += 1
  if check[now-1] == 0:
    check[now-1] += count
    loop.append(now)
    now = l[now-1]
  else:
    mod = count-check[now-1]
    loop = loop[check[now-1]-1:]
    start = check[now-1]-1
    break
x = (k-start)%mod
print(loop[x])
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
 
def func1(lst, value):
    return [i for i, x in enumerate(lst) if x == value]
  
temp=1
trace=[1]
circle=[]
for i in range(K):
  temp=A[temp-1]
  ans=func1(trace,temp)
  if len(ans)==1:
    circle.append(i-ans[0]+1)
    #print(circle)
    break
  trace.append(temp)
  #print("temp",temp)
#print((K-i)%len(circle)-1)
if len(circle)!=0:
  amari=(K-i)%circle[0]
  for j in range(i+amari):
    temp=A[temp-1]

print(temp)
N, K = map(int, input().split())
where_go = list(map(int, input().split()))
now = 0
now_list = []
start = 0
roop = 0
count = 0

while now not in now_list:
    now_list.append(now)
    now = where_go[now] -  1

start = now_list.index(now)
roop = len(now_list) - start



while count < (K - start) % roop:
    now = where_go[now] -  1
    count += 1

print(now+1)
N,K= list(map(int , input().split()))
A = list(map(int , input().split()))
li = []
k = 0
s = A[0]-1
li.append(A[0])
while k == 0:
    k = li.count(A[s])
    li.append(A[s])
    s = A[s]-1
    if k == 1:
        n = A[s]
q = li.index(n)
m = len(li) - q
p = (K - q)%m
ss = 0
for i in range (p+ q-1):
    ss = A[ss]-1
print(A[ss])
N,K=map(int,input().split())
P=list(map(int,input().split()))

A={k+1:P[k] for k in range(N)}


U=list()
x=1
k=0

while (x not in U) and (k<K):
    U.append(x)
    x=A[x]
    k+=1

if k==K:
    print(U[-1])
else:
    d=U.index(x)
    V=U[d:]
    print(V[(K-d)%len(V)])
n,k=map(int,input().split())
pl=list(map(int,input().split()))
cl=[0]*2*n
order=[]
next=0
for i in range(k):
  if cl[next]==0:
    cl[next]=1
    next=pl[next]-1
  elif cl[next]==1:
    cl[next]=2
    order.append(pl[next])
    next=pl[next]-1
  else:
    kk=len(order)
    hasuu=cl.count(1)
    newk=k-hasuu
    ans=newk%kk
    print(order[ans])
    break
else:
  print(pl[next])
#!/usr/bin/env python3
from collections import defaultdict
N, K = map(int, input().split())
A = list(map(int, input().split()))

dst = defaultdict(int)

for i, a in enumerate(A, 1):
    dst[i] = a

done = [0] * (N + 1)
done[1] = 1
hist = [1]

now = 1
cnt = 0
while True:
    now = dst[now]
    cnt += 1
    if done[now] == 0:
        done[now] = 1
        hist.append(now)
    else:
        syuki = len(hist) - hist.index(now)
        print(hist[hist.index(now) + (K - cnt) % syuki])
        exit()
N, K = map(int, input().split())
A = list(map(int, input().split()))
cnt = 0
visited = []
next = 1
while True:
  present = next
  cnt += 1
  if cnt == K:
    print(present)
    break

  visited.append(present)
  next = A[present-1]
  if next in visited:
    ind = visited.index(next)
    l = len(visited[ind:])
    break

if cnt != K:
  rest = (K-cnt) % l
  present = visited[ind + rest]
  print(present)
b0=input().split()
towns=int(b0[0])
k=int(b0[1])

al=[int(i)-1 for i in input().split()]

rt=[-1 for x in range(towns)]
rt[0]=0
now=0
for x in range(1,300000):
  rec=now
  now=al[now]
  if rt[now]!=-1:
    stp=x
    break
  else:
    rt[now]=x

ll=stp-rt[now]
k2=k-rt[now]
print(rt.index(k2%ll+rt[now])+1)
N, K = map(int, input().split())
A = list(map(int,input().split()))
B = []

count= 0
now=1
flag = 0
if(K == 0):
  print(1)
else:
  while(True):
    now = A[now-1]
    for i in range(len(B)):
      if(now == B[i]):
        flag = 1
        count = i
        break
    if(flag == 1):
      break
    B.append(now)
  print(B[(K-count)%(len(B)-count)+count-1])
n, k = map(int, input().split())
a = list(map(int, input().split()))
m = [-1]*n
t = 0
i = 0
while True:
  if m[t] != -1:
    r = i - m[t]
    break
  m[t] = i
  t = a[t]-1
  i += 1
b = k%r
for i in range(n):
  if m[i] == b:
    print(i+1)
    break
N, K = map(int,input().split())
A = list(map(int,input().split()))

if N == 1:
    print(A[0])
else:
    loop = 1
    des = 0
    while des != A[-1]:
        des = A[des-1]
        loop += 1

    print(A[K % loop + 1])
from sys import stdin

def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    A = [int(x) for x in stdin.readline().rstrip().split()]
    
    X=1
    if N < K :
    	K = K % N
    for i in range(K):
        X = A[X-1]
    
    print(X)
    
main()
n,k = map(int,input().split())
a = list(map(int,input().split()))
ls = [1,a[0]]
for i in range(20):
    ls.append(a[ls[-1]-1])
print(ls[-2])
n, k = map(int, input().split())
A = [0] + list(map(int, input().split()))
now = 1
used = [0] * (n + 191)
used[1] = 1
cn = -292
for _ in range(k):
    now = A[now]
    if used[now] != 0:
        cn = _+1-used[now]+1
        break
    used[now] = _ + 1
if cn == -292:
    print(now)
    exit(0)
for i in range(k % cn):
    now = A[now]
print(now)
n, k = map(int, input().split())
A = list(map(int, input().split()))
doubleA = {}
doubleA[1] = A
for i in range(2, 73):
    doubleA[i] = [doubleA[i-1][doubleA[i-1][j]-1] for j in range(n)]
now = 1
for i in range(len(bin(k))-3):
    if (k>>i)&1:
        now = doubleA[i+1][now-1]
print(now)
N,K = [int(i) for i in input().split()]
A = [int(i)-1 for i in input().split()]

place = (0,)
now = 0
length = 1
while True:
    length += 1
    now = A[now]
    place = place + (now,)
    place_set = set(place)
    if length != len(place_set):
        break
    

t = place.index(place[length-1])
if K <= t:
    print(place[K-1]+1)
    exit()
K -= t
K %= (length-t-1)
print(place[K+t]+1)
n, k = map(int, input().split())

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

b = a[0]-1
c = 1

while c < n:   
    b = a[b]-1
    c = c+1
    if b == 0:
        break
g = k%c

d = a[0]-1
i = 1
while i<g:
    d = a[d] - 1
    i = i+1
    
print(d+1)
n,k = map(int,input().split())
A = list(map(int,input().split()))
B=[1]
m=0
j=0
for i in range(1,n):
    B.append(A[m])
    m=A[m]-1
    if B[i] in B[:i]:
        while B[j]!=B[i]:
            j+=1
        break
T=len(B)-j-1
print(B[k-((k-j)//T)*T])
N,K=map(int, input().split())
A = list(map(int, input().split()))

A0=list(range(N))
temp=0

for i in range(N):
  temp=A[i]
n,k = map(int, input().split())
a = list(map(int, input().split()))

tele = [1]
went = set(tele)
now = 1
cnt = 0
flag = True

for i in range(k):
  now = a[now-1]
  if now in went:
    tele = tele[tele.index(now):]
    cnt = len(tele)
    flag = False
    break
  tele.append(now)
  went.add(now)

if flag == True:
  print(tele[-1])

else:
  cnt = k % cnt
  print(tele[cnt])

print(tele)
n,k=list(map(int,input().split()))
A=list(map(int,input().split()))
start,i=1,0
root=[1]

while True:
  start=A[start-1]
  if start in root:
    i = start
    break
  root.append(start)
  
if k <= len(root):
  print(root[k-1])
else:
  s=root.index(i)
  newroot=root[s:]
  ans=(k-len(root))%len(newroot)
  print(newroot[ans])
n,k = map(int,input().split())
a = [int(i) for i in input().split()]
tmp=0
tmplist=[0]
#loopしてない判定
loopinter=n
loopstart=0
for i in range(k):
    tmp=a[tmp]-1
    if tmp not in tmplist:
        tmplist.append(tmp)
    else:
        loopstart = tmplist.index(tmp)
        loopinter = len(tmplist)-tmplist.index(tmp)
        break
for i in range((k-loopstart)%loopinter):
    tmp=a[tmp]-1
print(tmp+1)
# coding: utf-8
# Your code here!
N, K = input().split(' ')
A = list(map(int, input().split(' ')))
count = 0
i = 0
a = []
while True:
    a.append(i + 1)
    i = A[i] - 1
   
    if len(a) != len(set(a)):
        break
    count += 1

b = count - a.index(a[count])

K = int(K) - a.index(a[count])

K = K % b

while K >= 0:
    i = A[i] - 1
    K -= 1
print(A[i])
n,k = map(int,input().split())
a_list = list(map(int,input().split()))
visited = []
i,j = 0,1

while i<len(a_list)+10:
    tmp = a_list[j-1]
    if (j,tmp) not in visited:
        visited.append((j,tmp))
        j = tmp
        i += 1
    else:
        break
        
start = visited.index((j,tmp))
loop_len = len(visited) - start
not_loop_len = start
finish = (k -not_loop_len)%loop_len

print(a_list[start+finish])
n,k = (int(x) for x in input().split())
town = [int(i) for i in input().split()]
warp  = [1]
checklist = [0]+[-1]*n
i = 0
j = 1
s = 0
while j<=n:
    warp.append(town[i])
    if checklist[town[i]-1]!=-1:
        s = checklist[town[i]-1]
        warp = warp[s:j]
        break
    checklist[town[i]-1] = j
    i = town[i]-1
    j += 1


m = len(warp)
print(warp[(k-s)%m])
def main():
	n, k = [int(x) for x in input().split(" ")]
	a = [int(x) for x in input().split(" ")]
	d = {}
	pos = 0
	step = 0
	cycle = False
	way = []
	while step < k and cycle == False:
		if pos in d:
			cycle = True
			step += 1
		else:
			d[pos] = step
			way.append(a[pos])
			pos = a[pos] - 1
			step += 1

	k -= step
	if k > 0:
		print(k)
		cycle = way[d[pos]:step]
		x = k % len(cycle)
		print(cycle[x])
	else:
		print(a[pos])

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

limit = K
now = 1
log = []
count = 1

while limit > 0 and now not in log:
    if 1 not in log:
        log.append(1)
    #print("通過1",now,limit,log)
    now = A[now - 1]
    log.append(now)
    limit -= 1
    count += 1

if limit > 0:
    #print("通過2",now,limit,log)
    log2 = log[log.index(now):]
    limit = limit % len(log2)

while limit > 0:
    #print("通過3",now)
    now = A[now - 1]
    limit -= 1

now = A[now - 1]

print(now)
n, k = map(int, input().split())
a_list = list(map(int, input().split()))
route = []
roop = []

pos = 1
while k > 0:
    pos = a_list[pos-1]
    if pos in route:
        if len(roop) > 0 and pos == roop[0]:
            k -= k % max(1, (k * (k // len(roop))))

        roop.append(pos)
    else:
        roop = []

    route.append(pos)
    k -= 1

print(pos)
N, K = map(int, input().split())
A = list(map(int, input().split()))
now = 1
count = 0

now = A[now-1]
route = [now]
count += 1

def check(now, route, count):
  now = A[now-1]
  if now in route:
    return 0
  else:
    route.append(now)
    count += 1

check(now, route, count)

print(route[(K % count)-1])
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
a.insert(0,0)
start = 1
now = start
c = 0
flag = [-1]*(n+1)
while c <= k and flag[now] == -1:
    #print(now)
    flag[now] = c
    c+= 1
    now = a[now]
#print(c, now)
if c == k:
    print(now)
else:
    k-=c
    cc = c-flag[now]
    nokori = k % cc
    for i in range(nokori):
        now = a[now]
    print(now)
from sys import stdin
input = stdin.readline

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

graph = []

time = [-1] * N

for i in range(N):
    graph.append([i, A[i]])

now = 0
nex = 0
time[0] = 0
loop = 0

while True:
    now = nex
    nex = A[now] - 1

    if time[nex] == -1:
        time[nex] = time[now] + 1
    else:
        loop = nex
        break

if K > max(time):
    K %= (max(time) - time[loop] + 1)

for i in range(N):
    if time[i] == K:
        print(i + 1)
N, K = map(int,input().split())
A = list(map(int, input().split()))
for i in range(N) :
    if A[i] == 1 :
        break
i += 1
print(A[K%i])
n,k=map(int,input().split())
a=[0]+list(map(int,input().split()))
pointer=1
visited=[1]
for i in range(n):
    if a[pointer] in visited:
        loopstart=visited.index(a[pointer])
        looplength=len(visited)-visited.index(a[pointer])
        break
    else:
        visited.append(a[pointer])
        pointer = a[pointer]
        
print(visited[loopstart+(k-loopstart)%looplength])
n, k = map(int, input().split())
list_A = list(map(int, input().split()))
list_MAP = []
set_MAP = set()
x = 1
while True:
    list_MAP.append(x)
    set_MAP.add(x)
    x = list_A[x-1]
    if x in set_MAP:
        break


q = len(list_MAP) - list_MAP.index(x)
p = k - list_MAP.index(x)

if p < 0:
    print(list_MAP[k-1])
else:
    list_MAP = list_MAP[list_MAP.index(x):]
    print(list_MAP[p % q])
import sys
input = sys.stdin.readline
def main():
  N,M=map(int,input().split())
  L=list(map(int,input().split()))
  i=1
  R=list()
  for j in range(N+1):
    if L[i-1] in R:
      c=R.index(L[i-1])
      t=len(R)-c
      m=R[c:]
      break
    R.append(L[i-1])
    i=L[i-1]
  l=(M-c-1)%t
  print(m[l])

if __name__ == '__main__':
    main()
n, m = map(int, input().split())
a = list(map(int, input().split()))
a = [a[i]-1 for i in range(n)]
x = a[a[0]]
l = [0, a[0]]
cnt = 0
while x not in l:
    l.append(x)
    x = a[x]
cnt = len(l)-l.index(x)
loop = l[l.index(x):l.index(x)+cnt]
print(loop[(m-l.index(x)+1)%cnt-1]+1)
N,K=map(int,input().split())
A=list(map(int,input().split()))
done=[0 for _ in range(N)]
done[0]=1
W=[0]

init=0
loop=N

for i in range(N):
    if done[A[i]-1]==2:
        init=W.index(A[i]-1)
        loop=i-init
        break
    elif W[-1]==A[W[-1]]-1:
        init=i
        loop=1
        break
    done[W[-1]]+=1
    W.append(A[W[-1]]-1)

if K>=init:
    print(W[(K-init)%loop+init]+1)
else:
    print(W[K]+1)
N,K = map(int,input().split())
A = list(map(int,input().split()))
if A[0] == 1:
    print(1)
    exit()
B = [A[0]]
num = 0
C = 0
for i in range(N):
    num = A[num-1]
    B.append(num)
    if num in B:
        c = num
        break
C = [c]
num = c
for i in range(N):
    num = A[num-1]
    if num == c:
        break
    C.append(num)
if K <= len(B):
    print(B[K-1])
    exit()
K -= len(B)
print(C[K%len(C)-1])
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))
cities = [0]
c = 0
used = [False] * N
used[0] = True
while not used[A[c]]:
    used[A[c]] = True
    cities.append(A[c])
    c = A[c]
x = cities.index(A[c])
if K < x:
    print(cities[K])
else:
    K -= x
    K %= len(cities) - x
    print(cities[x + K] + 1)
n, k = map(int, input().split())
a = list(map(int, input().split()))

l = [0]*n
s = set()
x = 0

for i in range(n):
    machi = a[x]
    if machi in s:
        hajime = l.index(machi)
        repeat = l[hajime:i]
        break
    x = machi-1
    l[i] = machi
    s.add(machi)
else:
    amari = k%n
    print(l[amari-1])
    exit()

amari = (k-hajime)%len(repeat)
if hajime + amari == 0:
    print(repeat[-1])
else:
    print(l[hajime+amari-1])
n,k = map(int,input().split())
A = list(map(int,input().split()))
sub = [0]*n
city = 0
all = []
roop = []
while sub[city] != 2:
  if sub[city] == 0:
    all.append(city)
  else:
    roop.append(city)
  sub[city] += 1
  city = A[city] -1
if len(roop)<=k:
  print(roop[(k-(len(all)-len(roop)))%len(roop)]+1)
else:
  print(all[k]+1)
import sys
lines = sys.stdin.readlines()
(n, k) = map(int, lines[0].strip().split(" "))
nums = list(map(int, lines[1].strip().split(" ")))
pt = 0
seen = set()
while pt not in seen:
  seen.add(pt)
  pt = nums[pt] - 1
  k -= 1

cyc = 1
entry = pt
pt = nums[pt]-1
while pt != entry:
  cyc += 1
  pt = nums[pt] -1
k %= cyc
for i in range(k):
  pt = nums[pt] -1 
print(pt+1)
import sys
input = sys.stdin.readline

n, k = (int(x) for x in input().split())
seq = [int(x) for x in input().split()]
visited = [[False,0] for _ in xrange(n)]

t = 1
cnt = 0
while not visited[t-1][0]:
    visited[t-1][0] = True
    visited[t-1][1] = cnt
    t = seq[t-1]
    cnt += 1

cycle_len = cnt - visited[t-1][1]
for _ in xrange((k-visited[t-1][1]) % cycle_len):
    t = seq[t-1]
print(t)
n, k = map(int, input().split())
A = list(map(int, input().split()))
now = 1
 
for i in range(len(bin(k))-2):
    if (k>>i)&1:
        now = A[now-1]
    A = [A[a] for a in range(n)]
print(now)