peterverebics/CodeNet_Python_118 huggingface dataset:
n,k = map(int,input().split())
li = list(map(int,input().split()))

lili = [1]
while li[(lili[-1] - 1)] not in lili:
    lili.append(li[(lili[-1] - 1)])

newlist = lili[lili.index(li[lili[-1]-1]):]

print(newlist[((k - len(lili)) % len(newlist))])
n,k=map(int,input('').split(' '))
a=input('')
a=a.split(' ')
for p in range(n):
    a[p]=int(a[p])

p=1
for j in range(k):
    p=a[p-1]-1

print(a[p])
n,k = map(int,input().split())
a = list(map(int,input().split()))
l = [1]
tp = a[0]-1
for i in range(n):  
    l.append(a[tp])
    tp = a[tp] -1    
p = l.index(l[-1])
if len(l)>k:
  print(l[k])
  exit()
  
x = (k-n)%(n-p)
if x==0:
  print(l[n-1])
else:
  print(l[x+p-1])
N, K = map(int, input().split())
A = list(map(int, input().split()))
memo = [-1] * N

teleporter = A[0]
memo[0] = teleporter
repeat_point = -1

for i in range(1, N):
    memo[i] = A[teleporter-1]
    teleporter = A[teleporter-1]
    if memo[i] in memo[:i]:
        repeat_point = i
        break

start_point = memo.index(memo[repeat_point])
repeat = repeat_point - start_point
# print(memo, start_point, repeat_point)

if K <= N:
    print(memo[K-1])
else:
    mod = K % repeat
    if mod == 0:
        mod = repeat_point
    print(memo[mod-1])
n, k = map(int,input().split())
a = list(map(int,input().split()))

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

if cy == -1:
    print(d[k-1])
    exit()

noncyi = d.index(cy)
cyi = len(d) - noncyi
if k < noncyi:
    print(d[k-1])
else:
    k -= noncyi
    print(d[noncyi + k%cyi]+1)
n,m=map(int,input().split())
l=list(map(int,input().split()))
x=y=t=0
c=1
x=min(l)
s=[0]*n
while 1:
    if s[x-1]==0:
        s[x-1]=1
    else:
        break
    y = l[x - 1]
    c+=1
    x=y
z=m%c
x=min(l)
while z:
    y = l[x-1]
    z-=1
    x=y
print(y)
N,K = [int(i) for i in input().split(' ')]
A = [int(i) for i in input().split(' ')]
 
where_li = []
pre = 0
for i in range(N):
	where_li.append(A[pre])
	pre = A[pre]-1
N, K = map(int,input().split())
A = list(map(int,input().split()))
roop = 0
port = [A[0]]
for _ in range(len(A)):
    port.append(A[port[-1]-1])
    if port.count(A[port[-2]-1]) == 2:#A in port
        port.pop()
        roop = len(port)-port.index(A[port[-1]-1])
        break
K = K-len(port)
print(port[K % roop])
N, K = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))
path = [-1] * N
nex = A[0]
path[0] = 0
cnt = 1
if K > N:
    while path[nex] == -1:
        path[nex] = cnt
        cnt += 1
        nex = A[nex]

    mod = cnt - path[nex]
    n = path[nex] + (K-path[nex])%mod
else:
    n = N

ans = 0
for _ in range(n):
    ans = A[ans]
ans += 1

print(ans)
N,K=map(int,input().split())
A=list(map(int, input().split()))
s=1
t=0

lists=[]
listt=[]
for i in range(10**6):
    s=A[s-1]
    listt=lists[:]
    lists.append(s)
    t+=1
    if len(set(lists))==len(set(listt)):
        break
    else:
        continue
k=0
for i in range(1,t):
    if lists[t-1]==lists[t-1-i]:
        k=i
if K<=t-1-k:
  	print (lists[K])
else:
  	print (lists[(K-t)%k+(t-1-k)])
