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

num = list(map(int, input().split()))
t = [-1] * n
count = 0
s = 1

while t[s - 1] == -1:
  if count == k:
    break

  t[s - 1] = count
  count += 1
  s = num[s - 1]
else:
  print(t.index(t[s - 1] + ((k - t[s - 1]) % (count - t[s - 1]))) + 1)
n,k=map(int,input().split())
A = [int(v)-1 for v in input().split()]

t = 0
done = [t]
itvl = 1
st = 1
for i in range(k):
  t=A[t]
  if t in done:
    itvl = i + 1 - done.index(t)
    for j in range((k - i - 1)%itvl):
      t=A[t]
    print(t+1)
    exit()
  done.append(t)
def main():
    n,k = map(int, input().split())
    a =  list(map(int, input().split()))

    t = 1
    ans = [1]
    while True:
        if a[t-1] in ans :
            start = ans.index(a[t-1])
            break
        ans.append(a[t-1])
        t = a[t-1]

    if k > (len(ans)-start):
        index = k % (len(ans)-start)
        t = ans[index]
    else:
        t = ans[k]
    return print(t)

if __name__ == '__main__':
    main()
from collections import defaultdict as d 

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

s = '1'
i = 1
while True:
	if str(arr[i - 1]) in s:
		s += str(arr[i - 1])
		break
	s += str(arr[i - 1])
	i = arr[i - 1]

if k <= s.index(s[-1]):
	print(s[k])
else:
	k -= (s.index(s[-1]))
	x = len(s) - s.index(s[-1]) - 1
	print(s[s.index(s[-1]):-1][k%x])
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
senni=[1]
dabu=False
while dabu == False:
    if a[senni[-1]-1] in senni:
        dabu = True
        t=len(senni)-(senni.index(a[senni[-1]-1]))
        break
    senni.append(a[senni[-1]-1])
if t<=k and k%t < senni.index(a[senni[-1]-1]):
    ans = senni[k%t+t]
else:
    ans=senni[k%t]
print(ans)
N, K = map(int, input().split())
M = list(map(lambda x: int(x)-1, input().split()))
s = 0
order = [s]
sets = set(order)
while True:
  if M[s] in sets:
    #print(order, M[s])
    
    if K < order.index(M[s]):
      print(order[K-1]+1)
    else:
      K -= order.index(M[s])
      order = order[order.index(M[s]):]
      print(order[K%len(order)]+1)
    break
  order.append(M[s])
  sets.add(M[s])
  s = M[s]
N, K = map(int, input().split())
where_go = list(map(int, input().split()))
now = 0
PATH = []
start = 0
roop = 0
count = 0

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

print(now+1)
import numpy as np
n,k=map(int,input().split())
twon=np.zeros(n)
twon=list(map(int,input().split()))
data=twon[0]
array=[-1]*n
array[0]=1
count=0
for i in range(1,k):
    if array[data-1]==-1:
        array[data-1]=i+1
    else:
        count=(k-i-1)%(i+1-array[data-1])
        array=[-1]*n
        count=k-count
    data=twon[data-1]
    if count!=0:
        break
    
for j in range(count,k):
    data=twon[data-1]
print(data)
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()

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

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

tt = [False] * n
tt[0] = True
ans_l = [1]
i = 1

while True:
    i = a[i-1]
    if tt[i-1] == False:
        tt[i-1] = 1
        ans_l.append(i)
    else:
        break

d = ans_l.index(i)
k -= d
ans = k % (len(ans_l)-d)
print(ans_l[ans+d])
import numpy as np
N,K = map(int,input().split())
A = np.array(list(map(int,input().split())))
num = 0
position = 1
ok = False
position_list = np.array([])
for i in range(N):
    position = A[position -1]
    if position in position_list and ok == False:
        former = list(zip(*np.where(position_list == position)))[0][0]
        later = position
        ok = True
    position_list = np.append(position_list,position)
step = later - former
mod = (K - 1 - former) % step
print(int(position_list[former + mod]))
#!/usr/bin/env python3
# atcoder 
# Türkçe konuşyorum
# Türkler var mı

n, k = map(int, input().split(" "))
val = list(map(int,input().split(" "))) 
dico = {}
i = 0
#for j in range(len(val)):
    #val = int(input().split(" "))
