peterverebics/CodeNet_Python_118 huggingface dataset:
import sys
N, K = map(int, input().split())
A = list(map(int, input().split()))
if K == 1:
    print(A[0])
    sys.exit()
now = A[0]
kiseki = [1]
count = 0
while now not in kiseki:
    kiseki.append(now)
    count += 1
    now = A[now - 1]
    if count == K:
        print(now)
        sys.exit()
ind = kiseki.index(now)
roop = len(kiseki) - ind
nokori = (K - ind) % roop
ans = kiseki[ind + nokori]
print(ans)
n,k=map(int,input().split())
L=list(map(int,input().split()))
L.insert(0,0)
#print(L)
visit=[0]*(n+1)
visit[L[0]]=1
prev=L[1]
count=0

for i in range(1,k):
	nex = L[prev]
	if visit[nex]==0:
		visit[nex]=1
	else:
		count+=i
		val=nex
		#print(i)
		#print(prev)
		break
	prev = nex
	
k-=count
k = k%(i-1)
#print(k)
for i in range(k):
	nex=L[prev]
	prev=nex
print(prev)
n, k = map(int,input().split())
a = list(map(int, input().split()))

D = 60
MAX_N = 200005
to = [[0]*MAX_N]*D
# to = [[0]*D]*MAX_N
# to = [D][MAX_N]

for i in range(n):
    to[0][i] = a[i]-1
    print(a[i]-1)
for i in range(D-1):
    for j in range(n):
        to[i+1][j] = to[i][to[i][j]]

v = 0
for i in range(D-1):
    l = 1<<i
    if (l <= k):
        v = to[i][v]
        k -= l
    # print(i)
    
print(v+1)
numOfCity, numOfTeleport = map(int, input().split())
A = list(map(int, input().split()))

cityIndex = 0

while numOfTeleport > 0:
    cityIndex = A[A[cityIndex] - 1] - 1
    numOfTeleport -= 1

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

l_set = set()
tele_list = [0]
current_city = 0


for i in range(N):
    if current_city in l_set:
        break

    

    else:
        current_city = A_list[current_city] - 1

        tele_list.append(current_city)
        l_set.add(current_city)

len_tele_list = len(tele_list)
index_last = len_tele_list % K - 1

print(tele_list[index_last] + 1)
n,k=map(int,input().split())

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

a.insert(0,0)

path=[1]

ans=0
s=1

while ans==0:
    if a[s] not in path:
        path.append(a[s])
        s=a[s]
        k-=1
        
    else:
        junkan=path[path.index(a[s]):]
        x=len(junkan)
        ans=1
        k-=1
        
  
m=k%x

print(junkan[m])
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
d = {}
for i in range(N):
    d[i+1] = A[i]
    
al = [1]
for i in range(N):
    ne = d[al[-1]]
    if ne in al:
        tar = al.index(ne)+1
        break
    al.append(ne)
    
ne = i+2
if K<=len(al):
    print(al[K-1])
else:
    K-=(tar-1)
    K%=(ne-tar)
    print(al[tar-1+K])
n, k = map(int, input().split())
a_list = list(map(int, input().split()))
route = []

now = 1
# next = a_list[now-1]

while True:
    if now in route:
        start = route.index(now)
        end = len(route)
        junkai = route[start:end]
        amari = route[:start]
        break
    route.append(now)
    now = a_list[now-1]
    # next = a_list[now-1]
    
hoge = (k-len(amari))%len(junkai)
print(junkai[hoge])
N, K = map(int,input().split())
A = list(map(int,input().split()))

count = 0
i = 1
position = 1
position_list = [1]
position_history = [0] * N
start_loop = 0
while i > 0:
  position = A[position-1]
  position_history[position-1] += 1
  count += 1
  if position_history[position-1] > 1:
    start_loop = position_list.index(position)
    position_list.append(position)
    break
  else:
    position_list.append(position)
  	
if count >= K:
  print(position_list[K-1])
