peterverebics/CodeNet_Python_118 huggingface dataset:
N, K = map(int, input().split())
A = list(map(lambda x:int(x) - 1, input().split()))

already = [False for i in range(N)]
already[0] = True
tel = [0]

while True:
    tmp = A[tel[-1]]
    tel.append(tmp)
    
    if already[tmp]:
        tel.pop()
        break
        
    already[tmp] = True

for i in range(len(tel)):
    if tel[i] == tmp:
        s = i
        break

ans = (K - s) % (len(tel) - s)
print(tel[ans + s] + 1)
n,k = map(int, input().split())
a = [0] + list(map(int, input().split()))

before=0
roop=0
cnt={}
nxt=1
mp = []
for i in range(1, k+1):
    mp.append(nxt)
    if nxt not in cnt:
        cnt[nxt] = i
        nxt = a[nxt]
    else:
        roop = i - cnt[nxt]
        before = cnt[nxt]-1
        break
    roop=1
    before=0

print(mp[before + (k-before)%roop])
import sys

n, k = map(int, input().split())
ara = list( map(int, input().split()) )

now = 0
vis = []
taken = [0]*n

while taken[now] == 0:
	vis.append(now)
	taken[now] = 1
	now = ara[now]-1

cycleLen = len(vis) - vis.index(now)

if k < vis.index(now):
	print(vis[k])
	sys.exit()

k -= vis.index(now)

k %= cycleLen

print(vis[k + vis.index(now) ] + 1)
n,k=map(int,input().split())
a=[0]+list(map(int,input().split()))


b=[1]
d=set()
d.add(1)
e=1
while n==n:
  if a[e] in d:
    e=a[e]
    break
  b.append(a[e])
  d.add(a[e])
  e=a[e]
print(b[(k-b.index(e))%(len(b)-b.index(e))+b.index(e)])
n,k=map(int,input().split())
a=[int(x) for x in input().rstrip().split()]

l=[0 for i in range(n)]
cnt=0

dist=[]
now=0
while(True):
  
  if l[now]+1==3:
    break
  dist.append(now)

  l[now]+=1
  now=a[now]-1


# k=k-l.count(1)
g=l.count(2)
# print(dist)
# print(l)
print(dist[k%g+l.count(1)]+1)
N, K =map(int, input().split())
A = [int(a) - 1 for a in input().split()]
idx = 0
if K<=N:
    for i in range(K):
        idx = A[idx]
    print(A[idx] + 1)
    exit() #全ての実行をここで終了

t = [-1] * N
t[0] = 0
for i in range(1, N):
    idx = A[idx]
    last = i
    if t[idx] != -1:
        break
    t[idx] = i

d = i - t[idx]
K = K - i
amari = K%d

for i in range(amari):
    idx = A[idx]
print(idx + 1)
NK = list(map(int, input().split()))
N = NK[0]; K = NK[1]
A = list(map(int, input().split()))
 
rlist = [0]*N
flag = 1
location = 1
div = 0
 
while flag>0:
    location = A[location-1]
    if rlist[A[location-1]-1] == 0:
        rlist[A[location-1]-1] = rlist[location-1]+1
    else:
        div = rlist[location-1]+1-rlist[A[location-1]-1]
        flag = -1
 
cnt = (K-rlist[A[location-1]-1])%div + rlist[A[location-1]-1]
 
result = 0
for i in range(cnt):
    result = A[result]-1
 
print(result+1)
n,k = map(int,input().split())
l = list(map(int,input().split()))
curr_town = 1

for i in range(k) :
    temp = l[curr_town - 1]
    curr_town = l[temp - 1]

print(curr_town + 1)
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))

s = [0] * (N+1)
p = 1
h = []
while True:
    h.append(p)
    s[p] = 1
    p = A[p]

    if s[p] != 0:
        break

last = h.index(p)
leng = len(h) - last
rem = (K - last) % leng
print(h[last:][rem])
N, K = map(int, input().split())
A = list(map(int, input().split()))

r = [0]*N

