peterverebics/CodeNet_Python_118 huggingface dataset:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [0] + A

L = [[] for _ in range(N)]
L[0] = 1
prev = 1
count = 1
for i in range(1, N):
    now = A[prev]
    if now == 1:
        break
    else:
        L[i] = now
        prev = now
        count += 1
#        print(L)
L = L[:count]
#print(L)
print(L[K%count])
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [item - 1 for item in a]

passed = [-1] * n
current_town = 0
passed[0] = 0 
for i in range(1, 1000000):
    current_town = a[current_town]
    if passed[current_town] > -1:
        start = passed[current_town]
        period = i - start
        break
    else:
        passed[current_town] = i

m = (k - start) % period
for i in range(m):
    current_town = a[current_town]

print(current_town + 1)
n, k = map(int, input().split())
targets = list(map(int, input().split()))

seen = dict()
goal = dict()

seen[1] = 0
goal[0] = 1

prev = targets[0]
startFlag = False
for i in range(1, k):
    seen[prev] = i
    goal[i] = prev
    current = targets[prev - 1]
    if current in seen:
        before = seen[current]

        finalGoal = k % (i + 1 - before)
        print(goal[finalGoal])
        exit()
    prev = current

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

visited = {}
for i in range(1, N+1):
    visited[i] = False

i2p = {}
p2i = {}
i = 0
cur = 1
while True:
    i2p[i] = cur
    p2i[cur] = i
    visited[cur] = True
    if visited[A[cur]]:
        break
    cur = A[cur]
    i += 1

start_loop = p2i[A[cur]]
end_loop = len(i2p)
num_loop = end_loop - start_loop

if K < num_loop:
    print(i2p[K])
else:
    print(i2p[(K-start_loop)%num_loop+start_loop])
n, k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]

visited = [0]
x = 0
T = -1
i = 0
while T < 0:
  x = a[x]
  i += 1
  if x in visited:
    for j in range(i):
      if visited[j] == x:
        first = j
        break
    T = i - j
    break
  else:
    visited.append(x)
mod = (k-first) % T
print(visited[first+mod]+1)
n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  a = b
  b = A[a-1]
  B.append(b)
  if len(B) == n:
    ans = 0
aaa = k%n
print(B[aaa-1])
N,K = map(int,input().split())
A = list(map(int,input().split()))
B =[1]
i = 0
Flag = True
a = A[0] 
i +=1 #1回
B.append(a)
while i < K and Flag == True:
  a = A[a-1]
  if a in B:
    Flag = False
  B.append(a)
  i += 1

t = B.index(a)
l = i-t
if Flag == True:
  print(a)
else:
  s = (K-t)%l
  a =A[t+s]
  print(a)
n,k=map(int,input().split())
a=input().split()
go=[]
fin=0
now=1
loop=0

while len(go)==len(set(go)):
  go.append(a[now-1])
  now=int(a[now-1])
  
fin=go.index(go[len(go)-1])
del(go[len(go)-1])
loop=len(go)-fin
k-=fin
k-=1

print(str(int(go[k%loop+fin])))
N, K = map(int, input().split())
A = list(map(int, input().split()))

takahashi = [1, ]

for i in range(K):
    to = A[takahashi[i] - 1]
    if to in takahashi:
        l = (K - i)%(i - takahashi.index(to) + 1)
        for j in range(l):
            takahashi.append(to)
        break
    takahashi.append(to)

print(takahashi[-1])
from collections import Counter
N, K = map(int,input().split())
tele = list(map(int, input().split()))

#A1から順にワープした時の経由順
next_,goal = 1,[]
while goal.count(next_)<1:
  goal.append(next_)
  next_ = tele[next_-1]
  #print(goal.count(next_))
  
i,loop = -1,[]
while goal[i]!=next_:
  loop.insert(0,goal[i])
  #print(goal[i])
  i-=1

loop.insert(0,next_)
amari = len(goal)-len(loop)
print(loop[(K-amari)%len(loop)])
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 -start

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())
an = list(map(int, input().split()))
bn = [-1]*(n+1)