n, k = [int(i) for i in input().split()]
a = [int(j) for j in input().split()]
dp = [0] * (n + 1)
for i in range(1, n + 1):
    dp[i] = a[dp[i - 1]] - 1
b = dp[n]
for j in range(n - 2, -1, -1):
    if dp[j] == b:
        period = n - j
        break
k = k % period
print(dp[k] + 1)
N, K = map(int, input().split())
Nlist = {}
Nlist[1]=0
Vlist={}
count = 1
Vlist[0]=1
for A in input().split():
    A=int(A)
    if count==A:
        continue
    if A in Nlist:
        break
    Nlist[A] = count
    Vlist[count]=A
    count = count+1

print(Vlist[K%count])
n, k = map(int,input().split())
machi = list(map(int, input().split()))

current = machi[n - 1]
for _ in range(k):
    current = machi[current - 1]

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

def rec(now, k):
    if k == 0:
        return now
    if k == 1:
        return A[now]

    log2k = len(bin(k)) - 3

    for i in range(log2k):
        now = A[A[now]]

    return rec(now, k - 2 ** log2k)

print(rec(1, K))
N,K = map(int, input().split())
A =[0]
B=list(map(int, input().split()))
for m in range(N):
    A.append(B[m])
place=[]
i=1
count=0
while i not in place:
    place.append(i)
    i=A[i]
    count+=1
loop=[i]
start=i
j=A[i]
count+=1
while j!=start:
    loop.append(j)
    j=A[j]
    count+=1
num=(K-count)%len(loop)
print(loop[num])
def main():
    N, K = map(int, input().split())
    A = [int(x) for x in input().split()]
    L=[1]
    a = A[0]
    for _ in range(N):
        L.append(a)
        a=A[a-1]
        if a==1:
            break
    g = K%len(L)
    print(L[g])
    
main()
N, K = map(int, input().split(" "))
A = list(map(int, input().split(" ")))
Cyc = [0]
i = 0
while True:
	if Cyc.count(A[i]) > 0:
		place = Cyc.index(A[i])
		break
	Cyc.append(A[i])
	i = A[i]-1

p2 = place-1
ans = (K-p2) % (len(Cyc)-place) 
print(Cyc[p2+ans])
n, k = map(int, input().split())

num = list(map(int, input().split()))
t = [1]
t_test = []

for i in range(n):
  x = num[t[-1] - 1]
  if(x not in t):
    t.append(x)
  else:
    t_test = t[t.index(x):len(t)]

list_num = list(set(t) - set(t_test))

if(len(t_test) >= 1):
  print(t_test[(k - len(list_num)) % len(t_test)])
else:
  print(t[-1])
def main():
    n,k = map(int,input().split())
    A = list(map(int,input().split()))
    array = [1]
    now = 1
    for i in range(n):
        next = A[now-1]
        array.append(next)
        now = next
    array = list(set(array))
    length = len(array)
    print(array[k%length])


if __name__ == '__main__':
    main()
# -*- coding: utf-8 -*-

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

M = K%N

town_now = 1

for _ in range(M):
  town_now = A[town_now-1]

print(town_now)
from collections import defaultdict
N, K = map(int, input().split())
A = list(map(lambda x:int(x), input().split()))
adj = defaultdict(int)
for i in range(len(A)):
    adj[i+1] = A[i]

d = 0
D = 1
slow = adj[1]
fast = adj[adj[1]] 

while fast != slow:
    D += 1
    slow = adj[slow]
    fast = adj[adj[fast]]

slow = 1
while fast != slow:
    D += 1
    d += 1
    slow = adj[slow]
    fast = adj[fast]


r = (K - d) % (D - d)

while r:
    slow = adj[slow]
    r -= 1
print(slow)
n,m=map(int,input().split())
li=list(map(int, input().split()))
s=m%n
ans=li[li[s-1]-1]
print(ans)
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=1
c=[1]
for i in range(k):
    b=a[b-1]
    c.append(b)
    if len(c)!=len(set(c)):
        break