now = 0
r[now]=1
for i in range(N):
  now = A[now]-1
  if r[now] == 0:
    r[now] = i+2
  else:
    x1 = r[now]
    x2 = i+1
    break
print(x1, x2)
print(r)
roop_num = x2 - x1 + 1
x = (K-x1+1) % roop_num
print(x)
if K < x1:
  print(r.index(K+1)+1)
else:
  print(r.index(x1 + x)+1)
n, k = map(int, input().split())
a_list = list(map(int, input().split()))
loop = []
now = 1
while not now in loop:
    loop.append(now)
    now = a_list[now - 1]
loop_start_index = loop.index(now)
print(loop[loop_start_index + (k - loop_start_index) % (len(loop) - loop_start_index)])
n, k = map(int, input().split())
A = list(map(int, input().split()))
order = [-1] * (n+1)
v = [0] * (n+1)
v[0] = 1
order[v[0]] = 0
# iは順番
for i in range(1, n+1):
    v[i] = A[v[i-1] - 1]
    if order[v[i]] != -1:
        old_i = order[v[i]]
        c = i - old_i
        break
    order[v[i]] = i
if k < old_i:
    print(v[k-1])
else:
    print(v[(k - old_i)%c + old_i])
import sys

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

#print(N)
#print(K)

A = sys.stdin.readlines()
#改行コード削除
A = [b.strip() for b in A][0].split()
#print(A)

print(A[int(A[(K % N) - 1]) - 1])
N, K = map(int, input().split())
A = list(map(int, input().split()))
temp = 1
count = 0
ans = 0

dict_num = {}
set_num = set()

for _ in range(K):
  count += 1
  temp = A[temp-1]
  if temp in set_num:
    dict_num[temp] += 1
    if dict_num[temp] == 3:
      break
  else:
    dict_num[temp] = 1
    set_num.add(temp)
    continue
else:
  ans = temp


if count == K:
  pass
else:
  count = count - list(dict_num.values()).count(1) -1
  mod = K % count
  for _ in range(mod + 1):
    temp = A[temp-1]
  else:
    ans = temp
  
print(ans)
n,k = map(int,input().split())
l = list(map(int,input().split()))
position = 1

town = ''
stayed = [1]
stayed_c = [0]*n
for i in range(n):
    position = l[position-1]
    if not position in stayed:
        stayed.append(position)
        stayed_c[position-1] = i+1
    else:
        c = i+1 - stayed_c[position-1]
        g = stayed_c[position-1]
        town = position
        break
m = (k -g) % c 
position = town
for i in range(m):
    position = l[position-1]
print(position)
N, K = map(int, input().split())
A = list(map(int, input().split()))

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

loop_lis = [0]
visited = [0 for _ in range(N)]
visited[0] = 1
loop_time = 0
start_loop = 0
for i in range(K):
  if visited[A[loop_lis[-1]]] != 0:
    loop_time = (i+2)-(visited[A[loop_lis[-1]]])
    start_loop = i+1 - loop_time
    break
  visited[A[loop_lis[-1]]] = i+1
  loop_lis.append(A[loop_lis[-1]])
if len(loop_lis) == K+1:
  print(loop_lis[-1]+1)
else:
  print(loop_lis[start_loop+(K-start_loop)%loop_time]+1)
N,K=map(int, input().split())
A = list(map(int, input().split()))
where = [0 for k in range(N)]
where[0]=1
loop=0
for k in range(N):
    where[k+1] = A[where[k]-1]

    if where[k+1] in where[0:k]:
        li = where[0:k]
        end = k+1
        start = li.index(where[k+1])
        loop = end -start

        break
loopbefore = K -end

loopafter = loopbefore%loop

where2 = [where[end]]
for k in range(loopafter):
    where2.append(A[where2[k]-1])
print(where2[-1])
n,k=map(int,input().split())
l=list(map(int,input().split()))
lcount=[0]*(n+1)
lnum=[0]*(n+1)
count=0
place=1
while lcount[place]==0:
    lnum[place]=count
    lcount[place]=1
    place=l[place-1]
    count+=1
    