else:
  k = (K - start_loop) % (count - start_loop)
  print(position_list[start_loop+k])
N,K= map(int, input().split())
flags = [-1]*N
M = 1
mods = []
mods.append(0)
A = list(map(int,input().split()))
now = 0
I = 0
flags[0] = 0
for i in range(N):
    now = A[now]-1
    
    
    if flags[now]!=-1:
        I = i
        M=i+1-flags[now]
        break
    mods.append(now)
    flags[now] = i+1

I += 1
alt = mods[I-M:]

if K-I<=0:
    print(now+1)
else:
    print(alt[(K-I)%(len(alt))]+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))

a.insert(0, 'hoge')
# a = dict(list(enumerate(a)))
amari = k % n

tmp = 1
# print(amari)
for i in range(1, n + amari+1):
    # print(tmp)
    tmp = a[tmp]

print(tmp)
n,k=map(int,input().split())
ar=list(map(int,input().split()))
vis=[0 for i in range(n+1)]
x=1
seq=[1]
go=0
rep=0
ct=1
while (vis[x]==0):
    vis[x]=ct
    seq.append(ar[x-1])
    x=ar[x-1]
    ct+=1
    if vis[x]!=0:
        rep=ct-vis[x]
        go=vis[x]
if k<=go:
    print(seq[k-1])
else:
    print(seq[go+(k-go)%rep])
N, K = map(int, input().split())
A = list(map(int, input().split()))
li = [False] * N
f = True
now = 0
l = []
l2 = [0] * N
i = 0
while f:
    l.append(now)
    li[now] = True
    l2[now] = i
    if li[A[now]-1]:
        f = False
        num = len(l) - l2[A[now]-1]
    i += 1
    now = A[now]-1
K %= num
print(l[K]+1)
N,K=map(int,input().split())
A=list(map(int,input().split()))
f=[0]*N
l,r=[],[]
p=0
while f[p]<2:
    if f[p]:
        r.append(A[p])
        K-=1
    else:
        l.append(A[p])
        K+=1
    f[p]+=1
    p=A[p]-1
print(r[K%len(r)-1])
N, K = map(int, input().split())
A = list(map(int,input().split()))
T = 1
cnt = 0
Tlist = []
for k in range(K):
    cnt += 1
    T = A[T-1]
    if T in Tlist:
        Tlist.append(T)
        break
    Tlist.append(T)
for i in range(len(Tlist)):
    if Tlist[i] == Tlist[-1]:
        lp = len(Tlist)-i
        break

if k == K-1:
    print(T)
else:
    for k in range((K-i)%lp):
        T = A[T-1]
    print(T)
import collections as cl
N, K = map(int, input().split())
A = list(map(int, input().split()))

i = 0
telp = [1]
while True:
    if A[i] not in telp:
        telp.append(A[i])
        i = A[i] - 1
    else:
        telp.append(A[i])
        break

c = cl.Counter(telp)
lll = telp.index(c.most_common()[0][0])

K -= lll
ans = (K % len(telp[lll + 1:]))
print(telp[ans + lll])
n, k = map(int, input().split())
a = list(map(int, input().split()))
visited = []
now = 0
lim = 2 * 10**5 + 1
for _ in range(n+1000):
    visited.append(now)
    now = a[visited[-1]]-1
    if now in visited:
        break
visited.append(now)

ln1 = len(visited)-1
idx = visited.index(now)
ln2 = len(visited[idx:-1])
ln = (k-ln1)%ln2

print(visited[idx+ln]+1)
n, k = [int(i) for i in input().split()]
towns = [int(i) for i in input().split()]
ind_ = 1
ts = []
visited = [0]*n
loop_s = None
while(True):
    ind_ = towns[ind_-1]
    if visited[ind_-1]:
        loop_s = ts.index(ind_)
        break
    else:
        visited[ind_-1] = 1
        ts.append(ind_)
init = len(ts[:loop_s])
loop_l = len(ts) - init
loop = ts[loop_s:]
print(loop[((k-init)%loop_l)-1])
n, k = map(int, input().split())
a = list(map(int, input().split()))