now=an[0]
bn[1]=0
for i in range(1, k+1):
    now = an[now-1]
    if bn[now] >0:
        rp =i-bn[now]
        p= k%rp
        for j in range(p+1):
            now = an[now-1]
        print(now)
        exit(0)
    bn[now]=i
print(now)
n,k = map(int,input().split())
A = list(map(int,input().split()))
visted = [1]
bre = 0

for i in range(k):
    a = A[visted[-1]-1]
    
    if a not in visted:
        visted.append(a)
    else:
        visted.append(a)
        bre = 1
        break
        
loop_start = visted.index(visted[-1])+1

if bre == 0:
    print(visted[k-1])
else:
    x = len(visted[:loop_start])
    visted = visted[loop_start:]
    print(visted[(k-x)%len(visted)])
n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))

ls = [-1] * n
now = 0
cnt = 0
path = []
while 1:
    if ls[now] != -1:
        b = ls[now]
        break
    ls[now] = cnt
    path.append(now)
    now = a[now]
    cnt += 1
loop = path[b:]
if k <= b:
    print(path[k])
else:
    print(loop[(k - b) % len(loop)] + 1)
N, K = map(int, input().split())
a = [int(i) for i in input().split()]

pos = a[0]
p = []
while pos not in p:
    p.append(pos)
    pos = a[pos-1]

idx = p.index(pos)
k = K - idx
p = p[idx:]

print(p[k%len(p) - 1])
N, K = map(int, input().split())
A = list(map(int,input().split()))

temp = [[0] * 2 for i in range(len(A))]
index = 0
previous = 0
count = 0
while True:
    temp[index][0] += 1
    
    if temp[index][0] == 2:
        previous = temp[index][1]
        count = count - temp[index][1]
        break

    temp[index][1] = count
    count += 1
    index = A[index] - 1

places = []
place_index = index
for i in range(count):
    places.append(place_index + 1)
    place_index = A[place_index] - 1

K -= previous
print(places[K % count])
N,K=map(int,input().split())
A=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())
data = list(map(int, input().split()))
town = [0] * N
temp = []
once = []



p = 1 #pointer
for i in range(N):
  temp.append(p-1)
  if town[p-1] == 1:
    break
  else:
    town[p-1] = 1
    p = data[p-1]
    #print(town)
    #print("pointer = {}".format(p))

#print(temp)

while True:
  if temp[0] == temp[-1]:
    break
  else:
    once.append(temp[0])
    temp.pop(0)


temp.pop(-1)
#print(once)
#print(temp)

K = K - len(once)
K = K % len(temp)
print(temp[K]+1)
N,K = map(int, input().split())
A = list(map(int, input().split()))
p = 1
p_hist = []
while p not in p_hist:
    p_hist.append(p)
    p = A[p-1]
end_loop = p_hist[-1]
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [x-1 for x in A]

town = 0
cnt = 0

memo = []

for i in range(n):
    town = A[town]
    memo.append(town)
    if town == 0:
        break

loop = len(memo)

print(memo[k%loop-1]+1)
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 = []
Flag = False
while c<=K:
    if i in check:
        flag = True
        break
    else:
        check.append(i)
        i = A[i]

X = check[check.index(i):]
print(1+X[(K-c)%len(X)] if flag==True else print(i+1))
N, K = map(int, input().split())
A = list(map(int, input().split()))
teleport = dict()
for i, a in enumerate(A):
    teleport[i+1] = a

state = 1
connect = [1]
looplen= 10**18
for n in range(N):
    state = teleport[state] 
    if state in connect:
        loop_init = connect.index(state)
        looplen = n-connect.index(state)+1
        break
    else:
        connect.append(state)
        
index = (K-loop_init)%looplen
end = connect[loop_init+index]
print(end)
n, k = map(int, input().split())
teleporter = list(map(lambda x: int(x) - 1, input().split()))

destinations = {0: 0}
curr_city = 0
cycle_start = -1
while True:
    next_city = teleporter[curr_city]

    if next_city in destinations:
        cycle_start = destinations[next_city]
        break

    destinations[next_city] = len(destinations)
    curr_city = next_city

