peterverebics/CodeNet_Python_118 huggingface dataset:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
 
check=[-1 for i in range(N)]
  
temp=1
circle=[]
for i in range(K):
  temp=A[temp-1]
  if check[temp-1]==-1:
    check[temp-1]=i
  else:
    circle.append(i-check[temp-1])
    #print(circle)
    break
  #print("temp",temp)
#print((K-i)%len(circle)-1)
if len(circle)!=0:
  amari=(K-i)%circle[0]
  temp=1
  for j in range(i+amari):
    temp=A[temp-1] 

print(temp)
N,K = map(int,input().split())
A = [int(v)-1 for v in input().split()]
p = 0
memo = {}
for i in range(K):
  if p in memo:
    interval = i-memo[p]
    rest = (K-i-1)%interval
    for j in range(rest+1):
      p=A[p]
    print(p+1)
    exit()
  else:
    memo[p] = i
  p = A[p]
print(p+1)
def solve(N,K,A):
    visited = [None]*N
    cur = 0
    for i in range(K):
        if visited[cur] is None:
            visited[cur] = i
            cur = A[cur]-1
        else:
            break
    else:
        print(cur+1)
        return

    K -= i
    K %= i-visited[cur]

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

    print(cur+1)


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

solve(N,K,A)
n,k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]
ans = 0
cnt = 0

x = [-1] * n
y = -1

while cnt < k:
    if x[ans] == -1:
        x[ans] = cnt
        ans = a[ans]
        cnt += 1
    else:
        y = x[ans] + (k-cnt) % (cnt-x[ans])
        break

if y == -1:
    print(ans+1)
else:
    print(x.index(y) +1)
N,K = (int(x) for x in input().split())
A = list(map(int, input().split()))
newlist = [1,A[0]]
num = A[0] - 1
ans = [0]*N
ans[0] = 1
ans[A[0]-1] = 1
b = 0
c = 0
r = 0
for i in range(K):
    if(ans[A[num]-1] == 1):
        b = A[num]
        break
    newlist.append(A[num])
    ans[A[num]-1] = 1
    num = A[num]-1
for i in range (len(newlist)):
    if(newlist[i] == b):
        c = i
r = len(newlist)-c
a = (K-c)%r
print(newlist[c+a])
n, k = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))
bit = k.bit_length()
table = [[0]*bit for _ in range(n)]
for i in range(n):
    table[i][0] = A[i]

for i in range(1, bit):
    for j in range(n):
        table[j][i] = table[table[j][i-1]][i-1]

ans = 0
for i in reversed(range(bit)):
    if (1 << i) <= k:
        k -= 1 << i
        ans = table[ans][i]
print(ans+1)
def main():
    N, K = map(int, input().split())
    A = list(map(lambda x: int(x)-1, input().split()))

    visited = [0] * N
    way = []
    now = 0

    while True:
        if visited[now] == 1: break
        if K == 0: return print(now+1)
        visited[now] = 1
        way.append(now)
        destination = A[now]
        K -= 1
        now = destination

    loop = way[way.index(now):len(way)]
    # print(loop)
    # print(K)
    print(loop[K%len(loop)]+1)



if __name__ == '__main__':
    main()
N,K = map(int,input().split(" "))
A = list(map(int,input().split(" ")))
A.insert(0,0)
rireki = [1]
counter = 1
now = 1
flag = [0 for _ in range(N + 1)]
flag[1] = 1
while(flag[A[now]] == 0):
    counter += 1
    rireki.append(A[now])
    flag[A[now]] = counter
    now = A[now]
if K < flag[A[now]]:
    print(rireki[K])
else:
    K += counter - (flag[A[now]] - 1)
    K -= flag[A[now]] - 1
    K %= counter - (flag[A[now]] - 1)
    print(rireki[flag[A[now]]- 1 + K])
n,k = map(int,input().split())
a = list(map(int,input().split()))

t = 1
tl = [1]
ts = {1}
for i in range(len(a)):

    t = a[t-1]
    if t in ts:
        break
    else:
        tl.append(t)
        ts.add(t)