now = 1
k = k % n

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

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

route = []
town = set()
now = 1

while True:
    now = a[now-1]
    if now in town:
        break
    town.add(now)
    route.append(now)
count = route.index(now)
if k <= count:
    print(route[count-1])
else:
    route = route[count:]
    num = (k-count)%len(route)
    print(route[num-1])
n,k=map(int,input().split())
a=list(map(int,input().split()))
now=0
ima=[]
while True:
    now=a[now-1]
    ima.append(now)
    if now in ima[:len(ima)-1]:
        break
print(a[k % (len(ima)-1)])
N,K = map(int, input().split())
A = list(map(int, input().split()))

limit = K
now = 1
log = []

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

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

    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(map(int,input().split()))

town = [0 for i in range(N)]

def next(A,now):
    return(A[now-1])

now = 1
i = 0
while True:
    if town[now-1] != 0:
        roop_start = town[now-1]
        roop_end = i
        break
    town[now-1] = i+1
    now = next(A,now)
    i += 1

roop = roop_end - roop_start + 1
teleport = roop_start + (K - roop_start)%roop
print(teleport)
now = 1
for i in range(teleport):
    now = next(A,now)

print(now)
n, k = map(int, input().split())
a = list(map(int, input().split()))
l = [1]
for i in range(n):
    if a[l[i] - 1] in l:
        res = l[:l.index(a[l[i] - 1])]
        cyc = l[l.index(a[l[i] - 1]):]
        break
    else:
        l += [a[l[i] - 1]]
print(cyc[(k - len(res)) % len(cyc)] if k > len(res) else res[k - 1])
n, k = map(int,input().split())
town = list(map(int,input().split()))
cnt = 1
step = 1
now = town[0]
via = [1]
while((now not in via)):
  via.append(now)
  now = town[now -1]
  cnt += 1
  step += 1

loop = via[via.index(now):]
print(loop[(k - cnt)%len(loop)])
N, K = map(int, input().split())
A = list(map(int, input().split()))
visited = [-1]*N
count = 0
v = 0
while visited[v] == -1:
    visited[v] = count
    v = A[v]-1
    count += 1
loop_length = count - visited[v]
K -= visited[v]
print(A[K%loop_length+visited[v]])
n, k = 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 = 0
loop = 10**8
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

for i in range((k-start_cnt)%loop+start_cnt):
    town = A[town]

print(town+1)
import sys

def input():
  return sys.stdin.readline()


N,K = map(int,input().split())
A=list(map(int,input().split()))
P=[True]*N
P[0]=False
i,a=1,0
while 1:
  a+=1
  if P[A[i-1]-1]:
    P[A[i-1]-1]=False
    i=A[i-1]
  else:
    break
if a>=K:
  print(A[K])
else:
  K-=a
  a,j=0,i
  while 1:
    a+=1
    if A[j-1]==i:
      break
    j=A[j-1]
  K%=a
  for _ in range(K):
    i=A[i-1]
  print(A[i-1])
import itertools
n,k=map(int,input().split())
a=list(map(int,input().split()))
pas=[1]
p=1
count=1
for i in range(n):
	p=a[p-1]
	pas.append(p)
	count+=1
	if count!=len(set(pas)):
		nloop=pas.index(p)
		loop=count-1-nloop
		break
last=(k-nloop)%loop
print(pas[nloop+last])
n,k = map(int, input().split())
a = [0] + list(map(int, input().split()))

r = [1]
now = 1
for i in range(n):
   now = a[now]
   if now in r: break
   r.append(now)
I = r.index(now)
if I != 0: 
  k -= I
  r = r[I:]
print(r[k%(len(r))])
n,k=map(int,input().split())
A=[0]+list(map(int,input().split()))
from collections import defaultdict

reached=defaultdict(int)
reached[1]=1
i=1
for j in range(1,n+1):
    if reached[A[i]]==0:
        reached[A[i]]=j+1
    else:
        t1=reached[A[i]]
        t2=j+1
        break
    i=A[i]