if cycle_start == -1:
    print(curr_city + 1)
else:
    for i in range((k - cycle_start) % (len(destinations) - cycle_start) + 1):
        curr_city = teleporter[curr_city]
    print(curr_city + 1)
def resolve():
    N, K = list(map(int, input().split()))
    A = list(map(lambda x: int(x)-1, input().split()))
    routes = [0]
    current = 0
    visited = [False for _ in range(N)]
    visited[0] = True
    while visited[A[current]] is False:
        current = A[current]
        routes.append(current)
        visited[current] = True
    leftlen = routes.index(A[current])
    repeat = routes[leftlen:]
    # print(leftlen)
    # print(routes)
    # print(repeat)
    print(repeat[(K-leftlen)%len(repeat)]+1 if K >= len(routes) else routes[K])


if '__main__' == __name__:
    resolve()
def calc(i, k):
    ans_list = [0]
    j = i
    count = 1
    i = a[i]
    ans_list.append(i)
    while (i != j) & (count < n):
        i = a[i]
        count += 1
        ans_list.append(i)
    return count, ans_list

n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]

count, ans_list = calc(0, n)
print(ans_list[k % count] + 1)
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

l = [A[0]]
a = A[0] - 1
for _ in range(0, N):
    if A[a] not in l:
        l.append(A[a])
        a = A[a] - 1
    else:
        break
        a = A[a]
n = len(l)
print(l)
b = 0
for i in range(n):
    if l[i] == a:
        del l[0:i]
        b = i - 1
        break
N = len(A)
S = (K - b) % N
ans = A[S - 1]

print(ans)
N , K = list(map(int,input().split()))
A = list(map(int,input().split()))
A.insert(0,0)
local = 1
tmp = []
tmp.append(local)
for k in range(N):
    local = A[local]
    if local in tmp:
        result = len(tmp) - tmp.index(local)
        break
    else:
        tmp.append(local)

for k in range((K-tmp.index(local)) % result):
    local = A[local]
print(local)
import sys

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

visited = [False] * (N+1)
s = 1

while not visited[s]:
    visited[s] = True
    s = A[s]

a = 1

while a != s:
    K -= 1
    if K == 0: 
        print(a)
        sys.exit()
    a = A[a]

visited = [False] * (N+1)
length = 0
while not visited[s]:
    visited[s] = True
    s = A[s]
    length += 1
    
k = K % length
while k:
    s = A[s]
    k -= 1
    
print(s)
n, k = map(int, input().split())
A = [0] + list(map(int, input().split()))
cnt = 0
cur = 1
memo = []
kumi = set([])
for i in range(2 * (10 ** 5) + 5):
    cur = A[cur]
    cnt += 1
    memo.append(cur)
    if cur in kumi:
        break
    kumi.add(cur)
k -= len(memo)
for j in range(cnt):
    if memo[j] == cur:
        start = j
        break
loop = memo[start:]

loop_num = i - start
print(loop[k % loop_num])
from collections import defaultdict
import sys
sys.setrecursionlimit(10**6)
n , k = map(int , input().split())
arr = list(map(int , input().split()))
dic = defaultdict(int)
for i in range(1,n+1):
	dic[i] = arr[i-1]
cycle = []
def dfs(node):
	if node in cycle:
		return node
	else:
		cycle.append(node)
		k = dfs(dic[node])
	return k
cyele = dfs(1)
ind = 0
offset = 0
for i in range(len(cycle)):
	if cycle[i] == cyele:
		ind = i
		break
	offset+=1
#rem = k - offset
csize = len(cycle[ind:])
rem=k%csize

count = 0
curr = 1
while count != rem:
	curr = dic[curr]
	count+=1
print(curr)
n,k = map(int,input().split())
li = list(map(int,input().split()))
count = 2
li2 = [1]
li2.append(li[0])
for i in range(n):
    li2.append(li[li2[i+1]-1])
    count += 1
    if len(set(li2)) != count:
        break