tl2 = tl[tl.index(t):len(tl)]


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

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

rireki = [-1 for i in range(N)]
rireki[0] = 0
ima = 0
kuri = 0
start = 0
end = 0
for i in range(K):
  saki = A[ima]
  if rireki[saki - 1] == -1:
    rireki[saki - 1] = i + 1
    ima = saki - 1
  else:
    ima = saki - 1
    kuri = 1
    start = rireki[saki - 1]
    end = i + 1
    break
    
if kuri == 1:
  KK = (K - start) % (end - start)
  for i in range(KK):
    saki = A[ima]
    ima = saki - 1

print(ima+1)
n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]
visit = {0}
tmp = 0
a_list = [0]
ans = True
for i in range(k):
    tmp = a[tmp]
    if tmp in visit:
        left = a_list.index(tmp)
        ans = False
        print(a_list[left + (k - left) % (i + 1 - left)] + 1)
        break
    else:
        visit.add(tmp)
        a_list.append(tmp)
if ans:
    print(tmp + 1)
N,K=map(int,input().split())
t=list(map(int,input().split()))
g=[1]
h={1}
for i in range(N-1):
  x=t[g[i]-1]
  g.append(x)
  if x in h:
    break
  h.add(x)
if K>len(g):
  a=g.index(g[-1])
  b=len(g)-a-1
  K=(K-a)%b+a
print(g[K])
def resolve():
	n, k = map(int, input().split())
	a = list(map(int, input().split()))
	rec = [True]*(n+2)
	now = 0
	stp = [1]

	while rec[now]:
		rec[now] = False
		now = a[now] - 1
		stp.append(now+1)
	before_loop = stp.index(now+1)
	loop = stp[before_loop:-1]
	if k <= (before_loop):
		print(stp[k])
	else:
		r = (k - before_loop) % len(loop)
		print(loop[r])
resolve()
n, k = map(int, input().split())
A = list(map(int, input().split()))
D = [0] * (n+1)
now = 1
for i in range(1, k+1):
    tmp = A[now-1]
    if not D[tmp]:
        now = tmp
        D[now] += i
    else:
        pre = D[tmp]
        loop = i - D[tmp]
        if loop>1:
            now=D.index((k - pre) % loop + pre)
        else:
            now=tmp
        break
print(now)
N, K = map(int, input().split())
As = list(map(int, input().split()))
As = [A-1 for A in As]
Vs = [False]*N
p = 0
Rs = [p]
Vs[p] = True
for i in range(N+3):
  n = As[p]
  Rs.append(n)
  if Vs[n]:
    break
  p = n
  Vs[p] = True
p0 = Rs.index(Rs[-1])
#print(Rs, p0)
if K <= p0:
  r = Rs[K]
else:
  r = Rs[p0+(K-p0)%(len(Rs)-p0-1)]
print(r+1)
N, K = map(int, input().split())
A = list(map(int, input().split()))
visited_set = set([1])
visited_list = [1]
i = 0
loop_pos = 0
for _ in range(len(A)):
    if A[i] in visited_set:
        while A[i] != visited_list[loop_pos]:
            loop_pos += 1
        break
    visited_set.add(A[i])
    visited_list.append(A[i])
    i = A[i] - 1
if K < loop_pos:
    print(visited_list[K])
else:
    loop_len = len(visited_list) - loop_pos
    K -= loop_pos
    pos = loop_pos + (K % loop_len)
    print(visited_list[pos])
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = [-1 for i in range(N+1)]
B[1] = 0
n = 1
m = 0
while 1:
  n = A[n]
  m += 1
  if m == K:
    print(n)
    break
  if B[n] == -1:
    B[n] = m
  else:
    a = (K - m) % (m - B[n])
    for i in range(a):
      n = A[n]
    print(n)
    break
n, k = map(int, input().split())
lst = [int(i) for i in input().split()]

tele_lst = []
town_set = set()
town = 1
while True:
  if town not in town_set:
    tele_lst.append(town)
    town_set.add(town)
  else:
    break
  town = lst[town - 1]