e=c.index(b)
mod=k%(len(c)-e)
print(c[mod+e])
N,K = map(int,input().split())
A = list(map(int,input().split()))

cnt = 0
dic = {1 : 1}
x = 1
for i in range(N):
    y = A[x-1]
    cnt += 1
    if y in dic:
        break
    else:
        dic[y] = cnt
        x = y

if N < K:
    s = K % cnt  + dic[y]
    x = y
else:
    s = K
    x = 1
for i in range(s):
    
    y = A[x-1]
    x = y
print(x)
n,k = map(int,input().split())
a = [int(i) - 1 for i in input().split()]
r = []
nx = 0
for i in range(n):
    r.append(nx)
    tnx = a[nx]
    if tnx in r:
        m = r.index(tnx)
        d = i - m + 1
        k = k - m
        k = k % d
        print(r[k + m] + 1)
        break  
    nx = tnx
n,k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
 
l = [1]
now = 1
notloop = 0
dupl = -1
 
for i in range(n-1):
  now = a[now-1]
  
  if now in l:
    dupl = now
    break
  else:
    l.append(now)
    
if(dupl == -1):
  print(now)
  
else:
  notloop = l.index(dupl)
  length = len(l)-notloop
  print(l[(k-notloop)%length+notloop])
NK = list(map(int, input().split())) 
A = list(map(int, input().split())) 

N = NK[0]
K = NK[1]
town= 1
list = []
for i in range(N+1):
  town = A[town-1]
  list.append(town)
  if i >0 and town == list[0]:
    break
list1 = list[:-1]
 
d = K % len(list1)

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

a = [i - 1 for i in a]
l = []
used = [0 for _ in range(n)]
used[0] = 0

c = 0
while 1:
    l.append(c)
    if used[c] == 1 and l:
        break
    used[c] = 1
    c = a[c]


end = l[-1]
p = 0
for i in range(len(l)):
    if l[i] == end:
        l = l[i:]
        p = i
        break

l=l[:-1]
b = (k-p) % len(l)
print(l[b]+1)
import sys

def main():
    n, k = map(int, sys.stdin.buffer.readline().split())
    a = [0] + list(map(int, sys.stdin.buffer.readline().split()))

    visited = [0]*(n+1)
    town = 1
    for i in range(k+1):
        town = a[town]
        if visited[town] == 0:
            visited[town] = i
        else:
            print(visited.index( (k - visited[town])%(i - visited[town]) + visited[town]))
            exit()
    print(town)

if __name__ == "__main__":
    main()
N,K=list(map(int,input().split()))
l=list(map(int,input().split()))
from collections import deque
roop=deque([1])
now=l[0]
while now not in roop:
   roop.append(now)
   now=l[now-1]
start=roop.index(now)
K-=start
for i in range(start):roop.popleft()
K=K%len(roop)
print(roop[K])
n, k = map(int, input().split())
port = list(map(int, input().split()))
visited = [1]
current = 1

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

s = visited.index(port[current-1])
bubun = visited[s:]

g = (k-s) % len(bubun)
print(bubun[g])
n, k = map(int, input().split())
A = [0] + list(map(int, input().split()))
now = 1
used = [-228] * (n + 191)
used[1] = -1
cn = -292
for _ in range(k):
    now = A[now]
    if used[now] != -228:
        cn = _ + 1 - used[now]
        break
    used[now] = _
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()))
town=[0]*n
info=[1]
now=1
while town[now-1]==0:
    town[now-1]+=1
    now=a[now-1]
    info.append(now)
loop_list=list(info[info.index(now):-1])
loop_len=len(loop_list)
before_loop_list=list(info[:info.index(now)])
if k<=len(before_loop_list):
    print(before_loop_list[k-1])
else:
    print(loop_list[((k-len(before_loop_list))%loop_len)])