#    dico[j] = val[j]
#print(dico)

while k >= 1:
    if not i < n:
         i = 0
    i += 1
    k -= 1

print(val[i+1])
n,k= list(map(int, input().split()))
li= list(map(int, input().split()))
c=[1]
nex=1
first=0
for i in range(k):
    nex=li[nex-1]
    if nex in c:
        first=c.index(nex)
        break
    c.append(nex)
if first:
    k-=first
    k%=len(c)-first
    print(c[first+k])
else:
    print(c[-1])
import collections

n,k=map(int,input().split())
a=list(map(int,input().split()))
L=[1]
count=1
nowt=1
roops=0
v=[False]*n
while(True):
    if v[a[nowt-1]-1]==True:
        roops=a[nowt-1]
        break

    L.append(a[nowt-1])
    v[a[nowt-1]-1]=True
    nowt=a[nowt-1]
    count+=1

play=0
for i in range(len(L)):
    if L[i]==roops:
        break
    play+=1

rooplen=len(L)-play

ans=L[play+(k-play)%rooplen]


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

tt = [0] * n
tt[0] = 1
ans_l = [1]
i = 1
c = 0

while True:
   # if c == k:
#        break
    c += 1
    i = a[i-1]
    if tt[i-1] == False:
        tt[i-1] = 1
        ans_l.append(i)
    else:
        break

#print(c,k)
d = ans_l.index(i)
k -= d
ans = k % (len(ans_l)-d)
print(ans_l[ans+d])
n,k=map(int,input().split())
A=list(map(int,input().split()))
C=[0]
now=0
for i in range(n):
    now=A[now]-1
    if now==0:
        break
    C.append(now)
    
L=len(C)
print(C[k%L]+1)
N,K = [int(i) for i in input().split()]
A = [int(i)-1 for i in input().split()]
place = [0]
for _ in range(N-1):
    place.append(A[place[-1]])
a = place.index(place[-1])+1
K = a + ((K-a)%(N-a))
print(place[K]+1)
n,k=map(int,input().split())
li=list(map(int,input().split()))
H=[0]*(n)
walk=[]
x=1
l=0
while l==0:
    walk.append(x)
    if H[x-1]==0:
        H[x-1]=1
    else:
        dd=[i for i, j in enumerate(walk) if j == x ]
        l=1
    x=li[x-1]
ran=dd[1]-dd[0]
k-=dd[0]
kk=k%ran
print(walk[dd[0]+kk])
n, k = map(int, input().split())
tele = list(map(int, input().split()))
reached = [-1 for _ in range(n + 1)]
now = 1
count = 0
reached[1] = 0
cycle = False
while cycle == False:
    now = tele[now - 1] #current town
    count += 1
    if reached[now] == -1:
        reached[now] = count
    else:
        cycle = True
roop_long = count - reached[now]
from_roop_in_town = (k - reached[now]) % roop_long

#print(reached, now, roop_long, from_roop_in_town)

for i in range(from_roop_in_town):
    now = tele[now - 1]
print(now)
n, k = map(int, input().split())
a = list(map(lambda x: int(x)-1, input().split()))

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

while True:
    num = a[num]
    if flag[num]:
        li.append(num)
        flag[num] = False
    else:
        break

d = li.index(num)
ans = (k-d)%(len(li)-d)+d
print(li[ans]+1)
n,k = map(int,input().split())
A = [0]+list(map(int,input().split()))
visited = [False for _ in range(n+1)]
pos = 1
x = []
flg = False
for i in range(k):
    if visited[pos] == False:
        x.append(pos)
        visited[pos] = True
        pos = A[pos]
    else:
        flg = True
        pre = x.index(pos)
        loop = x[pre:]
        break
print(loop[(k-pre)%len(loop)] if flg else x[-1])
n,k=map(int,input().split())
A=tuple(map(int,input().split()))
s=1
state=[s]
count={}
for i in range(k+1):
    s=A[s-1]
    if s in count:
        start=state.index(s)
        end=len(state)
        print(state[start+(k-start)%(end-start)])
        break
    else:
        count[state[i]]=i
        state.append(s)
else:
    print(s)
N,K= list(map(int, input().split()))
A= list(map(int, input().split()))

path = [0]*N
a = 0