ind = tele_lst.index(town)
length = len(town_set)
circle = length - ind
#print(ind, length)
#print(tele_lst)
if k < ind:
  town = tele_lst[k]
  print(town)
else:
  k -= ind
  k %= circle
  #print(k)
  town = tele_lst[ind + k]
  print(town)
n, k = map(int, input().split())
a = list(map(int, input().split()))


g = [aa - 1 for aa in a]

root = []
seen = set()

cur = 0
while True:
    seen.add(cur)
    root.append(cur)

    cur = g[cur]
    if cur in seen:
        break

for i in range(len(root)):
    if root[i] == cur:
        break

p = i
n_loop = len(root) - p

if k <= p:
    print(root[k] + 1)
else:
    idx = ((k - p) % n_loop + p) % len(root)
    print(root[idx] + 1)
import sys
readline = sys.stdin.readline

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

while K:
  if K & 1:
    now = A[now]
  nex = [0] * (N + 1)
  for i in range(N + 1):
    nex[i] = A[A[i]]
  A = nex
  K >>= 1
  
print(now)
N,K=map(int,input().split())
A=[0]+list(map(int,input().split()))
c=1
while K:
  if K&1:
    c=A[c]
  A=[A[A[i]] for i in range(len(A))]
  K>>=1
print(c)
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))

now = 1
visited = []
visited_set = set()
flag = False

for i in range(1, K+1):
    visited.append(now)
    visited_set.add(now)
    now = A[now]
    if now in visited_set:
        loop_start = visited.index(now)
        loop_length = i - loop_start
        flag = True
        break
else:
    print(now)

if flag:
    print(visited[loop_start + (K - loop_start) % loop_length])
n, k = map(int, input().split())
teleport_list = list(map(int, input().split()))
t_cycle = [1]
c_set = set([1])
now = 1
while len(t_cycle) == len(c_set):
  now = teleport_list[now-1]
  t_cycle.append(now)
  c_set.add(now)

start_cycle = t_cycle[-1]
before_cycle_len = t_cycle.index(start_cycle)
cycle_len = len(t_cycle) - t_cycle.index(start_cycle) - 1

if k <= before_cycle_len:
  print(t_cycle[k])
else:
  print(t_cycle[(k-before_cycle_len)%cycle_len + before_cycle_len])
import sys

read = sys.stdin.read
readline = sys.stdin.readline

N, K, *A = map(int, read().split())
A = [i - 1 for i in A]

check = [False] * N
seq = []
here = 0
while True:
    if K == 0:
        print(here + 1)
        exit()
    if check[here]:
        break
    seq.append(here)
    check[here] = True
    here = A[here]
    K -= 1

seq = seq[seq.index(here):]
circ = len(seq)
K %= circ
print(seq[K] + 1)
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=[False]*n
visited[now-1]=-1
for i in range(4*10**5):
  now=a[now-1]
  if visited[now-1]==False:
    visited[now-1]=i
  else:
    c,d=visited[now-1],i
    break

k=(k-c)%(d-c)+c
while k<n:
  k+=(d-c)
now=1
for i in range(k):
  now=a[now-1]
print(now)
N, K = list(map(int, input().split()))
A = list(map(lambda x: int(x) - 1, input().split()))

now = 0
seen = []
while True:
    seen.append(now)
    teleporter = A[now]
    A[now] = 'used'
    if teleporter == 'used':
        break
    else:
        now = teleporter  

loop_s = seen.index(now)
size = len(seen) - loop_s - 1

if K <= loop_s:
    print(seen[K] + 1)
else:
    print(seen[loop_s +(K - loop_s) % size] + 1)
#D
N,K=map(int,input().split())
A=[0]+[int(x) for x in input().split()]

p=1
log=[[0,0] for i in range(N+1)]
log[1]=[1,0]
cnt=0
while True:
    cnt+=1
    p=A[p]
    log[p][0]+=1
    if log[p][0]==2:
        break
    else:
        log[p][1]+=cnt