from sys import stdin
n,k = [int(x) for x in stdin.readline().rstrip().split()]
a = [int(x) for x in stdin.readline().rstrip().split()]
b = [-1]*n
b[0] = 0
tf = True
t = 1
g = 0
while tf:
    if b[a[g]-1] == -1:
        b[a[g]-1] = t
    else:
        m = b[a[g]-1]
        g = a[g]-1
        break
    t += 1
    g = a[g]-1
k -= t
mo = t-m
k = k % mo
while k > 0:
    g = a[g]-1
    k -= 1
print(g+1)
n, k = map(int, input().split())
A = list(map(int, input().split()))
def f(x):
    return A[x-1]
c = 1
P = [1]
 
for i in range(1,k+1):
    c = f(c)
    if c in P:
        roop_start = P.index(c)
        roop_num = i- roop_start
        break
    P.append(c)
    if i == k:
        roop_start = 0
        roop_num = 1
a = (k - roop_start) %  roop_num
b = P[a + roop_start]
print(b)
n,k=map(int,input().split())
a=list(map(int,input().split()))
li=[]
ans=1

for i in range(k):
    ans=a[ans-1]
    
    if ans not in li:
        li.append(ans)
    else:
        break
else:
    print(ans)
    exit()


li_index=li.index(ans)
t=len(li)-li_index
k-=li_index
k%=t

#print(li_index,t,k)
print(li[li_index+k-1])

#print(ans)
#print(li)
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
a = [i-1 for i in a]
# print(a)
visit = [0]*(n+1)
v = []
v.append(0)
i = 0
num = 0
while True:
    if visit[a[v[i]]]==0:
        v.append(a[v[i]])
        visit[a[v[i]]] += 1
        i += 1
    else:
        num = len(v)
        v = v[v.index(a[v[i]]):]
        break
# print(v)
k -= num
# print(k%len(v))
print(v[k%len(v)]+1)
n,k=map(int,input().split())
a=list(map(int,input().split()))
next=a[0]
town=[0]*n
town[0]+=1
for i in range(n):
  if town[next-1]==0:
    town[next-1]+=1
    next=a[next-1]
  else:
    start=next
    break
next=a[start-1]
cnt=1
for i in range(n):
  if a[next-1]!=start:
    next=a[next-1]
    cnt+=1
  else:
    cnt+=1
    break
print(cnt)
k%=cnt
next=a[0]
for i in range(k-1):
  next=a[next-1]
print(next)
N,K = map(int, input().split())
A = list(map(int, input().split()))
list = [0]*N

#print(A)
#print(list)
num = 0
ans = A[0]
list[ans-1] += 1
while num <= 2*N+1:
  num += 1
  ans = A[ans-1]
  list[ans-1] += 1
  #print(list)

x = list.count(1)
y = N-x-list.count(0)
#print(x,y)

K = K%y
#print(K)
ans = A[0]
while K > 1:
  K -= 1
  ans = A[ans-1]
      
print(ans)
n, k = map(int, input().split())
A = list(map(int, input().split()))
r = 1
root = [1]
for _ in range(n):
    roo = A[r-1]
    root.append(roo)
for i in range(n):
    if roo == root[i]:
        roopcount = n-i-1
        break
if k <= n:
    print(root[k-1])
else:
    s = k%n
    print(root[i])
n,k = map(int,input().split())
l1 = list(map(int,input().split()))
l = list(i-1 for i in l1)
bit = list(0 for i in range(n))
bit[0] =1
mi = [0]
m = l[0]
er = 0
while er ==0:
  if bit[m]==1:
    a = mi.index(m)
    r = mi[a:]
    er  =1
  else:
    bit[m] = 1
    mi.append(m)
    m = l[m]
 # print(bit)
be = mi[:a]+r
#print(be,r)
if k< len(be):
  print(be[k])
else:
  w = k-len(be)+1
  u = (w%len(r))-1
  print(r[u]+1)
N,K = map(int, input().split(' '))

cache={}
log=[]
A = list(map(lambda x:int(x)-1, input().split(' ')))

now=0