for j in range(N+1):
    a = A[a]-1
    if path[a]!= 0:
        c = j+1-path[a]
        break
    else:
        path[a] = j+1

mod = K%c

print(path.index(mod)+1)
n,k = map(int,input().split())
a_list = list(input().split())
dum = a_list[0]
k = k - 1
ans_list = a_list[0]
while(1):
    if k == 0:
        print(dum)
        exit()
    dum = a_list[int(dum)-1]
    ans_list = ans_list + dum
    k = k - 1
    if ans_list.count(dum) == 1:
        break
#print(ans_list)
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()))
A = [0] + A
# Dに何回目の移動で到達したか記録してみるか
D = [[] for _ in range(N+1)]

now = 1
i = 0
j = True
while i < K:
    now = A[now]
    D[now].append(i)
    if j and len(D[now]) > 2:
        length = D[now][2] - D[now][1]
        i = K - ((K - i) % length) - 1
        j = False
    i += 1
   
print(now)
N,K = map(int,input().split())
A = list(map(int,input().split()))
c = 0
tmp = 1
List = []
List2 = [0] * N
for i in range(N):
    List.append(A[tmp-1])
    tmp = List[i]
    if(List2[tmp-1] >= 1):
        tmp2 = List2[List[i]-1]-1
        List.pop(-1)
        break
    else:
        c += 1
        List2[tmp-1] = i+1 
List3 = List[tmp2:]
l = len(List3)
if(c%l == 0):
    print(List3[(K%l)-1])
else:
    print(List3[((K%l)+(c%l))%l])
n,k=map(int,input().split())
a=list(map(int,input().split()))
ok=set()
now=0
l=[]
while now not in ok and k:
    l.append(now)
    ok.add(now)
    now=a[now]-1
    k-=1
if k==0:print(now+1);exit()
length=len(ok)-(h:=l.index(now))
l=l[h:]
print(l,h,length)
k=(k)%length

print(l[k]+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))
time = 0
roop = [1]
while(len(set(roop)) == len(roop)):
    roop.append(a[roop[-1]-1])
first = roop.index(roop[-1])
roop.pop(-1)
if(first != 0):
    atama = len(roop) - first
    print(atama)
    gachiroop = roop[atama-1:]
    print(gachiroop[(k-atama+1)%len(gachiroop)])
else:
    print(roop[k%len(roop)])
N,K = map(int,input().split())
A = list(map(int,input().split()))

visited = [0]*N
i = 0
rootin = [0]*N
rootin[0] = 1
count = 0
for _ in range(N):
  count += 1
  visited[i] += 1
  if visited[i] == 2:
    end = count-1
    break
  rootin[count] = A[i]
  i = A[i]-1
start = 0
for i in range(N):
  if rootin[i] == rootin[end]:
    start = i
    break
od = len(rootin[:start])
root = len(rootin[start:end])
if K > od:
  K -= od
  ans = rootin[start+K%root]
else:
  ans = rootin[K-1]