reached=dict(reached)
ama=(k-(t1-1))%(t2-t1)
ans=[s for s,h in reached.items() if h==t1+ama]
print(ans[0])
import sys
sys.setrecursionlimit(200000)
L = []

def Search(pos):
  if pos - 1 not in L:
    L.append(pos - 1)
    Search(A[pos-1])
  else:
    L.append(pos - 1)
    return pos
  
N, K = map(int, input().split())
A = list(map(int, input().split()))
Search(1)
#print(L)
if K < L.index(L[-1]):
  print(L[K])
else:
  t = L.index(L[-1])
  k = len(L) - t - 1
  K -= t
  #print(t, k, K % k)
  print(L[t + (K % k)] + 1)
n,k=[int(x) for x in input().split()]
p=[(int(x)-1) for x in input().split()]
a=[0]
i=0
while p[i] not in a:
  a.append(p[i])
  i=p[i]
l=a.index(p[i])
if k<=l: print(a[k])
else: print(1+a[l+(k-l)%(len(a)-l)])
n,k,*a=map(int,open(0).read().split())
d={}
s=1
while k:k-=1;s=a[s-1];k%=d.get(s,2**60)-k;d[s]=k
print(s)
n, k = map(int, input().split())
a = tuple(map(int, input().split()))

i = 0
aa = 0
visited = {}
for i in range(k):
    aa = a[aa] - 1
    if aa in visited:
        s, e = visited[aa], i
        loop = e - s
        k = s + ((k - s) % loop)
        exit(0)
    visited[aa] = i

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

#ループする場合
flag=[0]*N
patt=[]
i=0
while True:
    patt.append(i)
    if flag[i]==1:
        #del patt[-1]
        break
    flag[i]=1
    i=A[i]-1

srt = patt.index(patt[-1])
#print(srt)
end = len(patt)-2
ptrn = patt[srt:end+1]

B = (K-srt) % len(ptrn)
#print('B',B)
if B==0:
    print(ptrn[0]+1)
else:
    print(ptrn[B]+1)
    #print('0000000')
n, k = map(int, input().split())
a = list(map(lambda x: int(x)-1, input().split()))

visited = list()
i = 0
cnt = 0
while True:
    if i in visited:
        break
    visited.append(i)
    i = a[i]
    cnt += 1

si = visited.index(i)
loop = visited[si:]
n_loop = len(loop)
left_k = k - cnt
print(loop[left_k % n_loop] + 1)
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+1:
        x = 1
        break 
if x == 1:
    aa = k%(len(ss)+1)
    if aa == 0:
        for i in range(len(ss)):
            now = a[now-1]
    else:
        for i in range(aa-1):
            now = a[now-1]
print(now)
N,K = map(int,input().split())
A = list(map(int,input().split()))

flag = [0]*(N+5) # 何回目に初めて訪れたか 

period = 1
now = 0
for i in range(N):
  now = A[now]-1
  #print(now+1)
  #print(flag)
  #print()
  if(flag[now] != 0):
    period = (i+1)-flag[now] 
    break
  flag[now] = i+1

#print('period',period)  
num = K % period    
#print('num',num)

now = 0
for i in range(num):
  now = A[now]-1
ans = now+1 # index→town_no  
print(ans)
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-len(d))%(len(d)-b.index(e))+b.index(e)])
n,k = map(int, input().split())
d={}
l = list(map(int, input().split()))
for i in range(len(l)):
    d[i+1] = l[i]
print(d)

city = 1
l2 = [1]
while True:
    city = d[city]
    if city in l2:
        break
    l2.append(city)
first = l2.index(city)
if len(l2) <= k:
    if city == l2[-1]:
        print(city)
    else:
        l2 = l2[first:]
        print(l2[(k-first)%(len(l2))])
else:
    print(l2[k])
import itertools

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

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

Loop=1
Next=1
From=0
To=0