if count == n + 2:
    print(li2[-2])
else:
    s = li2.index(li2[-1])
    del li2[0:s]
    del li2[-1]
    print(li2[(k-s) % len(li2)])
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
cur = 0
i = 0
loop = 0
list = []

while (i <= K):
  i = i+1
  cur = A[cur-1]
  if(loop == 0):
    if (cur in list):
#      loop = len(list)
#      print("loop", loop)
      loop = len(list) - list.index(cur)
      if (K > 100):
        K= K - ((K // loop))*loop
    list.append(cur)
#  print(cur)

print(cur)
n,k =map(int,input().split())
a = list(map(int,input().split()))
l = 0  
count = 0

for i in range(k) :
    l = a[l]-1 
    count = +1
    if l == 0 :
        x = k%count
        for i in range(x) :
            l = a[l]-1
            
        print(l+1)
        exit()
        
print(l+1)
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [x-1 for x in A]

town = 0
cnt = 0

memo = []
visited = [False] * n
visited[0] = -1

start = 0
for i in range(n*2):
    town = A[town]
    memo.append(town)
    if visited[town] != False:
        start_cnt = i + 1
        loop = i - visited[town]
        break
    else:
        visited[town] = i

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

print(town+1)
n,K=list(map(int,input().split()))
a = list(map(int,input().split()))
b = []
c = [0]*len(a)

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

city = [i for i in range(n)]

egde = [-1] * n
tmp = a[0]
for i in range(n):
    tmp = a[tmp - 1]
    egde[tmp - 1] = i

tmp = a[0]
for i in range(k % (n - egde.count(-1)) - 1):
    tmp = a[tmp - 1]

print(tmp)
n, k = map(int, input().split())
list_A = list(map(int, input().split()))
list_MAP = []
set_MAP = set()
x = 1
while True:
    list_MAP.append(x)
    set_MAP.add(x)
    x = list_A[x-1]
    if x in list_MAP:
        break


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

if p < 0:
    print(list_MAP[k-1])
else:
    list_MAP = list_MAP[list_MAP.index(x):]
    print(list_MAP[p % q])
n,k = map(int,input().split())
A = list(map(int,input().split()))
sub = [0]*n
ans = 1
cir = 0
a = 0
for i in range(n):
  sub[i] = ans
  if A[ans-1] in sub:
    a = sub.index(A[ans-1])
    cir = i+1 - a
    break
  else:
    ans = A[ans-1]
if sub[-1] != 0:
  print(sub[k%n])
  exit(0)
print(sub[a + (k - a) % cir])
n,k=map(int,input().split())
a=list(map(int,input().split()))

s=1
ans=0
v={}
for j in range(len(a)):
	if(s-1 in v):
		break
	else:
		v.update({s-1:ans})
		s=a[s-1]
		ans+=1
lens=ans-v[s-1]
k=(k-ans)%lens
for j in range(k):
	s=a[s-1]
print(s)
N, M = map(int, input().split())
P = [i -1 for i in map(int,input().split())]
ss = set([])
point = 0
for i in range(N):
    if point in ss:
        break
    ss.add(point)
    point = P[point]
S = [0]
points = 0
for i in range(len(ss)-1):
    points = P[points]
    S.append(points)
loop = S[S.index(points):]
ll = len(S)
l = len(loop)
if ll > M:
    print(S[M]+1)
elif ll == M:
    print(points+1)
else:
    print(loop[(M-ll)%l]+1)
N,K = map(int,input().split())
A = list(map(int,input().split()))
if A[0] == 1:
    print(1)
    exit()
B = [A[0]]
num = 0
C = 0
for i in range(N):
    num = A[num-1]
    if num in B:
        c = num
        break
    B.append(num)
C = [c]
num = c
for i in range(N):
    num = A[num-1]
    if num == c:
        break
    C.append(num)
K -= len(B)
print(C[K%len(C)-1])
N,K = map(int,input().split())
A = list(map(int,input().split()))

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

  i = next-1

K -= len(route)
print(cycle[K%len(cycle)])
from collections import Counter
N, K = map(int,input().split())
tele = list(map(int, input().split()))

#A1から順にワープした時の経由順
now = 0
goal,loop,visit = [],[],[0]*N

while visit[now]!=2:

  if visit[now]==0:
    goal.append(now)
  else:
    loop.append(now)
    
  visit[now] += 1
  now = tele[now]-1

amari = len(goal)-len(loop)
print(loop[(K-amari)%len(loop)]+1)
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[0]*n
c=[0]*n
total=0
b[0]+=1
next=a[0]
while True:
  total+=1
  if b[next-1]==0:
    b[next-1]+=1
    c[next-1]+=total
    next=a[next-1]
  else:
    total1=total
    k-=(c[next-1])
    total-=c[next-1]
    start=next-1
    break
if total1<=k:
  start=0
  for i in range(k):
    start=a[start]-1
  print(start+1)
else:
  k%=total
  for i in range(k):
    start=a[start]-1
  print(start+1)
N,K = map(int,input().split())
A = list(map(int,input().strip().split()))
B=1
list=[]
for i in range(N):
    if B in list:
        num2=list.index(B)
        break
    else:
        list.append(B)
        B=A[B-1]

del list[:num2]
num=(K-num2)%len(list)
print(list[num])
n, k = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = [False for _ in range(n+1)]
B[1] = True
C = [1]

for i in range(1,n):
    C.append(A[C[-1]])
    #print(C)
    B[C[-1]] = True
    if B[A[C[-1]]] == True:
        C.append(A[C[-1]])
        break

num = i + 1
s = C.index(C[-1])
N = num - s

if k < N:
    print(C[k])
else:
    print(C[k%N])
n,k=map(int,input().split(' '))
a=list(map(int,input().split(' ')))
loc=1
begin=0
end=0
d={1:0}
for i in range(n+1):
    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==begin+1:
    k=begin
    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()))