print(ans)
from collections import deque

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 pos in roop:
            k -= k % (k * (k // len(roop)))

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

    route.append(pos)
    k -= 1

print(pos)
n, k = map(int, input().split())
towns = [int(s) for s in input().split()]
pos = []
pos_set = set()
cur = towns[0]

while not cur in pos_set:
    pos.append(cur)
    pos_set.add(cur)
    cur = towns[cur-1]
    
j = pos.index(cur)
print(pos[(k-j-1)%(len(pos)-j) + j])
import sys
n, k = map(int, input().split())
A = list(map(int, input().split()))

route = []
route = [1]
appearance = set()
index = 1
flg = False
for _ in range(n * 10):
    if index in appearance:
        flg = True
        break
    appearance.add(index)
    next_city = A[index - 1]
    route.append(next_city)
    index = next_city

if flg is False:
    print(route[k-1])
    sys.exit()

# 繰り返し個数
duplicate = route[route.index(index):len(route) - 1]
print(duplicate[(k - route.index(index)) % (len(duplicate))])
n,k = map(int,input().split())
A = list(map(int,input().split()))
visited = [False for _ in range(n)]
nx = A[0] - 1
x = []
p = -1
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
if p!=-1:
    k = (k-len(q))%len(r)
    print(r[k-1]+1)
else:
    print(x[k-1]+1)
N, K = map(int, input().split())
A = list(map(int, input().split()))
journey = [1]
dup = {}
for i in range(N+10000):
    journey.append(A[journey[-1] - 1])

end_idx, start_idx = 0, 0
dup = {}
for i, j in enumerate(journey):
    if j in dup:
        end_idx = i
        break
    else:
        dup[j] = 0
for i, j in enumerate(journey):
    if j == journey[end_idx]:
        start_idx = i
        break
b = end_idx - start_idx
m = (K - start_idx) % b
print(journey[start_idx + m])
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=[0 for i in range(n)]
i=0
s[i]=1
while s.count(s[i])==1:
  i+=1
  s[i]=a[s[i-1]-1]

l=[j for j, x in enumerate(s) if x == s[i]] 
x=l[1]-l[0]
y=(k-l[0])%x
print(s[l[0]+y])
N, K = map(int, input().split())
d = dict()
for i, a in enumerate(input().split()):
    d[i+1] = int(a)

a = []
j = 1
for k in range(K):
    j = d[j]
    if j in a:
        hh = a.index(j)
        aaa = (K - k) % (k - hh)
        print(a[hh + aaa - 1])
        exit()
    else:
        a.append(j)
n,k = map(int,input().split())
a = list(map(int,input().split()))
lis = [1]

for i in range(n):
  lis.append(a[lis[i]-1])
  
for j in range(1,n):
  if len(set(lis[:j]).intersection(set(lis[j:]))) != 0:
    roopstartind = j-1
    break
    
for j in range(roopstartind+1,n):
  if lis[j] == lis[roopstartind]:
    roopendind = j
    break
    
rooplen = roopendind - roopstartind
nokori = k-roopstartind

print(lis[roopstartind + (nokori%rooplen)])
n,k=map(int,input().split())
a=list(map(int,input().split()))
p=k%n
o=a[0]-1
for i in range(p):
    o=a[o]
print(o)
n,k = map(int,input().split())
A = list(map(int,input().split()))
dp = [0] * (n+1)
B = []
f = 1
c = 1
for e,i in enumerate(A,1):
    if dp[f-1]:
        ans = e-dp[f-1]
        bal = dp[f-1]
        break
    else:
        dp[f-1]+=c
        B.append(f)
        f = A[f-1]
        c+=1
bal-=1
k-= bal+1
# print(B,bal)
k = (k%ans+1)%ans
print(B[k+bal])
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)
a = raw_input().split()
n = int(a[0])
k = int(a[1])
a = raw_input().split()
m = []
i = 0
while 1:
    if i in m:
        break
    m.append(i)
    i = int(a[i]) - 1
l = len(m)
s = m.index(i)
res = (k - s) % (l - s)
print m[res + s] + 1
n, k = map(int, input().split())
a_list = list(map(int, input().split()))

list_order = []
already = [1]
idx = 0
for i in range(1, k + 1):
    next_city = a_list[idx]
    list_order.append(next_city)
    idx = next_city - 1
    if next_city in already:
        break
    already.append(next_city)
    if idx == 0:
        break
print(list_order[(k % i) - 1])
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
A -= 1

flg = [False for _ in range(N)]
loop = 0
town = []
i = 0
while flg[i] == False:
    town.append(i)
    flg[i] = True
    i = A[i]
    loop += 1
else:
    s = i
    to_s = town.index(s)
    loop -= to_s

print(town[(K - to_s) % loop + to_s] + 1)
# coding: utf-8
# Your code here!
import copy

N, K = map(int, input().split())
List = list(map(int, input().split()))
saki=[1]
ato=[]

i=0
j=1
while i<N:
    saki.append(List[j-1])
    j=List[j-1]
    i+=1

k=copy.deepcopy(N)
ato.append(List[j-1])

start=saki.index(ato[0])

SA=(len(List)-start)+1

o=0
check=[]
while o<SA:
    check.append(List[j-1])
    j=List[j-1]
    o+=1
print(check[(K-start)%SA])
# -*- 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
    if D[P]>0: 
        R=D[P]
        break
    D[P]=c
    if P==0: break
    L[c]=P

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

print(ans)
def main():
    N,K = map(int,input().split())
    A = map(int,input().split())

    doubling = [[0]*(N+1) for _ in range(60)]
    for i,a in enumerate(A):
        doubling[0][i+1] = a
    for i in range(1,60):
        for j in range(1,N+1):
            doubling[i][j] = doubling[i-1][doubling[i-1][j]]
            
    t = 1
    for c in bin(K)[2:]:
        t = doubling[int(c)][t]
    print(t)
main()
n,k=map(int,input().split(' '))
a=list(map(int,input().split(' ')))
loc=1
begin=0
end=0
d={}
for i in range(n):
    loc=a[loc-1]
    if loc in d.keys():
        begin=d[loc]
        end=i+1
        break
    d[loc]=i+1
if k<=end:
    end=0
if end !=0:
    k=(k-begin+1)%(end-begin)+begin-1
loc=1
for _ in range(k):
    loc=a[loc-1]
print(loc)
n, k = map(int, input().split())
a = list(map(int, input().split()))
now = [1]
ans =1

for i in range(2*n):
    if a[now[i]-1] in now:
        break
    now.append(a[now[i]-1])

    
t = len(now)-now.index(a[now[i]-1])
f = now.index(a[now[i]-1])


if k >= f + t:
    p = (k-f)%t
    k = f + p
    
for i in range(k):
      ans = a[ans]
print(ans)
N, K = map(int, input().split())
S = list(num-1 for num in map(int, input().split()))
loc = 0

arrived = [0]*N
ar = set()
cnt = 0
while not loc in ar:
    arrived[cnt] = loc
    ar.add(loc)
    loc = S[loc]
    cnt += 1

if K < cnt:
    print(arrived[K-1])
else:
    for i in range(cnt):
        if arrived[i] == loc:
            K -= i
            roop = cnt-i
            K %= roop
            print(arrived[i+K]+1)
            break
N, K = map(int, input().split())
A = list(map(int, input().split()))
cnt = 0
visited = []
next = 1
while True:
  present = next
  cnt += 1
  visited.append(present)
  next = A[present-1]
  if next in visited:
    ind = visited.index(next)
    l = len(visited[ind:])
    break

rest = (K-cnt) % l
present = visited[ind + rest]
print(present)
n,k=map(int, input().split())
a=[int(x)-1 for x in list(input().split())]
way=[0]
noappear=[i for i in range(n)]
while True:
        way.append(a[way[-1]])
        
        if bool(way[-1] in noappear)==False:
                break

        noappear.remove(way[-1])

last=way[-1]
st=way.index(last)
en=len(way)-1
step=en-st
rest=k%step
if k>=en:
        ans=way[rest]+1

else:
        ans=way[k]+1
print(ans)
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
loop = [0]

for _ in range(K):
    if A[loop[-1]]-1 not in loop:
        loop.append(A[loop[-1]]-1)
    else:
        break

l = len(loop)
print(loop[K % l]+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))
for i in range(n):
    a[i] -= 1