re=cnt-log[p][1]
if K<cnt+log[p][1]:
    p=1
else:
    K=(K-log[p][1])%re

for i in range(K):
    p=A[p]
print(p)
n ,k =map(int,input().split())
a = list(map(int,input().split()))
p = {i for i in range(n)}
now = 1
ok = [0 for i in range(n)]
cou = 1
ok[0] = 1
while True:
    cou += 1
    nex = a[now-1]
    if ok[nex-1] == 0:
        ok[nex-1] = cou
    elif ok[nex-1] != 0:
        break
    now = nex
    if cou == k+1:
        print(nex)
        exit()

lost = k - (ok[nex-1]-1)
lokz = cou - ok[nex-1]

p = lost%lokz
ans = ok.index(ok[nex-1]+p)+1
print(ans)
n, k = [int(x) for x in input().split()]
a_list = [0] + [int(x) for x in input().split()]

ci = 1
temp_dict = {1: 0}
temp_list = [1]
for i in range(1, k + 1):
    temp = a_list[ci]
    if temp in temp_dict:
        ci = temp_list[(k - i) % (i - temp_dict[temp]) + temp_dict[temp]]
        break
    temp_dict[temp] = i
    temp_list.append(temp)
    ci = temp
print(ci)
import sys
from io import StringIO
import unittest

sys.setrecursionlimit(10**6)
def search(n, step, rec, seq, k):
	if rec[n] != -1:
		c = step - rec[n]
		sim = (k - rec[n]) % c
		v = n
		for i in range(sim):
			v = seq[v-1]
		print(v)
		return
	rec[n] = step
	if step == k:
		print(n)
		return
	search(seq[n-1], step + 1, rec, seq, k)
def resolve():
	n, k = map(int, input().split())
	seq = list(map(int, input().split()))
	rec = [-1]*(n+1)
	search(1, 0, rec, seq, k)

resolve()
N, K = map(int, input().split())
A = list(map(int, input().split()))
idx = 1
S = [idx]
ed = set([idx])

for i in range(1, K+1):
    idx = A[idx-1]
    if idx in ed:
        start = S.index(idx)
        end = len(S)
        n = start + (K - start) % (end - start)
        print(S[n])
        break
    else:
        S.append(idx)
        ed.add(idx)
        continue
else:
    print(idx)
def f():
    n,k=map(int,input().split())
    l=list(map(int,input().split()))
    now=1
    for d in range(k.bit_length()):
        k,f=divmod(k,2)
        if f:now=l[now-1]
        l=tuple(l[i-1]for i in l)
    print(now)
if __name__ == "__main__":
    f()
N,K=list(map(int,input().split()))
l=list(map(int,input().split()))
roop=[1]
s=set([1])
now=l[0]
while now not in s:
   roop.append(now)
   s.add(now)
   now=l[now-1]
start=roop.index(now)
if len(roop) <= K:
   K-=start
   K=K%(len(roop)-start)
   print(roop[start+K])
else:
   print(roop[K])
n,k,*a=map(int,open(0).read().split())
v=[0]+[-1]*n
p=0
for i in range(n):
  t=a[p]-1
  if v[t]>=0: l,r=v[t],i+1; break
  v[t]=i+1; p=t
print(v.index(min(k,l+(k-l)%(r-l)))+1)
import sys

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

i = 0
c = 0
while K > 0:
    if B[i] == 0:
        B[i] = c
        j = A[i] - 1
        i = j
        K -= 1
        c += 1
    else:
        x = c - B[i]
        K = K % x
        B = [0]*len(A)

print(j + 1)
n, k = map(int, input().split())
A = list(map(int, input().split()))
order = [-1] * (n+1)
v = [0] * (n+1)
v[0] = 1
order[v[0]] = 0
# iは順番
for i in range(1, n+1):
    v[i] = A[v[i-1] - 1]
    if order[v[i]] != -1:
        old_i = order[v[i]]
        c = i - old_i
        break
    order[v[i]] = i
if k < old_i:
    print(v[k])
else:
    print(v[(k - old_i)%c + old_i])