b = [0 for i in range(n)]
b[0] += 1
i = 0
ans = 0
c = a[0]
while True:
    b[c-1] += 1
    i += 1
    if b[c-1] > 1:
        ans = c
        break
    c = a[c-1]
c = a[0]
j = 0
while True:
    j+=1
    if c == ans:
        break
    c = a[c-1]
if c == 1:
    j = 0
k -= j
k %= (i-j)
for i in range(k):
    c = a[c-1]
print(c)
N, K = map(int, input().split())
A = list(map(int, input().split()))

teleport = [-1] * (N+1)
teleport[0] = 1
visited = [(False, -1)] * (N+1)
visited[1] = (True, 0)

for i in range(1, N+1):
    next_town = A[teleport[i-1]-1]
    if visited[next_town][0]:
        before = visited[next_town][1]
        break
    teleport[i] = next_town
    visited[next_town] = (True, i)


period = i - before
offset = (K - before) % period
print(teleport[before + offset])
from collections import deque
N, K = map(int, input().split())
memo = [-1]*(N+1)
junban = deque()
A = [0]+list(map(int, input().split()))
i = 0
x = 1
while memo[x] < 0:
  junban.append(x)
  memo[x] = i
  x = A[x]
  i += 1
T = i-memo[x]
if K < T:
  print(junban[K])
else:
  print(list(junban)[memo[x]:][(K-memo[x])%T])
n,k,*a=map(int,open(0).read().split())

now=1
if k<=n:
  for i in range(k):
    now=a[now-1]
  print(now)
  exit()
# 絶対ループする
visited=[-1]*n
for i in range(4*10**5):
  now=a[now-1]
  if visited[now-1]==-1:
    visited[now-1]=i
  else:
    c,d=visited[now-1],i
    break

k=(k-c)%(d-c)+c
for i in range(k+c):
  now=a[now-1]
print(now)
n,k = map(int,input().split())
alist = list(map(int,input().split()))

town = 1
numberlist = []
count = 0
count2 = 0
for i in range(k):
    town = alist[town-1]
    if town in numberlist:
        break
    else:
        numberlist.append(town)
        count += 1