d = {0}
p = 0
while not a[p] in d:
    d.add(a[p])
    p = a[p]
#print(d)
val = [0]
p = 0
for i in range(len(d)):
    val.append(a[p])
    p = a[p]
#print(val)
cnt = 0
p = 0
while val[p] != val[-1]:
    cnt += 1
    p += 1
del val[-1]
k -= cnt
k += 1
k2 = k % (len(d)-cnt)
#print(k2)
#print(cnt)
print(val[cnt + k2-1]+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))
p = 1
t = [1]
u = set()
for i in range(k):
  p = a[p - 1]
  if p not in u:
    t.append(p)
    u.add(p)
  else:
    break
c = t.index(p)
r = len(t) - c
s = len(t) - r
ans = t[(k - s + 1) % r + s - 1]
print(ans)
n, k = map(int, input().split())
a = list(map(int, input().split()))
for i in range(n):
    a[i] -= 1

visited = [0 for _ in range(n)]
i = 0
while visited[i] == 0:
    visited[i] = 1
    i = a[i]

cnt = 0
goal = i
i = goal
while not (i == goal and cnt > 0):
    i = a[i]
    cnt += 1

i = 0
while i != goal:
    k -= 1
    i = a[i]
k = k % cnt
i = goal

for cnt in range(k):
    i = a[i]

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

reached=[1]
i=1
for k in range(n):
    if not A[i] in reached:
        reached.append(A[i])
        i=A[i]
    else:
        t1=reached.index(A[i])
        t2=k+1
        break