dif=count-lnum[place]
k=k%dif+lnum[place]
print(lnum.index(k))
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]-1
    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]-1
    i=i+1

print(current_town+1)
N,K = map(int,input().split())
A = list(map(int,input().split()))
flag = 0
position = [0]
while flag == 0:
  temp = position[-1]
  if A[temp] -1 in position:
    flag = 1
  position.append(A[temp]-1)
for i in range(len(position)):
  if position[i] == position[-1]:
    start = i
    break

x = start + (K - start)%(len(position) - start -1)

print(position[x]+1)
n,k=map(int,input().split())
a=list(map(int,input().split()))
check=[[] for i in range(n)]
c=0
g=1
tmp=[]
cy=[]
non=[]

while c<n:
    if check[g-1]==[]:
        c+=1
        tmp.append(g)
        check[g - 1].append(c)
        g=a[g-1]
    else:
        cy=tmp[(check[g-1][0]-1):]
        non=tmp[:(check[g-1][0]-1)]
        break
k-=len(non)
k%=len(cy)
print(cy[k])
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = [1]
C = [1] + [0] * (N - 1)
i = 0

while C[A[i] - 1] == 0:
    B.append(A[i])
    C[A[i] - 1] = 1
    i = A[i] - 1

ans = 0
for j in range(len(B)):
    if B[j] == A[i]:
        ans += j

if len(B) - ans != 0:
    ans += (K - ans) % (len(B) - ans)

print(B[ans])
n,k=map(int,input().split())
a=[0]+list(map(int,input().split()))
c=[0]*(n+1)
c[1]=1
q=[0,1]
while True:
  i=a[q[-1]]
  if c[i]:break
  c[i]=c[q[-1]]+1
  q+=[i]
k-=c[i]-1
t=c[q[-1]]-c[i]+1
print(q[c[i]+k%(t)])
from collections import deque
import numpy as np
n, k = map(int, input().split())
An = list(map(int, input().split()))
root = deque([1])
check = np.zeros(n, np.int)
check[0] = 1
i = 0
dup = 1
cnt = 1
for _ in range(n):
    temp = An[i]
    if check[temp-1] == 1:
        dup = temp
        break
    root.append(temp)
    check[temp-1] = 1
    i = temp - 1
    cnt += 1
index = root.index(dup)
if index == cnt - 1:
    print(dup)
else:
    print(root[(k-index) % (cnt-index) + index])
N,K=map(int,input().split())
A=list(map(int, input().split()))

def has_duplicates(seq):
    return len(seq) != len(set(seq))

c=[1]
while 1:
    if has_duplicates(c):
        break
    d=c[-1]-1
    c.append(A[d])
p=c.pop()
i=c.index(p)
c_=c[i:]
e=(K-i)%len(c_)
print(c_[e])
import numpy as np

N, K = map(int, input().split())
A = np.array([int(x) for x in input().split()])
B = np.zeros(N)
C = np.zeros(N)
pos = 0
for i in range(N*2):
    B[pos] += 1
    if C[pos] == 0:
        C[pos] = i+1
    pos = A[pos] - 1
ans = K - len(B[B <= 1])
ans %= len(B[B > 1])
D = sorted(C[B > 1])
tmp = D[ans]
index = A[C == tmp]
print(*index)
N,K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N):
    A[i] = A[i] -1
G = 0
lis = [0]*N
lis[0] = 1
roop = []
cnt = 0
while True:
    G = A[G]
    cnt +=1
    lis[G] += 1
    if lis[G] == 2:
        roop.append(G)
    elif lis[G] == 3:
        break