while(True):
  log.append(now)
  cache[now]=1
  now=A[now]
  if(cache.get(now)):break

loop_start=log.index(now)
loop_end=len(log)
loop=loop_end-loop_start
#print(loop_end-loop_start,loop_start)
print(loop_start,loop_end,log)
if len(log) >= loop:
  if loop_end>K:
    print(now)
  else:
    print(log[K%loop]+1)
else:
  print(log[K%loop+loop]+1)
N,K = map(int, input().split())
a = list(map(int, input().split()))
ans = a[0]
K = K % len(a)
for i in range(1,K):
    ans = a[ans-1]

print(ans)
n,k = map(int, input().split())
a = list(map(int, input().split()))
a = [i-1 for i in a]
num = [0] * (n)
num[0] = 1
now = 0
for i in range(2,300000):
  nex = a[now]
  if num[nex]:
    start = num[nex]
    cycle = i - num[nex]
    v = (k+1-start) % cycle + start
    ans = num.index(v) + 1
    break
  else:
    num[nex] = i
    now = nex
print(ans)
from collections import defaultdict as d 

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

s = '1'
i = 1
b = 0
while True:
	if b == 2 * 10**5:
		break
	s += str(arr[i - 1])
	i = arr[i - 1]
	b += 1
y = (k) % len(s)
print(s[y])
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:
  print(keys[(start-1)+(k-start+1)%loopcnt-1])
import sys
from collections import deque

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


time=1
route=[1]
x=A[0]
Loop=0
x_list=[0]*(N+1)
x_list[1]=1

while x_list[x]==0:
    route.append(x)
    x_list[x]=1
    x=A[x-1]
    time+=1

Loop=time-route.index(x)
a=route.index(x)

        
if Loop==0:
    mod=K%time
    print(route[mod])
    sys.exit()

else:
    mod=(K-a)%Loop
    print(route[mod+a])
N, K = map(int, input().split())
A = list(map(int, input().split()))

went = [1, A[0]]

for i in range(1, K):
    next_town = A[went[i]-1]
    if next_town in went:
        head = went.index(next_town)
        loop = went[head:i + 1]
        last_town = (K - len(went) + 1) % len(loop)
        print(loop[last_town - 1])
        break
    else:
        went.append(next_town)
n,k=map(int,input().split())
a=list(map(int,input().split()))
x=0
y=0
con1=0

if k<=n:
 for i in range(0,n):
  x=a[x]
 print(x)

else:

 for i in range(0,n):
  x=0
  x=a[x]
 for j in range(0,i-1):
  y=a[y]
  if x==y:
   break
  else:
   y=x
  
 w=y+k%(x-y+1)
 for l in range(w):
  u=0
  u=a[u]
 print(u)
N = [int(e) for e in input().split()]
nums = [int(t) for t in input().split()]
count=[]
t = True
k=0
check = []
for i in range(N[0]):
    count.append(0)
while t:
    t = nums[k]
    if(t in check):
        break
    else:
        check.append(t)
    k = t -1
ans = (N[1] - nums.index(t)-1)%(len(nums)-nums.index(t)-1)
ans1 = nums.index(t) + ans
print(nums[ans1])
N, K = list(map(int, input().split()))
A = [0] + list(map(int, input().split()))
time = [-1] * (N + 1)
curr = 1
time[curr] = 0
now = 0
while True:
  now += 1
  curr = A[curr]
  if time[curr] >= 0:
    break
  if now == K:
    print(curr)
    exit()
  time[curr] = now

# K = time[curr] + loop * (now - time[curr]) + r
idx = K % (now - time[curr])
print(time.index(idx))
N,K= map(int,input().split())
A=list(map(int,input().split()))
count=0
temp=1
memo=[]
flag=True
while (temp not in memo):
    count=count+1
    memo.append(temp)
    temp=A[temp-1]
num=memo.index(temp)
roopnum=len(memo)-num
K=(K-num)%roopnum
temp=1
for i in range(K+num):
    temp=A[temp-1]