#print(reached)
ama=(k-t1)%(t2-t1)
#print(t1,t2,ama)
print(reached[t1+ama])
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]+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))
p = 1
t = [1]
u = set()
for i in range(n):
  p = a[p - 1]
  if p not in u:
    t.append(p)
    u.add(p)
  else:
    break
c = t.index(p)
r = len(t) - c
s = len(t) - r
ans = t[(k + 1 - s) % r + s - 1]
print(ans)
n,k = map(int,input().split())
A = list(map(int,input().split()))
visited = [False for _ in range(n)]
nx = A[0] - 1
x = []
p = -1
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
if p!=-1:
    k = (k-len(q))%len(r)
    print(r[k-1]+1)
else:
    print(x[k]+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+1):
    B.append(A[m])
    m=A[m]-1
    if B[i] in B[:i]:
        M=B[i]
        break

while B[j]!=M:
    j+=1

T=len(B)-j-1
print(B[k-((k-j)//T)*T])
from math import floor as fl

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

for i in range(10000):
    if fl(i * 8 / 100) == A and fl(i / 10) == B:
        print(i)
        break
else:
    print(-1)
A,B=map(int,input().split())

ansA=A*(100/8)#この数か、この数よりでかI数となる
if ansA==int(ansA):#小数点以下０なら
    ansA=int(ansA)
else:
    ansA=int(ansA)+1


ansB=B*10#この数か、この数よりでかい数となる

if ansA>=ansB:
    ans=ansA
else:
    ans=ansB

if int(ans*(8/100))==A and int(ans*(10/100))==B:
    print(ans)
else:
    print(-1)
# coding: utf-8
# Your code here!
import math 
a, b = map(int,input().split())
Flg = True
for i in range(10**5+2):
    if int(i * 0.08) == a and int(i * 0.1) == b:
        print(i)
        Flg = False
        break
if Flg:
    print(-1)
import math

a, b = (int(x) for x in input().split())
ans = 0
i = 0
ok = False

while i < 10000 :
    case_a = math.floor(i * 0.08)
    # print(case_a)
    case_b = math.floor(i * 0.10)
    # print(case_b)

    if case_a == a and case_b == b :
        
        ans = i
        ok = True
        break

    i = i + 1

if ok == False : 
    ans = -1

print(ans)
import math

A, B = map(int, input().split())
min = B * 10
max = B * 10 + 9
flag = False
for i in range(min, max + 1):
    temp = math.floor(i * 0.08)
    if (temp == A):
        flag = True
        ans = i
        break
if flag:
    print(ans)
else:
    print('-1')
tax_a, tax_b = map(int, input().split())
ans = tax_b * 10

for i in range(9):
    if int(ans * 0.08) == tax_a:
        print(ans)
        break
    else:
        ans += 1
else:
    print('-1')
A,B = list(map(int,input().split()))
X = []
mA = int(100/8*A)
MA = int(100/8*(A+1))+1
mB = int(10*B)
MB = int(10*(B+1))+1
 

for i in range(max(mA,mB),min(MA,MB)):
    X.append(i)
#print(X)

f = True
for i in X:
    if int(i*0.08)==A and int(i*0.1)==B:
        print(i)
        f = False
        break
if f:
    print(-1)
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

a, b = input_nums()

a_rate = 0.08
b_rate = 0.1

al = int(a / a_rate)
ar = int((a+1) / a_rate)
bl = int(b / b_rate)
br = int((b+1) / b_rate)

ans = 0
if bl <= al and al < br:
    ans = al
elif al <= bl and bl < ar:
    ans = bl
else:
    ans = -1

if not ans == -1 and int(ans*0.08) < a:
    ans += 1
    if int(ans*0.1) > b:
        ans = -1
print(ans)
import sys
import math

read = sys.stdin.readline
S = read()

a, b = map(int, S.split())

fin = False
for i in range(1000+1):
    if a == math.floor(i * 0.08):
        if b == math.floor(i * 0.1):
            print(i)
            fin = True
            break
if not fin:
    print(-1)
a,b=map(int,input().split())
i=1
while(1):
    p=int(i*0.08)
    q=int(i*0.1)
    if p==a and q==b:
        break
    i+=1
    if i>=100000:
        i=-1
        break
print(i)