roop_cn = len(roop)
diff = cnt - roop_cn*2
K_n = K - diff
amari = K_n % roop_cn
print(roop[amari]+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt = [1]
jun = 0
i = 0
while len(cnt) == len(set(cnt)):
    cnt.append(a[i])
    i = a[i]-1
x = cnt.pop()
id = cnt.index(x)
k -= id
cnt = cnt[id:]
print(cnt[k%len(cnt)])
N, K = map(int, input().split())
A = list(map(int, input().split()))

visited = [-1] * N
visited[0] = 0
nxt = 1
cnt = 1
for i in range(N):
    nxt = A[nxt-1]
    if visited[nxt-1] == -1:
        visited[nxt-1] = cnt
        cnt += 1
    else:
        cycl = i + 1 - visited[nxt-1]
        m = visited[nxt-1]
        break
print(visited, cycl)

k = (K - m) % cycl + m

for i in range(N):
    if visited[i] == k:
        print(i+1)
        break
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

a = [0] + a
now = 1
cities = [now]
done = set()
for i in range(n):
    now = a[now]
    if now in done:
        break
    done.add(now)
    cities.append(now)
idx_s = cities.index(now)
f = (len(cities) - idx_s)
d = k - idx_s
idx = d%f
ans = cities[idx + idx_s]
print(ans)
n,k = map(int,input().split())
a_list = list(input().split())
dum = a_list[0]
ans_list = ""
for i in range(1,k+1):
    k = k - 1
    ans_list = ans_list+dum
    dum = a_list[int(dum)-1]
    if ans_list.count(dum) == 1:
        break
ind = ans_list.index(dum)
loop = ans_list[ind:]
len_loop = len(loop)
loop_after = (k)%len_loop
print(loop[loop_after-1])
n, k = map(int,input().split())
a = list(map(int,input().split()))

s = 0
seen = [False for _ in range(n)]
d = []
while not seen[s]:
    seen[s] = True
    d.append(s)
    s = a[s]-1

noncyi = d.index(s)
cyi = len(d) - noncyi
if k < noncyi:
    print(d[k-1])
else:
    k -= noncyi
    print(d[noncyi + k%cyi]+1)
n, k = map(int,input().split())
a = list(map(int,input().split()))

kaisuu = k % n

ans = a[kaisuu]
print(a[ans])
n, k = map(int, input().split())
a_ls = list(map(int, input().split()))

my_dict = dict()

ptr = 0
cnt = 0

while True:
    if ptr in my_dict:
        break
    else:
        my_dict[ptr] = cnt
        cnt += 1
        ptr = a_ls[ptr] - 1

loop = cnt - my_dict[ptr]

k = k % loop + loop * (10 ** 6 // loop)

ptr = 0

for i in range(k):
    ptr = a_ls[ptr] - 1

print(ptr + 1)
# coding: utf-8
# Your code here!
n,k=map(int,input().split())

A=list(map(int,input().split()))
L=[1]
now=1
for _ in range(n):
        now=A[now-1]
        if now == L[0]:
                break
        L.append(now)
        
R=k%len(L)
print(L[R])
import sys
readline = sys.stdin.readline
N,K = map(int,readline().split())
A = [0] + list(map(int,readline().split()))
c = 1
while K:
  if K & 1:
    c = A[c]
  A = [A[i] for i in range(len(A))]
  K >>= 1
print(c)
N,K=map(int, input().split())
A = list(map(int, input().split()))

A = [0] + A

visited = [1]
now = 1
while True:
  next = A[now]
  if next in visited:
    break
  visited.append(next)
  now = next

initial = visited.index(next)
cycle = len(visited[initial:])

residual = (K - initial) % cycle
print(visited[initial+residual])
N,K=map(int,input().split())
A=list(map(int,input().split()))
m=1
L=[1]
for i in range(N+2):
  m=A[m-1]
  if m not in L:
    L.append(m)
  else:
    L.append(m)
    break
c=L.index(m)
le=len(L)
#le-cでループ　c,le,2*le-c....
rupe=le-c-1
a=K%rupe
if a<c:
  a+=rupe
print(L[a])
n,k = map(int,input().split())
a = list(map(int,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)

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

print(ans)
n,k=map(int,input().split())
a=list(map(int,input().split()))

edge=[0]*n

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

now=1
to=0
t=k
l=[]
line=0
while t!=0:
    to=edge[now-1]
    t-=1
    if to in l:
        break
    else:
        l.append(to)
        line+=1
    now=to

if t==0:
    print(l[-1])
o=l.index(to)
x=line-o
y=o
cnt=(k-y)%x
print(l[y+cnt-1])
import numpy as np
N,K=map(int,input().split())
A=np.array(list(map(int,input().split())))

Appeared=[]
flag=0
k=0
location=0
T=0
T_List=[]
while flag==0:
    k+=1
    Appeared.append(location)
    location=A[location]-1
    if location in Appeared:
        flag=1
        T=k-Appeared.index(location)
        T_List=Appeared[k-T:k]
Destination=(K-(k-T-1))%T
print(T_List[Destination-1]+1)
N, K = map(int, input().split())
A = list(map(int, input().split()))

pos = 0
kiseki = [0]
while len(kiseki) < N:
    pos = A[pos] - 1
    if pos in kiseki:
        past = kiseki.index(pos)
        kiseki = kiseki[past:]
        print(kiseki[(K - past) % len(kiseki)] + 1)
        exit()
    else:
        kiseki.append(pos)
        # print(kiseki)

print(kiseki[K % len(kiseki)] + 1)
# D

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

cnt = 0
temp = []

d = [0]*(2*(10**5) + 1)

now = 1
while True:
  d[now] += 1
  if d[now] > 1:
    break

  temp.append(now)
  now = A[now-1]

for t in temp[::-1]:
  cnt += 1
  if t == now:
    break



# print(temp)
# print(cnt)
print(temp[K%cnt])
import sys

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

visited = [False] * n
visited[0] = True
l = [1]
while True:
    x = A[l[-1]-1]
    if visited[x-1]:
        break
    l.append(x)
    visited[x-1] = True
# print(x)
# print(l)
# print(l[l.index(x):])
print(l[l.index(x):][(k-l.index(x)) % len(l[l.index(x):])])
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [a - 1 for a in A]
visited = [0] * n

i = 0
lp = 0
step = []
while visited[i] == 0:
  visited[i] = 1
  next_visit = A[i] 
  step.append(i)
  lp += 1
  i = next_visit

lp_start = step.index(i)
syuki = lp - lp_start

if lp_start > k:
  ans = step[k]
else:
  cnt = (k - lp_start) % syuki
  ans = step[lp_start:][cnt] + 1

print(ans)
n,k = map(int,input().split())
a = list(map(int,input().split()))
now = 1
x = 0
ss = set([1])
for i in range(k):
    now = a[now-1]
    ss.add(now)
    if len(ss) != i+2:
        x = 1
        break
if x == 1:
    aa = k%len(ss)
    for i in range(aa):
        now = a[now-1]
print(now)
n,k = map(int,input().split())
li = list(map(int,input().split()))
count = 1
li2 = [1]
li2.append(li[0])
for i in range(k):
    li2.append(li[li2[i+1]-1])
    count += 1
    if len(set(li2)) != count:
        break
print(li2[k % len(li2)])
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [a - 1 for a in A]
L = [None]*(N+1)
s = 0
L[0] = s
index = -1
for i in range(min(N,K)):
  e = i+1
  s = A[s]
  if s not in L:
    L[i+1] = s
  else:
    index = L.index(s)
    break
if index == -1:
  g = K%e
  print(L[g]+1)
else:
  g = index + (K-index)%(e-index)
  print(L[g]+1)
N,K=map(int,input().split())
A=[0]+list(map(int,input().split()))
L=[]
tmp=1
cnt=1
L.append(1)
while True:
  L.append(A[tmp])
  tmp=A[tmp]
  cnt+=1
  #print(cnt,tmp)
  if A[tmp] in L:
    L.append(A[tmp])
    break
#print(L)
res=0
for i in range(len(L)):
  if L[i]!=L[-1]:
    res+=1
  else:
    break
#print(res)
R=L[res:-1]
#print(R)
K-=res
num=K%len(R)
ans=R[num]
print(ans)
import copy

N,K=map(int,input().split())
A=list(map(int,input().split()))
i=A[0]

for _ in range(N+K%N-1):
    i=A[i-1]
print(i)
n,k=map(int,input().split())
a=input().split()
l=[]
for i in a:
	l.append(int(i))
print(l[l[0]-1])
def resolve():
    N, K = map(int, input().split())
    A = tuple(map(int, input().split()))
    path = [1]
    for i in range(2*N):
        cur = A[path[-1]-1]
        path.append(cur)

    if K < N:
        print(path[K])
        exit()

    end = path[N]
    si = N-1
    while path[si] != end:
        si -= 1
    dist = end - si +1
    K = K % dist
    while K < N:
        K += dist

    print(path[K])

if __name__ == '__main__':
    resolve()
n,k=map(int,input().split())
a=[0]
a.extend(list(map(int,input().split())))
loop=[]
now=0
for  i in range(1,n+1):
    if i==1:
        now=a[1]
        loop.append(now)
    else:
        now=a[now]
        if now!=a[1]:
            loop.append(now)
        else:
            break

count=k%len(loop)
if count ==0:
    print(loop[-1])
else:
    print(loop[count-1])
s = input().split()
N = int(s[0])
K = int(s[1])

s2 = input().split()
A = []
for i in range(N):
  A.append(int(s2[i]))
  
now = 1
root = []
root.append(now)

for k in range(N):
  new = A[now-1]
  if new in root:
    Ka = K - root.index(new)
    new_root = root[root.index(new):]
    print(new_root[Ka%len(new_root)])
    break
  else:
    now = new
    root.append(new)
data = input().split()
n = int(data[0])
k = int(data[1])
t = list(map(int,input().split()))

p = t[0]
loop = []
while True:
    loop.append(p)
    p = t[p-1]
    if p in loop:
        repeat = p
        r_point = loop.index(p)
        break
#print(loop)
once = loop[:r_point]
repeat = loop[r_point:]
#print(once,repeat)

k -= len(once)
amari = k%len(repeat)
print(repeat[amari-1])


#6 727202214173249351
#6 5 2 5 3 2
from collections import deque
from sys import stdin


def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    As = [int(x) for x in stdin.readline().rstrip().split()]
    pos = 1
    tmp = deque([pos])
    while True:
        pos = As[pos - 1]
        if pos in tmp:
            idx_s = tmp.index(pos)
            break
        tmp.append(pos)
    K = K - idx_s
    tmp = list(tmp)
    print(tmp[idx_s:][K % (len(tmp) - idx_s)])


if __name__ == "__main__":
    main()
N, K = map(int, input().split())
A = list(map(int, input().split()))

j = 1
lis = [1]
loop = 0
for i in range(K):
    j = A[j-1]
    if not j in lis:
        lis.append(j)
    else:
        n = lis.index(j)
        loop = len(lis) - n
        break

if loop == 0:
    print(j)
else:
    print(A[n + (K - len(lis)) % loop])
n, k = 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
k = (k+n*gap-start) % gap
result = c
for _ in range(k):
    result = a[result]

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

route = [1]
current_town = 1
loop = False
while not loop:
    current_town = A[current_town-1]
    if current_town in route:
        route.append(current_town)
        loop = True
    else:
        route.append(current_town)  
        
start_index = route.index(route[-1])
loop_cnt = len(route) - (start_index+1)
K -= route.index(route[-1])
add_num = K % loop_cnt

print(route[start_index+add_num])
n,k = map(int,input().split())
listA = list(map(int,input().split()))

kouho = [1]
ikisaki = listA[0]
while ikisaki not in kouho:
    kouho.append(ikisaki)
    ikisaki = listA[ikisaki-1]

index = kouho.index(ikisaki)
loop = len(kouho) - index
amari = (k-index)%loop
print(kouho[index + amari])
n,k=map(int,input().split())
a=[int(i)-1 for i in input().split()]
ed=[False]*n
go=0
c=0
cc=0
while not ed[go]:#既に通っていたら終了
    ed[go]=True
    go=a[go]
    c+=1
ed=[False]*n
while not ed[go]:#二回目のループは違う可能性があるため6,5,2,5,3,2の場合、6,2,5,3,2,5,3,2,5,...となる
    ed[go]=True
    go=a[go]
    cc+=1#別変数で保存
for i in range((k-c-cc)%cc):
    go=a[go]
print(go+1)
N, K = map(int, input().split())
A = list(map(int, input().split()))
l = [0 for _ in range(N)]

now = 0
l[now] = 1
count = 0
loop = [0]

while count < K:
    count += 1
    now = A[now]-1
    loop.append(now)
    if l[now] == 1:
        loop = loop[loop.index(now):-1]
        break
    else:
        l[now] = 1

if count == K:
    print(now + 1)
else:
    ans = loop[K % len(loop)]
    print(ans + 1)
n,k = map(int,input().split())
a = list(map(int,input().split()))
m = 1
for i in range(k%n):
  m = a[m-1]
print(m)
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = [1]
C = [1] + [0] * (N - 1)
i = 0

while C[A[i] - 1] == 0:
    B.append(A[i])
    C[A[i] - 1] = 1
    i = A[i] - 1

ans = 0
for j in range(len(B)):
    if B[j] == A[i]:
        ans += j

if (len(B) - ans != 0) and (K - ans > 0):
    ans += (K - ans) % (len(B) - ans)

print(B[ans])
N,K = map(int, input().split())
A = list(map(int, input().split()))

B = [1]
for i in range(N):
    s = B[i]
    if A[s-1] != 1:
        B.append(A[s-1])
    else:
        break
    
l = len(B)
print(B[(K%l)])
N, K = map(int, input().split())
A = list(map(lambda x:int(x) - 1, input().split()))

already = [False for i in range(N)]
already[0] = True
tel = [0]

while True:
    tmp = A[tel[-1]]
    tel.append(tmp)
    
    if already[tmp]:
        tel.pop()
        break
        
    already[tmp] = True

for i in range(len(tel)):
    if tel[i] == tmp:
        s = i
        break
if K - s >= 0:
    ans = (K - s) % (len(tel) - s) 
    print(tel[ans + s] + 1)
else:
    print(tel[K])
n,k  = map(int,input().split())
a =[0]+list(map(int,input().split()))
res= [1]
i = 0

while True:
    
    go = a[res[-1]]
    if i > n+1 or go in res:break
    res.append(go)
    i += 1

for i in range(len(res)):
    if res[i] == go:
        ind = i
v = res[ind:]

m = len(v)

k -= ind
ans = v[k%m]
print(ans)
# coding: utf-8
# Your code here!

n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[1]
i=0
while True:
    i=a[i]
    if(i in b):
        break
    b.append(i)
    i-=1


i=b.index(i)
a=b[:i]
c=b[i:]
#print(a,c)
if(k<=len(a)):
    print(a[k-1])
else:
    k-=len(a)
    k%=len(c)
    print(c[k])
from collections import deque

N,K = map(int,input().split())
telpo = [int(x) for x in input().split()]
now = 1
loop = deque([now])
count = 0

for i in range(K):
    now = telpo[now-1]
    if now in loop:
        while now != loop[0]:
            loop.popleft()
            count += 1
        break
    else:
        loop.append(now)

remain = (K-count)%len(loop)

for i in range(remain):
    now = telpo[now-1]

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

route = [1]
next = A[0]

while(True):
    if next not in route:
        route.append(next)
        next = A[next-1]
    elif A[0] == next:
        break
    else:
        route = route[route.index(next):]
        break

print( A[K % len(route)])
N , K = map(int,input().split())
B = {}
C = {}
D = {0}
A = list(map(int,input().split()))
place = 0
num = 0
C[0] = 0
while True:
    place = int(A[place]) - 1
    num += 1   
    if place in D:
        Y = int(num) - int(C[place])
        break
    elif num == K:
        break
    else:
        B[num] = place
        C[place] = num
        D.add(place)
try:
    X = (K - num) % Y
    print(B[C[place] + X] + 1)
except:
    print(A[place])