found=[0]*N
while(True):
    found[Next-1]=1
    From=Next
    Next=A[Next-1]
    To=Next
    if(found[Next-1]==1):
        break
    Loop+=1

K=To-1+K%(Loop)
#print(Loop)
Next=1
for i in range(K):
    Next=A[Next-1]

print(Next)
n, k = map(int,input().split())
a = [int(i) for i in input().split()]
b = [a[0]]
for i in range(n-1):
    b.append(a[b[-1]-1])

for i in range(n):
    c = b.count(b[i])
    if c != 1:
        break

index_num = [n for n, v in enumerate(b) if v == b[i]]
ans = index_num[0] + 1
e = index_num[1]

f = e - ans+ 1

g = k - ans
h = g % f
print(b[index_num[0]+h])
n,m = map(lambda x: int(x), input().split())
a = [int(_) for _ in input().split()]

goal = 1
nexts = 1
visited = [1]
r_len = 1

for _ in range(m):
  nexts = a[nexts-1]
  if nexts in visited:
    r_len = len(visited[visited.index(nexts):])
    break
  else:
    visited.append(nexts)
  
rest = m-r_len+1
rec = rest%r_len
print(visited[r_len-rec-1])
n,k = map(int,input().split())
A = list(map(int,input().split()))
visited = [False for _ in range(n)]
nx = A[0] - 1
x = []
for e, i in enumerate(range(n)):
    if visited[nx] == False:
        visited[nx] = True
        x.append(nx)
        nx = A[nx] - 1
    else:
        p = x.index(nx)
        q = x[:p]
        r = x[p:]
        break
k = (k-len(q))%len(r)
print(r[k-1]+1)
N, K = list(map(int, input().split()))
A = list(map(lambda x: int(x) - 1, input().split()))

now = 0
route = []
while True:
    route.append(now)
    to = A[now]
    if to == None:
        break
    else:
        A[now] = None
        now = to

print(route)

loop_start = route.index(now)
loop_size = len(route) - loop_start

n_for_loop = min(K, loop_start)
n_in_loop = (K - n_for_loop) % loop_size

print(route[n_for_loop + n_in_loop] + 1)
n,k=map(int,input().split())
a=list(map(int,input().split()))
dic={}
wk=1
start=0
for i in range(1,n+1):
  wk=a[wk-1]
  if wk in dic:
    start=dic[wk]
    break
  dic[wk]=i
end=len(dic)
loopcnt=end-start+1
keys=list(dic.keys())
if k<start:
  print(keys[k-1])
else:
  chk=(k-start+1+loopcnt)%loopcnt
  print(keys[(start-1)+chk-1])
import itertools

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

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

Loop=1
Next=1

found=[0]*N
while(True):
    found[Next-1]=1
    Next=A[Next-1]
    if(found[Next-1]==1):
        break
    Loop+=1

K=K%(Loop+1)

for i in range(K+1):
    Next=A[Next-1]

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

init = [1]
while True:
    nxt = a[init[-1]-1]
    if nxt in init:
        idx_nxt = init.index(nxt)
        loop = init[idx_nxt:]
        init = init[:idx_nxt]
        break
    else:
        init.append(nxt)

idx = (k-len(init))%len(loop)
print(loop[idx])
N,K=map(int,input().split())
A=list(map(int,input().split()))
print(A[A[K%N]])
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    N, K = lr()
    A = lr()
    vis = [-1]*(N+1)
    dis = [0]*(N+1)
    v = 1
    cnt = 0
    while vis[v] == -1:
        dis[cnt] = v
        vis[v] = cnt
        v = A[v-1]
        cnt += 1
    print(dis[K%(cnt-vis[v])])
resolve()
n,k = map(int, input().split())
a = list(map(int, input().split()))
is_good = True
graph=[0]
s = 0
while is_good:
    next = a[graph[-1]]-1
    if  next in graph:
        next_ = graph.index(next)
        loop = len(graph) - next_
        is_good = False
    else:
        graph.append(next)