def f():
    n,k=map(int,input().split())
    l=list(map(int,input().split()))
    now=1

    for i in range(k.bit_length()):
        if k%2:now=l[now-1]
        l=[l[l[i]-1]for i in range(n)]
        k//=2
    print(now)
if __name__ == "__main__":
    f()
n, k = map(int, input().split())
*A, = map(int, input().split())
A = [0] + A
bit = list(bin(k)[2:][::-1])
m = len(bit)
DP = [[None for j in range(m)] for i in range(n + 1)]
for i in range(n + 1):
    DP[i][0] = A[i]
for j in range(1, m):
    for i in range(n + 1):
        DP[i][j] = DP[DP[i][j - 1]][j - 1]
i = 1
for j, b in enumerate(bit):
    if b == '1':
        i = DP[i][j]
print(i)
n,k=map(int,input().split())
l=list(map(int,input().split()))

s=1
q=[0]*n
q[0]=1
r=[1]
for i in range(k):
  s=l[s-1]
  if q[s-1]==0:
    q[s-1]+=1
    r.append(s)
    if i==k-1:
      print(s)
  else:
    x=r.index(s)
    mod=len(r)-x
    t=(k-i-1)%mod
    print(r[x+t])
    break
n, k = map(int, input().split())
A = list(map(int, input().split()))

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

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

if Exce > k:
    print(Town[k])
else:
    Warp = (k - Exce) % Cycl
    print(Town[Exce+Warp])
def f():
    n,k=map(int,input().split())
    l=list(map(int,input().split()))
    now=1
    for d in range(k.bit_length()):
        if k%2:now=l[now-1]
        l=tuple(l[i-1]for i in l)
        k//=2
    print(now)
if __name__ == "__main__":
    f()
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = [0 for _ in range(n)]
p = 1
i = 0
while True:
    p = a[p - 1]
    if b[p - 1] > 0 or p == 1:
        break
    else:
        i += 1
        b[p - 1] = i
bp = b[p - 1]
if k <= bp:
    print(b.index(k) + 1)
else:
    l = bp + (k - bp) % (i + 1 - bp)
    print(b.index(l) + 1)
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=2
t=0
path=[]
s=set()
cycle=[]
while(c<=k):
    if a[t] in s:
        #print(path.index(a[t]),a[t])
        cycle=path[path.index(a[t]):]
        k-=c-1
        break
    path.append(a[t])
    s.add(a[t])
    t=a[t]-1
    c+=1
if cycle==[]:
    print(a[t])
else:
    print(cycle[k%len(cycle)])
n, k = map(int,input().split())

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

gtyo = [0] *n
gtyo[0] = 1

gone = 0
t = 1

while t < n:
    gone = lis[gone] -1
    t += 1
    if gtyo[gone]:
        loop = t -gtyo[gone]
        t = n
    else:
        gtyo[gone] = t
        
if k -(gtyo[gone]-1)>= 0:
    res = (k- (gtyo[gone]-1)) % loop
else:
    res = k- (gtyo[gone]-1)
    
tmp = [i - gtyo[gone] for i in gtyo]

print(tmp.index(res)+1)
N, K = map(int, input().split())
*A, = map(int, input().split())
A = [a-1 for a in A]

# Douling(ダブリング)
i = 1
to = [[a for a in A]]
while pow(2, i) < K:
    t = [to[i-1][to[i-1][j]] for j in range(N)]
    to.append(t)
    i += 1

i -= 1
ans = [i for i in range(N)]
while i >= 0:
    if K & (1 << i) > 0:
        for j in range(N):
            ans[j] = to[i][ans[j]]
    i -= 1
print(ans[0]+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))
l = [0]
s = set()
s.add(0)
cur = a[0] - 1
while True:
    if cur in s:
        break
    else:
        s.add(cur)
        l.append(cur)
        cur = a[cur] - 1
i = l.index(cur)
if k <= i:
    print(l[k] + 1)
else:
    print(l[(k - i) % (len(l) - i) + i] + 1)