for i in numberlist:
    if i == town:
        break
    else:
        numberlist.remove(i)
        count2 += 1
k = k - count2
x = k % len(numberlist)
print(numberlist[x-1])
import sys
input=lambda: sys.stdin.readline().rstrip()
n,k=map(int,input().split())
A=[int(i) for i in input().split()]
B=[0]
S=set()
S.add(0)
cur=A[0]-1
while True:
  if cur in S:
    break
  else:
    S.add(cur)
    B.append(cur)
    cur=A[cur]-1
d=len(B)
ind=B.index(cur)
print(B[(k-ind)%(d-ind)+ind]+1)
n, k=map(int,input().split())
a=list(map(int,input().split()))

now_in=0
count=0
for i in range(n):
    now_in=a[now_in]-1
    count+=1
    if now_in==0:
        break

k = k%count

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

now_in +=1
print(now_in)
# coding: utf-8
# Your code here!

n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[1]
i=0
for _ in range(n):
    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])
n,k=map(int,input().split())
a_list=list(map(int,input().split()))

flag=[0]*n
place=[0]*n
p=0
for i in range(len(a_list)+1):
    if flag[p]>0:
        rep=i
        replis=place[flag[p]-1:i]
        st=p+1
        break
        
    else:
        place[i]=p+1
        flag[p]=i+1
        p=a_list[p]-1

if i == n+1:
    print(place[-1])
else:
    print(replis[(k-st-len(replis))%len(replis)])
n, k = map(int, input().split())
lst = list(map(int, (input().split())))
lst = [_ - 1 for _ in lst]

visited = [0] * n
flg = 1
i = 0
now = 0
while flg:
    if visited[now] == 0:
        i += 1
        visited[now] = i
        now = lst[now]
    else:
        flg = 0

k_bk = k - (i - visited[now])
k_bk =  k_bk // i

lft = k - i* k_bk

if lft ==0:
    print(now+1)
else:
    for _ in range(lft):
        now = lst[now]
    print(now+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))

a.insert(0, 'hoge')
amari = k % n

tmp = 1
if k >= n:
    for i in range(1, n + amari + 1):
        tmp = a[tmp]
        # print(i, tmp)
else:
    for i in range(1, k+1):
        if tmp != k:
            tmp = a[tmp]
        elif tmp == k:
            print(tmp)
            exit()

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

history = []
i = 0
now = 1
next_ = 0
while True:
  if now in history:
    break  
  else:
    history.append(now)
  
  next_ = A[now-1]
  now = next_
  i+=1

loop = history.index(now)
count = len(history[loop:])
K -= len(history) - count
K = K - (K//count)*count
print(history[loop:][K])
from collections import defaultdict
N, K = map(int, input().split())
A = list(map(int, 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]] 
#print(fast, slow)
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)
#print(slow, r)
while r:
    slow = adj[slow]
    r -= 1

print(slow)
n, k = map(int, input().split())
a = [i-1 for i in list(map(int, input().split()))]

l = []
used = [0 for _ in range(n)]

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

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

print(l[(k - p - 1) % len(l)] + 1)
n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))

history = []
visited = set()
cur = 0
for i in range(n):
    if cur in visited:
        x = history.index(cur)
        y = len(history) - x
        print(history[x+(k-x)%y] + 1)
        exit()

    visited.add(cur)
    history.append(cur)
    cur = a[cur]

print(history[k-1]+1)
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-check.index(i))%(len(check)-check.index(i))])
else: 
    print(i+1)
n,k=map(int,input().split())
l=list(map(int,input().split()))
list=[l[0]]
set=set()
num=0
while True:
  if l[list[num]-1] in set:
    break
  list.append(l[list[num]-1])
  set.add(l[list[num]-1])
  num+=1
y=list.index(l[list[num]-1])
x=len(list)-y
ans=list[y:]
print(ans[(k-y)%x-1])
n,k = map(int,input().split())
a = list(map(int,input().split()))

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

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

#print(ans_l,i,tt)
d = ans_l.index(i)
k -= d
ans = k % (len(ans_l)-d)
print(ans_l[ans+d])