if k <= next_:
    print(graph[k-1] + 1)

else:
    k -= next_
    x = k%loop
    print(graph[x+next_]+1)
import numpy as np
n, k = [int(k) for k in input().split()]
a = np.array([int(k) for k in input().split()], dtype=int)
used = np.full(n, -1)
r = 1
used[0] = -1
for i in range(n):
    r = a[r - 1]
    if used[r - 1] != -1:
        r = i - used[r - 1]
        break
    used[r - 1] = i
k = k % r
# print(k)
r = 1
for i in range(k):
    r = a[r - 1]
print(r)
N,K = map(int, input().split())
A = list(map(int, input().split()))

def forward(L ,n=1, count=K):
    n = L[L[n]]
    if (count == K):
        return n
    forward(L, n)
    
forward(A)
import numpy as np
n, k = [int(k) for k in input().split()]
a = np.array([int(k) for k in input().split()], dtype=int)
used = np.full(n, -1)
r = 1
for i in range(n):
    r = a[r - 1]
    if used[r - 1] != -1:
        k = k % (i - used[r - 1])
        break
    used[r - 1] = i
r = 1
for i in range(k):
    r = a[r - 1]
print(r)
n,k=list(map(int,input().split()))
A=list(map(int,input().split()))
start,i=1,0
root=[1]

for j in range(k):
  start=A[start-1]
  if start in root:
    i = start
    break
  root.append(start)
  
if k <= len(root):
  print(root[k-1])
else:
  ind = root.index(i)
  newroot=root[ind:]
  ans=(k-len(root))%len(newroot)
  print(newroot[ans])
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [i-1 for i in A]

check = [0]*N
i = 0
c = 0
check = []
visited = [0]*N
flag = False
while c<=K:
    if visited[i] == 1:
        flag = True
        break
    else:
        check.append(i)
        visited[i] = 1
        i = A[i]
        c += 1
if flag==True:
    X = check[check.index(i):]
    print(1+X[(K-c)%len(X)])  
else: 
    print(i+1)
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = 1
A = [1]
for i in range(1,k+1):
    ans = a[ans-1]
    if ans in A:
        x = (k-i+1) % (len(A)-A.index(ans))
        ans = A[A.index(ans)-1+x]
        print(ans)
        exit()
    A.append(ans)
print(ans)
N,K = map(int,input().split())
A = list(map(int,input().split()))
location = [1]
nowlocate = 1
for i in range(N):
  nowlocate = A[nowlocate-1]
  location.append(nowlocate)
  if len(set(location)) != len(location):
    M = len(location) -1
    break
for i in range(M):
  if location[M] == location[i]:
    roop = M-i
    stop = i
    break
print(location[((K-stop) % roop) + stop ])
n, k = map(int, input().split())
a = list(map(int, input().split()))

num = 1
l = []

for _ in range(n+1):
    if num in l:
        l2 = l[l.index(num):]
        break

    else:
        l.append(num)
        num = a[num-1]

kk = (k-len(set(l)-set(l2)))%len(l2)
print(l2[kk])
(N, K) = [int(n) for n in input().split()]
A = [int(n)-1 for n in input().split()]
v = [-1 for _ in range(N)]

D = [0]
v[0] = 0

cur = A[0]
i = 1

while v[cur] == -1:
    D.append(cur)
    v[cur] = i
    

    i += 1
    cur = A[cur]

K -= v[cur]
K %= (i - v[cur])
    
print(D[v[cur] + K]+1)
N,K=map(int,input().split())
t=list(map(int,input().split()))
g=[1]
for i in range(N):
  gs=set(g)
  if t[g[i]-1] in gs:
    h=g.index(t[g[i]-1])
    s=i+1-h
    break
  g.append(t[g[i]-1])
if K>h:
  K=(K-h)%s+h
print(g[K])
print(s,h)
n,k= map(int,input().split())
temp = list(map(int,input().split()))
As = [0]*(n+1)
for i in range(n):
    As[i+1] = temp[i]