print(temp)
n,k = map(int,input().split())
a = list(map(int,input().split()))
checked = [0]
next_num = a[0]-1
i = 0
while True:
    if i == 0:
        i += 1
    elif next_num in checked:
        loop = checked[checked.index(next_num):]
        break
    checked.append(next_num)
    next_num = a[next_num]-1
dif = len(checked) - len(loop)
if k <= dif:
    print(checked[k-1]+1)
else:
    k = k-dif
    print(loop[k%len(loop)]+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [i-1 for i in a]
visited = [-1]*n
route = []
now = 0
hop = 0
while visited[now] == -1:
    visited[now]=hop
    route.append(now)
    now = a[now]
    hop+=1
loop_st = 0
for r in route:
    if r==now: break
    loop_st+=1
loop = route[loop_st:]
if k-1 < len(route):
    print(route[k-1]+1)
else:
    print(loop[(k-loop_st)%len(loop)]+1)
N, K = map(int, input().split())
P = list(map(int, input().split()))
M = 40

c = 0
for j in range(M+1):
    if K % 2:
        c = P[c]-1
    K //= 2
print(c+1)
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = [1]
x = 1

for i in range(n):
  x = a[x-1]
  if not(x in b):
    b.append(x)
  else:
    c = b.index(x)
    break
d = (k-c) % (len(b)-c)
print( b[c+d] )
n, k = map(int, input().split())
a = list(map(int, input().split()))
c = [1]

while True:
    c.append(a[c[-1]-1])
    if c.count(c[-1])==2:
        cs = c.index(c[-1])
        l = len(c)-cs-1
        break
    
print(c[(k-cs)%l+cs])
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[0]*n
c=[0]*n
ka=0
kab=0
sihyou=0
while sihyou==0:
    
    
    if b[ka]==1:
        sihyou=1
    else:
        b[ka]=1
        c[ka]=kab
        ka=a[ka]-1
        kab=kab+1
ruupu=kab-c[ka]
ansa=(k-c[ka])%ruupu+c[ka]
yui=0
for i in range(ansa):
    yui=a[yui]-1
print(yui+1)
print(ansa)
n,k = map(int,input().split())
A = [0]
A.extend(list(map(int,input().split())))

town = 1
once = [0]*(n+1)

for i in range(n):
  if once[town]==0:
    once[town] = 1
  else:
    break
  town = A[town]

rt = A[town]
roop = 1
while rt != town:
  rt = A[rt]
  roop += 1

rt = 1
bf = 0
while rt != town:
  rt = A[rt]
  bf += 1  

if roop<k or bf<k:
  K = (k-bf)%roop + bf
else:
  K = k

town = 1
for _ in range(K):
  town = A[town]

print(town)
N, K = map(int, input().split())
A = list(map(int, input().split()))
ex = 0
B = [1]
for i in range(N):
    b = A[ex]
    if b == 1:
        break
    else:
        B.append(b)
        ex = A[ex] - 1
n = len(B)
ans = B[K % n]
print(ans)
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]
        e = c[b-1]
        break;
b = 1
for i in range(e+(k-e)%d):
    b = a[b-1]
print(b)
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(l[l.index(x):])
if A[x-1] == x:
    print(x)
else:
    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()))
B = []
b = 1
ans = 1
n,k = (int(a) for a in input().split())
A = list(map(int,input().split()))
t = [1]
x = 1
for i in range(n) :
    t.append(A[x-1])
    x = t[-1] 
    if len(t) != len(set(t)) :
        break
l = [i for i , x in enumerate(t) if x == t[-1]]
k = k-l[0]
ans = k%(l[1]-l[0])
print(t[l[0] + ans])
# -*- coding: utf-8 -*-

N,K=map(int,input().split())
A=input().split()
A=[int(a) for a in A]