n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
visited = [-1 for _ in range(n)]
visited[0] = 0
l = [0]
for i in range(k):
    nex = a[l[i]]
    if visited[nex] != -1:
        res = visited[nex]
        cyc = i + 1 - visited[nex]
        break
    else:
        l += [nex]
        visited[nex] = i + 1
else:
    res = k + 1
print(l[res + (k - res) % cyc] + 1 if k > res else l[k] + 1)
N, K = map(int, input().split())
A = tuple( map(int, input().split()) )

L = [0 for i in range(N+1)]
L[1] = 1
x = 1
t = 1
while K>0:
  t += 1
  K -= 1
  x = A[x-1]
  if L[x] != 0:
    p = t - L[x]
    K %= p
    break
  else:
    L[x] = t
    
while K>0:
  x = A[x-1]
  K -= 1
print(x)
n, k = [int(i) for i in input().split()]
l = [int(i) - 1 for i in input().split()]

i = 0
m = [-1] * n
p = 0

while m[i] == -1:
    if p == k:
        break
    m[i] = p
    p += 1
    i = l[i]
else:
    z = (k - m[i]) % (p - m[i])
    i = m.index(m[i] + z)
print(i + 1)
N, K = map(int, input().split())
A = list(map(lambda x: int(x) - 1, input().split()))

stack = [0]
visited = {0}

now = 0
while A[now] not in visited:
    now = A[now]
    stack.append(now)
    visited.add(now)

roop_start = A[now]

roop = []
while stack[-1] != roop_start:
    roop.append(stack.pop())
roop.append(roop_start)
roop = roop[::-1]
roop_size = len(roop)

now = 0
while now != roop_start and K:
    now = A[now]
    K -= 1

if K == 0:
    print(now + 1)
else:
    print(roop[K % roop_size] + 1)
n,k = map(int, input().split())
a = list(map(int, input().split()))

G = {}
for i in range(1, n + 1):
    G.update({i: a[i - 1]})

seen = [False] * (n + 1)
path = [1]

i = 1
while True:
    seen[i] = True
    i = G[i]
    
    if seen[i]:
        break
    else:
        path.append(i)

m = path.index(i)

if m > k:
    print(path[k])
else:
    k -= m
    path = path[m:]
    k %= len(path)
    print(path[k])
N,K = map(int,input().split())
*A, = map(int,input().split())

M = len(bin(K))-2

B = [[0]*N for _ in [0]*M]
B[0] = [a-1 for a in A]
for i in range(1,M):
    for j in range(N):
        B[i][j] = B[i-1][B[i-1][j]]

k = K
a = 0
while k>0:
    if k%2==1:
        a = B[0][a]
        k -= 1
        continue
    j = 1
    while k%(2**(j+1))==0:
        j += 1
    a = B[j][a]
    k -= 2**j
print(a+1)
N, K = map(int,input().split())
A = [0] + list(map(int,input().split()))
T = {1:1}
current_town = 1
for i in range(2,K+1):
    current_town = A[current_town]
    K -= 1
    if current_town in T:
        loop_towns = list(T.keys())[T[current_town]-1:]
        print(loop_towns[K%len(loop_towns)])
        break
    else:
        T[current_town] = i
else:
    print(A[current_town])
import sys
sys.setrecursionlimit(10**7)

N, K = map(int,input().split())
A = list(map(int, input().split())) 
check = [0]*N
tele_lis = []
start = []
def tele(x):
    check[x-1] = 1
    tele_lis.append(x)
    if check[A[x-1]-1] == 0:
        tele(A[x-1])
    else:
        start.append(A[x-1])
        return
tele(1)
for i in range(N):
    if start[0] == tele_lis[i]:
        break
if i >= K:
    print(tele_lis[K])
else:
    print(tele_lis[i::][(K-i) % (sum(check)-i)])
n, k = map(int, input().split())
A=list(map(int, input().split()))