visit_list = {}
now = 1
visit_list[1] = 0
visit_num = 1

while True:
    now = As[now]
    if now in visit_list:
        break
    visit_list[now] = visit_num
    visit_num += 1

loop_num = visit_num - visit_list[now]

res = k%loop_num
res -= visit_list[now]%loop_num

for i in range(res):
    now = As[now]

print(now)
n, k = map(int, input().split())
A = list(map(int,input().split()))
flag = [True]*n
p = 0
rt = [1]

while flag[p]:
    flag[p] = False
    p = A[p] -1
    rt.append(p+1)

to_p = rt.index(rt[-1])
p2p = rt[to_p:-1]
p2p_len = len(p2p)

if k < p2p_len:
    print(rt[k])
else:    
    idx = (k-to_p)%p2p_len
    print(p2p[idx])
N, K = map(int, input().split())
a = list(map(int, input().split()))
cnt = 0
li = [1]
c = 1
for i in range(200001):
    cnt += 1
    c = a[c-1]
    if c in li:
        break
    li.append(c)
b = li[li.index(c):]
e = (K-cnt)%len(b)
print(b[e])
n,k = map(int,input().split())
a = list(map(int,input().split()))
cur = 0
reach = [False]*n
x = []
for i in range(k):
    x.append(cur)
    if reach[cur]:
        break
    reach[cur]=True
    cur = a[cur]-1
cnt = 0
for i in range(len(x)):
    cnt += 1
    if x[i]==x[-1]:
        break
y = x[cnt-1:-1]
if k>cnt:
    ind = (k-cnt+1)%len(y)
    print(y[ind]+1)
else:
    print(x[k%len(x)]+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
if n!=2:    
    print(B[k-((k-j)//T)*T],B)
else:
    print(B[k%2])
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
ordi = [0, ]
for i in range(n):
    if a[ordi[-1]] - 1 not in ordi:
        ordi.append(a[ordi[-1]] - 1)
    else:
        ordi.append(a[ordi[-1]] - 1)
        index = ordi.index(a[ordi[-1]] - 1)
        len_ = len(ordi) - index
        break
print(ordi[index + (k - index) % len_] + 1)
N, K = map(int, input().split())
X = list(map(int, input().split()))

dp = [[0] * (N + 1) for _ in range(50)]
dp[0] = [0] + X
for k in range(1, 50):
    for i in range(N + 1):
        dp[k][i] = dp[k - 1][dp[k - 1][i]]

ans = 1
for k in range(50):
    if K >> k & 1:
        ans = dp[k][ans]

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

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

visited = [-1] * (n + 1)

next = 1
cnt = 1
while True:
    if visited[next] == -1:
        visited[next] = cnt
        cnt += 1
    else:
        mod = cnt - visited[next]
        break
    next = a[next - 1]

k -= cnt
k %= mod

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

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

f = [0] * n
i = 0
x = 0
while True:
    f[i] = 1
    i = a[i] - 1
    x += 1
    if f[i]:
        break
    
f = [0] * n
cnt = 0
while True:
    f[i] = 1
    i = a[i] - 1
    cnt += 1
    if f[i]:
        break

ans = i
cnt -= x - cnt
for i in range(cnt - 1):
    ans = a[ans] - 1

print(ans + 1)
n,k=map(int,input().split())
l=list(map(int,input().split()))
lcount=[0]*(n+1)
lnum=[0]*(n+1)
count=1
place=1
while lcount[place]==0:    
    lnum[place]=count
    lcount[place]=1
    place=l[place-1]
    count+=1 

dif=count-lnum[place]
k=(k+1-lnum[place])%dif+lnum[place]
print(lnum.index(k))
(N, K) = list(map(int, input().split()))
a_list = list(map(int, input().split()))

amap = {}
for i, dst  in enumerate(a_list):
    amap[i + 1] = dst

print(amap)

prev = 1
curr = None
for k in range(1, K + 1):
    curr = amap[prev]
    prev = curr
print(curr)