P=0
L=[0]*int(N)
D=[0]*int(N)
c=0
R=0
while(1):
    c+=1
    P=A[P]-1
    L[c]=P
    if D[P]>0: 
        R=D[P]
        break
    D[P]=c
    if P==0: break
    

#print(L)
K-=R
K%=(c-R)
#print(R,K)
ans=L[R+K]+1

print(ans)
import sys
N,K = map(int,input().split())
tele = list(map(int,input().split()))
k = 1
goal = tele[0]
miti = [1]
while (goal in miti)!=1:
    k += 1
    miti.append(goal)
    goal = tele[goal-1]
    if k == K:
        goal = tele[goal-1]
        print(goal)
        sys.exit()
print(miti)

a = miti.index(goal)
loop = k-a
ans = miti[(K-a)%loop+a]

print(ans)
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)
        len_init = idx_nxt
        len_loop = len(init) - idx_nxt
        break
    else:
        init.append(nxt)

idx = (k-len_init)%len_loop
print(init[len_init+idx])
data = input().split(" ")
N = int(data[0])
K = int(data[1])
tele = input().split(" ")
teles=[]
for t in tele:
    teles.append(int(t))
now = 1
for _ in range(K):
    now = teles[now-1]
print(now+1)
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = []
check = {}
key = -1
p = 0
while True:
    ans.append(p)
    check[p] = 0
    p = a[p]-1
    if p in check:
        key = ans.index(p)
        break
r = ans[key:]
mod = (k-key)%len(r)
print(1)
import numpy as np
N, K = (int(i) for i in input().split())  
A = [int(i) for i in input().split()] 

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

lis = [1]
idx = 0
for i in range(N):
  lis.append(A[idx])
  if has_duplicates(lis):
    lis.pop(-1)
    found = lis.index(A[idx])
    lis.append(A[idx])
    break
  idx = A[idx]-1
  
print(lis[found:i+1][(K-found) % (i - found + 1)])
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = k % n
ans = mod
for x in range(len(str(mod))+ 1):
    ans = a[ans]
print(ans)
n,k=map(int,input().split())
p=list(map(int,input().split()))
a=[0]+p
flag=0
bef=1
town=[1]
town_set=set(town)
while flag==0:
    if not a[bef] in town_set:
        town.append(a[bef])
        town_set.add(a[bef])
        bef=a[bef]
    else:
        flag=1
        memo=a[bef]
for j in range(len(town)):
    if town[j]==memo:
        num=j
x=len(town)-num
y=(k-num)%x
print(town[num+y])
N,K = map(int,input().split())
A = list(map(int,input().split()))
A_dict = {i:a for i,a in enumerate(A,1)}

History = [1]
History_set = {1}
now = 1
for _ in range(N):
    now = A_dict[now]
    if now not in History_set:
        History.append(now)
        History_set.add(now)
    else:
        break
#print(History,now)
for e,h in enumerate(History):
    if h == now:
        #print(e)
        break
loop_num = len(History[e:])
#print(loop_num)
num = (K-e)%loop_num
#print(num)
print(History[e:][num])
N, K = map(int, input().split())
A = list(map(int, input().split()))

index = 0
count = 0
node = [1]
while True:
    next = A[index]
    node.append(next)
    if A[next - 1] in node:
        catch_point = A[next - 1]
        break
    index = next - 1

for point in node:
    if point == catch_point:
        break
    else:
        count += 1

print(catch_point, count)
idx =  (K - count) % (len(node) - count)

print(node[count + idx])
n, k = map(int, input().split())
A = list(map(int, input().split()))

Town = []
#まだ訪れていない町は-1
rec = [-1] * (n+1)
idx = 1
#周期までループをまわす
while True:
    if rec[idx] != -1:
        break
    rec[idx] = len(Town)
    Town.append(idx)
    idx = A[idx-1]

#周期前
Exce = rec[idx]
#周期内
Cycl = len(Town) - Exce

if Cycl > k:
    print(Town[k])
else:
    k -= Exce
    k %= Cycl
    print(Town[Exce+k])