c=1
path=[]
d={}
l=0
while l<=k:
    if c in d:
        p=d[c]
        #print(p, path)
        pattern_l = l-p
        k-=((k-p)//pattern_l)*pattern_l
    path.append(c)
    l+=1
    d[c]=l-1
    c=A[c-1]
print(path[k])
N, K = map(int, input().split())
A = list(map(int, input().split()))

t_zero = [-1] * N

t = 0
a = 1

while t_zero[a-1] == -1:
  if t == K:
    break
  t_zero[a-1] = t
  t += 1
  a = A[a-1]
else:
  k = (K - t_zero[a-1]) % (t - t_zero[a-1])
  for _ in range(k):
    a = A[a-1]
print(a)
n, k, *a = map(int, open(0).read().split())
a = [None] + a
done = [None] + [0] * n
t = 0
p = 1
while not done[p]:
    done[p] = t
    p = a[p]
    t += 1

loop = t - done[p]
orig = done[p]

if k - orig > 0:
    k = (k - orig) % loop + orig
p = 1
while k:
    p = a[p]
    k -= 1

print(p)
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

if K<len(log):
  print(log[K]+1)
else:
  print(log[(K-loop_start)%loop+loop_start]+1)
def resolve():
    N, K = map(int, input().split())
    A = tuple(map(int, input().split()))
    path = [1]
    for i in range(2*N):
        cur = A[path[-1]-1]
        path.append(cur)

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

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

    print(path[K])

if __name__ == '__main__':
    resolve()
n, k = map(int, input().split())
a = list(map(int, input().split()))
visited = [0]
for _ in range(n):
    now = a[visited[-1]]-1
    visited.append(now)

if k < n:
    print(visited[k]+1)
else:
    p = visited.index(visited[-1])
    loop = visited[::-1][1:].index(visited[-1])+1
    x = (k-p-1)%loop
    print(visited[p+x+1]+1)
import sys
from collections import Counter
from collections import deque
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n,k=mp()
a=lmp()
l=[]
i=0
ch=[0]*(n+1)
while True:
    if ch[i]==1:
        break
    else:
        ch[i]=1
        l.append(a[i])
        i=a[i]-1


x=l.index(a[i])
y=len(l)-x

if k<=x:
    print(l[k-1])
else:
    print(l[(k-x-1)%y+x])
n, k = [int(i) for i in input().split()]
tel = [int(i) for i in input().split()]
been = [0 for _ in range(n)]
been[0] = 1

next = tel[0]
loop = [1]
for i in range(n):
	loop.append(next)
	if been[next-1] == 1:
		break
	been[next-1] = 1
	next = tel[next-1]

loop_begin = loop.index(tel[next-1])
loop_num = i - loop_begin + 2
if loop_begin >= k:
	ans = loop[k]
else:
	loop_remain = (k-loop_begin) % loop_num
	ans = loop[loop_begin+loop_remain]

print(ans)
N, K = map(int, input().split())
A = list(map(int, input().split()))
r = [None] * N
i = 0
cur = 0
m = None
while True:
    if m is None and r[cur] is not None:
        m = i - r[cur]
        K = i + (K-i)%m
    else:
        r[cur] = i
    if i == K:
        print(cur+1)
        exit()
    i += 1
    cur = A[cur] - 1
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
B = []
Vector = [0]*N
j = 0
 
for i in range (0,K):
	if Vector[A[j]-1] == 1:
		Value = B.index(A[j])
		C = B[B.index(A[j])::]
		break
	else:
		B.append(A[j])
		Vector[A[j]-1] = 1
		j = A[j]-1

if A[0] ==1:
	print(1)
elif len(B) == K:
	print(B[-1])
else:
	print(C[(K-Value-1)%len(C)])
n,k = [int(i) for i in input().split()]
a = [0] + [int(i) for i in input().split()]

pos = 1
move= 0
colors = [-1]*(n+1)

while(colors[pos]==-1):
    colors[pos] = move
    move += 1
    pos = a[pos]
    if move==k:
        print(pos)
        break
else:
    turn = move - colors[pos]
    rest = (k-colors[pos]) % turn
    while rest:
        pos = a[pos]
        rest -= 1
    print(pos)