peterverebics/CodeNet_Python_118 huggingface dataset:
N,K=map(int,input().split())
A=list(map(int,input().split()))
A=[A[i]-1 for i in range(N)]

doubling=[[0 for i in range(N)] for i in range(0,70)]
for i in range(N):
    doubling[0][i]=A[i]

for i in range(1,70):
    for j in range(N):
        doubling[i][j]=doubling[i-1][doubling[i-1][j]]

ans=0
for i in range(0,70):
    if K>>i &1==1:
        ans=doubling[i][ans]

print(ans+1)
N,K=map(int,input().split())
A=[int(i)-1 for i in input().split()]
def times(X,Y):
    return [X[Y[i]] for i in range(N)]
def exponen(X,n):
    if n==0:
        return [i for i in range(N)]
    if n==1:
        return X
    if n%2==0:
        Y=exponen(X,n//2)
        return times(Y,Y)
    if n%2==1:
        Y=exponen(X,n//2)
        return times(times(Y,Y),X)
B=exponen(A,K)
print(B[0]+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))

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

l = [-1]*(n + 1)
l[0] = 0

order = 0
now = 0

while k > 0:
    k -= 1
    now = a[now]
    order += 1
    if l[now] == -1:
        l[now] = order
    else:
        loop = order - l[now]
        k %= loop
        break

while k > 0:
    k -= 1
    now = a[now]

print(now + 1)
n,k = map(int,input().split())
a = list(map(int,input().split()))
already = ["1"]
set = {"1"}
now = 1

for i in range(n):
    now = str(a[int(now)-1])
    if now in set:
        break
    else:
        already.append(now)
        set.add(now)


ind = already.index(now)
k -= ind
cycle = already[ind:]

if k <= 0:
    print(already[k+ind])
else:
    now = (k+1) % len(cycle)
    print(cycle[now-1])
n, k = map(int, input().split())
a = [[int(x)-1 for x in input().split()]]

for i in range(1, k.bit_length()):
    ith = [a[-1][a[-1][i]] for i in range(n)]
    a.append(ith)

ans = 0

for i in range(k.bit_length()):
    if (k >> i) & 1:
        ans = a[i][ans]

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


lst = []
visit_now = 1
visit_list = [-1] * (n + 1)
cnt = 0

while visit_list[visit_now - 1] == -1:
    visit_list[visit_now - 1] = cnt
    lst.append(visit_now)
    visit_now = a[visit_now - 1]
    cnt += 1

start = visit_list[visit_now - 1]
loop_num = cnt - start

if k <= start:
    ans = lst[k]
else:
    k -= start
    k %= loop_num
    ans = lst[start + k]

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

    num = 1
    l = []
    s = [1]*n

    while s[num-1]:
        l.append(num)
        s[num-1] = 0
        num = a[num-1]

    if l.index(num) <= k:
        l2 = l[l.index(num):]
        kk = (k-len(set(l)-set(l2)))%len(l2)
        print(l2[kk])
    else:
        print(l[k])

main()
# -*- coding: utf-8 -*-
n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))

passed = [-1 for _ in range(n)]

cnt = 0
now = 0
_next = a[now]
remain = 0
for i in range(k):
    _next = a[now]
    if passed[_next] != -1:
        pre = passed[_next]
        remain = (k-pre) % (cnt + 1 - pre)
        now = _next
        break
    passed[now] = cnt
    now = _next
    cnt += 1

for _ in range(remain):
    now = a[now]

print(now + 1)
# D - Teleporter
N,K = map(int,input().split())
A = [0]+list(map(int,input().split()))
s = {1:0}
l = [1]
i = 1
for j in range(1,10**10):
    i = A[i]
    if i in s:
        sigma = j-s[i]
        break
    s[i] = j
    l.append(i)
if K<s[i]:
    print(l[K])
else:
    K -= s[i]
    mod = l[s[i]:]
    print(mod[K%sigma])
n, k = map(int, input().split())

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

for i in range(n):
	p[i] -= 1

current_pos = 0
steps = 0
first_occurence = {}

result = -1

while steps < k:
	if current_pos in first_occurence:
		first = first_occurence[current_pos]
		cycle_length = steps - first
		left = (k - steps) % cycle_length
		x = 0
		while x < left:
			current_pos = p[current_pos]
			x += 1

		result = current_pos
		break
	else:
		first_occurence[current_pos] = steps
		steps += 1
		current_pos = p[current_pos]

if result == -1:
	result = current_pos

print(current_pos + 1)
n,k,*a=map(int,open(0).read().split())
a=[0]+a
ans=1
while k:
  if k&1:
    ans=a[ans]
  a=[a[i] for i in a]
  k>>=1
print(ans)
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:
    x=(k-begin+1)%(end-begin)
    if x==0:
        x=end-begin
    k=x+begin-1
loc=1
for _ in range(k):
    loc=a[loc-1]
print(loc)
n,k=map(int,input().split())
a=[0]+[int(j) for j in input().split()]
l=[-1]*(n+1)
ll=[]
i=1
idx=0
while True:
    if l[i]==-1:
        l[i]=idx
    else:
        ll.append(i)
        break
    ll.append(i)
    i=a[i]
    idx+=1

p=ll[-1]
nl=ll[l[p]:-1]
ml=ll[:l[p]]
n,m=len(nl),len(ml)
if k<m:
    print(ml[k])
elif nl==[]:
    print(p)
    exit()
else:
    print(nl[(k-m)%n])
N, K = map(int, input().split())
A = list(map(int, input().split()))
visited = [0]*N #各場所に最初に到着するまでの移動回数
cnt = 0 #現在の移動回数
pos = 0 #現在の場所
flag = False
while K > 0:
    cnt += 1
    K -= 1
    if visited[pos] == 0:
        visited[pos] = cnt
    elif not flag:
        K %= cnt-visited[pos]
        flag = True
    pos = A[pos]-1
print(pos+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))
doubling_list = [[-1] * n for i in range(60)]

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

for i in range(1, 60):
    for j in range(n):
        doubling_list[i][j] = doubling_list[i - 1][doubling_list[i - 1][j]]

now = 0
for i in range(60)[::-1]:
    if k >> i & 1:
        now = doubling_list[i][now]

ans = now + 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
looped = 0
for i in range(2,k+2):
  nex = a[now]
  if num[nex]:
    start = num[nex]
    cycle = i - num[nex]
    v = (k+1-i) % cycle + start
    ans = num.index(v) + 1
    looped = 1
    break
  else:
    num[nex] = i
    now = nex
if looped:
  print(ans)
else:
  print(now+1)
n, k = [int(x) for x in input().split()]
a_list = [int(x) - 1 for x in input().split()]
ci = 0
while k:
    if k & 1:
        ci = a_list[ci]
    k >>= 1
    a_list = [a_list[a_list[i]] for i in range(n)]
print(ci + 1)
import sys
N, K = map(int, input().split())
As = [0] + list(map(int, input().split()))
# print(As)
d = [-1]*(N+1)
i = 1
step = 1
while step <= K:
	# print("step: {}, idx: {}".format(step,i))
	next_i = As[i]
	if d[next_i] != -1:
		freq = step - d[next_i]
		step = step + ((K - step) // freq)*freq
	d[next_i] = step
	step += 1
	i = next_i
 
print(i)
n, k = map(int,input().split())
A = list(map(int,input().split()))
A = [a-1 for a in A]

path = []
passed = set([])
p = 0
while 1:
  if k ==0:
    print(p+1)
    exit()
  if p not in passed:
    path.append(p)
    passed.add(p)
    p = A[p]
    k -= 1
  else:
    break
path = path[path.index(p):]
term = len(path)
k %= term
print(path[k]+1)
N,K = map(int,input().split()) #N = 4,K=5
A = [int(i)-1 for i in input().split()]
log_K = K.bit_length() #3bit (5 = 101)
next = [[-1]*N for _ in range(log_K)]

for i in range(N):
    next[0][i] = A[i]
    
for k in range(log_K - 1):
    for i in range(N):
        next[k+1][i] = next[k][next[k][i]]  

now = 0
for i in range(log_K)[::-1]: 
    if ((K >> i) & 1) == 1: 
        now = next[i][now]
print(now + 1)
n,k=map(int,input().split())
A=list(map(int,input().split()))
V=[0]*n
V[0]=0
a=0
c=0
for i in range(k):
  a=A[a]-1
  if V[a]!=0:
    break
  V[a]=i+1
if i==k-1:
  print(a+1)
  exit()
else:
  k=(k-V[a])%(i+1-V[a])
  for i in range(k):
    a=A[a]-1
  if k==0:
    print(a+1)
  else:
    print(a+1)
N, K = map(int, input().split())
A = list(map(int, input().split()))

dic = {}
list_town = []
set_town = set()
town = 1

for i, v in enumerate(A):
    dic[i+1] = v

for _ in range(K):
    list_town.append(town)
    set_town.add(town)
    town = dic[town]
    if town in set_town:
        stop_town = town
        break

if N <= K:
    list_first_split = list_town[:list_town.index(stop_town)]
    list_second_split = list_town[list_town.index(stop_town):]
    print(list_second_split[(K - (len(list_first_split))) % len(list_second_split)])
else:
    print(town)
N,K=map(int,input().split())
A=list(map(int,input().split()))
done=[-1 for _ in range(N)]
done[0]=0
n=0
now=0
init=0
loop=N
log=[0]

while n<N:
    log.append(A[log[-1]]-1)
    n+=1
    if done[log[-1]]>-1:
        init=done[log[-1]]
        loop=n-done[log[-1]]
        break
    done[log[-1]]=n

if K<init:
    print(log[K]+1)
else:
    num=init+(K-init)%loop
    print(log[num]+1)
def main():
  n,k=map(int,input().split())
  a=list(map(int,input().split()))
  b=1
  seen=[-1]*n
  c=0
  while seen[b-1]==-1:
    seen[b-1]=c
    b=a[b-1]
    c+=1
  start=seen[b-1]
  end=c
  k=min(k,start+(k-start)%(end-start))
  b=1
  for _ in range(k):
    b=a[b-1]
  print(b)
if __name__=='__main__':
  main()
n, k = map(int,input().split())
a = list(map(int,input().split()))

s = [True] * n  
c = [1]  
now = 0  
while s[now]:
    s[now] = False  
    now = a[now] - 1
    c.append(now+1)

start_cycle = c.index(c[-1]) 
loop = c[start_cycle:-1]  
cycle = len(loop)

if k < start_cycle:  
    print(c[k])
else:
    k -= start_cycle
    k %= cycle
    print(loop[k])
n, k = map(int, input().split())
a = list(map(int, input().split()))
for i in range(n):
	a[i] -= 1

city_position = {}
step = 0
city = 0

while city not in city_position:
	city_position[city] = step
	city = a[city]
	step += 1

steps_before_cycle = city_position[city]
cycle_len = step - steps_before_cycle

# k %= cycle_len

city = 0
for i in range(min(k, steps_before_cycle)):
	city = a[city]

k -= steps_before_cycle
if k > 0:
	k %= cycle_len

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


print(city+1)
N,K=map(int,input().split(' '))
A=list(map(lambda x:int(x)-1,input().split(' ')))
roop=[0]
flg = [0 for i in range(N)]
flg[0]=1
n = A[0]
for i in range(2,N+1):
    if flg[n]==0:
        roop.append(n)
        flg[n] = i
        n = A[n]
    else:
        end=i
        break
if K <len(roop):
    print(roop[K]+1)
else:
    K-=flg[n]-1
    K%=(end-flg[n])
    roop=roop[flg[n]-1:end]
    print(roop[K]+1)
#abc167d
n,k=map(int,input().split())
a=list(map(int,input().split()))
x=1
l=[1]
f=[1]*n
f[0]=0
for i in range(k):
 x=a[x-1]
 if f[x-1]:
  l.append(x)
  f[x-1]=0
 else:
  break
y=l.index(x)
res=(k-y)%(len(l)-y)+y
print(l[res])
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    da = [[None]*n for _ in range(64)]
    for i in range(n):
        da[0][i] = a[i]-1
    for i in range(1, 64):
        for j in range(n):
            da[i][j] = da[i-1][da[i-1][j]]
    ans = 0
    for i in range(64):
        p = k&1
        if p == 1:
            ans = da[i][ans]
        k//= 2
    print(ans+1)

if __name__ == "__main__":
    main()
def solve():
    N, K = map(int, input().split())
    A = [0]+list(map(int, input().split()))
    visited = [-1]*(N+1)
    visited[1] = 0
    now = 1
    for i in range(1,K+1):
        now = A[now]
        if visited[now]>=0:
            break
        visited[now] = i
    if i==K:
        ans = now
    else:
        num = (K-visited[now])%(i-visited[now])+visited[now]
        ans = visited.index(num)
    return ans
print(solve())
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)
if k<=ind:
  print(B[k]+1)
else:
  print(B[(k-ind)%(d-ind)+ind]+1)
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 set_MAP:
        break


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

if p < 0:
    print(list_MAP[k])
else:
    list_MAP = list_MAP[list_MAP.index(x):]
    print(list_MAP[p % q])
(N, K), A = [[*map(int, line.split())] for line in open(0)]

kiseki = [0] * N
now = 0
for i in range(N):
    kiseki[i] = now
    now = A[now] - 1

p = kiseki.index(now)
ans = p + (K - p) % (N - p) if p < K else K
print(kiseki[ans] + 1)
n,k=map(int,input().split())
l=list(map(int,input().split()))
lcount=[0]*(n+1)
lnum=[0]*(n+1)
count=1
place=1
while lcount[place]==0:    
    lnum[place]=count
    lcount[place]=1
    place=l[place-1]
    count+=1 

dif=count-lnum[place]
if k+1<lnum[place]:
    print(lnum.index(k+1))
else:
    k=(k+1-lnum[place])%dif+lnum[place]
    print(lnum.index(k))
n, k = map(int, input().split())
a = list(map(int, input().split()))

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

s = []
v = 1
while order[v] == -1:
	order[v] = len(s)
	s.append(v)
	v = a[v - 1]
c = len(s) - order[v]
l = order[v]

if k < l:
	print(s[k])
else:
	k -= l
	k %= c
	print(s[l + k])
n, k = map(int,input().split())
a = list(map(int,input().split()))

s = 1
seen = [False for _ in range(n+1)]
d = []
while not seen[s]:
    seen[s] = True
    d.append(s)
    s = a[s-1]

noncyi = d.index(s)
cyi = len(d) - noncyi
if k < noncyi:
    print(d[k])
else:
    k -= noncyi
    print(d[noncyi + k%cyi])
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
b = [-1] * (n+1)
now = 1
history = []

while b[now] == -1:
  b[now] = len(history)
  history.append(now)
  now = a[now-1]

c = len(history) - b[now]
l = b[now]

if k < l:
  print(history[k])
else:
  k -= l
  k %= c
  print(history[l+k])
N, K = map(int, input().split())
A = [int(s) for s in input().split()]

used = [False] * (N + 1)
dist = []
dist.append(1)
for i in range(N):
    v = dist[-1]
    if used[v]:
        break
    used[v] = True
    dist.append(A[v - 1])

g = dist[-1]
s = dist.index(g)

if K <= s:
    print(dist[K])
else:
    k = K - s
    index = k % (len(dist) - s - 1)
    print(dist[s + index])
N, K = map(int, input().split())
A = list(i - 1 for i in map(int, input().split()))
now = 0
loop_start = 0
load_list = [-1] * N
Y = - 1
while loop_start < K:
    if load_list[now] == -1:
        load_list[now] = loop_start
        now = A[now]
        loop_start += 1
    else:
        start = load_list[now]
        end = loop_start - load_list[now]
        Y = start + (K - loop_start) % end
        break

if Y == -1:
    print(now + 1)
else:
    print(load_list.index(Y) + 1)
n,k=map(int,input().split())
a=list(map(int,input().split()))

visited=[1]
for i in range(n):
    visited.append(a[visited[-1]-1])

if k < n:
    print(visited[k])
else:
    first = visited.index(visited[-1])
    loop = visited[::-1][1:].index(visited[-1])+1
    last = (k-first)%loop
    print(visited[first+last])
n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]
if k <= n:
  p = 0
  for i in range(k):
    p = a[p]
  print(p + 1)
  exit()
p = 0
t = [-1]*n
t[0] = 0
for i in range(1, n):
  p = a[p]
  if t[p] != -1:
    break
  t[p] = i
d = i - t[p]
k -= i
k %= d
for i in range(k):
  p = a[p]
print(p + 1)
259
N,K=map(int,input().split())
A=[0]+list(map(int,input().split()))
m=1
towns=[1]
flg=[0]*(N+1)
flg[1]=1
while 1:
    m=A[m]
    towns.append(m)
    if flg[m]==1:
        l=len(towns)-towns.index(m)-1
        break
    flg[m]=1
pl=towns.index(m)
if K<=pl:
    print(towns[K])
else:
    print(towns[(K-pl)%l+pl])
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

D = [0] * N
L = [0]

D[0] = 1
t = 0
c = 2
while True:
  t = A[t] - 1
  L.append(t)
  if D[t] == 0:
    D[t] = c
    c += 1
  else:
    l = D[t] - 1
    n = c - D[t]
    break
  
if l - 1 >= K:
  g = K + 1
else:
  K -= l - 1
  K %= n
  if K != 0:
    g = l + K
  else:
    g = l + n

for i in range(N):
  if D[i] == g:
    print(i + 1)
    exit()
n,k = map(int,input().split())
ls = list(map(int,input().split()))
fi = [-1]*(n+1)
fi[1] = 0
a = 0
aa = 0
for i in range(k):
    if fi[ls[a]] == -1:
        fi[ls[a]] = i + 1
        aa = a
        a = ls[a] - 1
    else:
        ind = fi[ls[a]]
        t = (k - ind) % (max(fi) + 1 - ind)
        t += ind
        print(fi.index(t))
        break
else:
    print(ls[aa])
n,k=map(int,input().split())
a=list(map(int,input().split()))
adj=[1]*n
b=[1]
now=0
while adj[now]:
  adj[now]=0
  now=a[now]-1
  b.append(now+1)
loop_start=b.index(b[-1])
loop=b[loop_start:-1]
p=len(loop)
if k<loop_start:
  print(b[k])
else:
  k-=loop_start
  k%=p
  print(loop[k])
n, k = map(int, input().split())
A = list(map(int, input().split()))
tmp = 1
route = [tmp]
edge = {tmp}

for i in range(1, k + 1):
    tmp = A[tmp - 1]
    if tmp in edge:
        left = route.index(tmp)
        right = len(route)
        idx = left + (k - left) % (right - left)
        print(route[idx])
        break
    else:
        route.append(tmp)
        edge.add(tmp)
        continue
else:
    print(tmp)
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = []
x = [0] * n
x.append(0)
v = 1
c = 1
l = 0
while x[v] == 0:
    x[v] = len(s)
    s.append(v)
    v = a[v-1]
c = len(s) - x[v]
l = x[v]


if k < l:
    print(s[k])
else:
    k -= l
    k = k % c
    print(s[l+k])
n,k = map(int,input().split())
li = [0]+list(map(int,input().split()))

memo = [-1]*(n+1)
start = 1
syuuki = 0
haji = 0

for i in range(k):
    if memo[start] != -1:
        syuuki = i - memo[start]
        haji = memo[start]
        break
    memo[start] = i
    start = li[start]
else:
    print(start)
    exit()

#print(syuuki,haji)
k-=haji
k%=syuuki
k+=haji

start = 1
for kk in range(k):
    start = li[start]
print(start)
n,k = map(int,input().split())
a = list(map(int,input().split()))
cur = 0
reach = [False]*n
x = []
for i in range(k+1):
    x.append(cur)
    if reach[cur]:
        break
    reach[cur]=True
    cur = a[cur]-1
cnt = 0
for i in range(len(x)):
    if x[i]==x[-1]:
        break
    cnt += 1


y = x[cnt:-1]
if k>cnt:
    ind = (k-cnt)%len(y)
    print(y[ind]+1)
else:
    print(x[k]+1)
n,k = map(int,input().split())
a = list(map(int,input().split()))
now = 0
loop = []
flag = [0 for _ in range(n)]
for _ in range(n):
    loop.append(now+1)
    if flag[now]:
        break
    else:
        flag[now] = 1
        now = a[now]-1
tloop = loop[loop.index(loop[-1]):len(loop)-1]
floop = loop[:loop.index(loop[-1])]
if k < len(floop):
    print(floop[k])
else:
    k -= len(floop)
    print(tloop[k%len(tloop)])
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
    a = A[a]
    if K == 0: 
        print(a)
        sys.exit()

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

a = [[0]+list(map(int, input().split()))]
for k in range(62):
    a.append([a[k][a[k][i]] for i in range(n+1)])

res = 1
for k in list(range(62))[::-1]:
    if m >= 2**k:
        res = a[k][res]
        m -= 2**k
print(res)
#!/usr/bin/env python
import sys
sys.setrecursionlimit(10**9)
from collections import deque
import heapq
from itertools import combinations
import bisect

n,k=map(int,input().split())
a=list(map(int,input().split()))
for i in range(n):
    a[i]-=1
v=[-1]*n
s=0
c=0
while True:
    if v[s]!=-1:
        break
    if c==k:
        print(s+1)
        exit()
    v[s]=c
    c+=1
    s=a[s]
p=c-v[s]
k-=c
k%=p
for i in range(k):
    s=a[s]
print(s+1)
N, K = map(int, input().split())

A = list(map(int, input().split()))
d = {}

d[1] = A[0]

idx = 1
start = 0
count = 0
while True:
  count += 1
  if K == count:
    print(A[idx-1])
    exit()
  d[idx] = A[idx-1]
  idx = A[idx-1]
  if idx in d:
    start = idx
    break


e = {}
l = []
while True:
  e[start] = A[start-1]
  l.append(e[start])
  start = A[start-1]
  if start in e:
    break


amari = ((K-count-len(l)) % len(l))
if amari != 0:
  print(l[amari-1])
else:
  print(l[-1])
n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
ts = [[] for _ in range(n)]

p = 0
t = 0
while True:
    ts[p].append(t)
    if t == k:
        print(p + 1)
        exit()
    if len(ts[p]) == 2:
        if (k - ts[p][0]) % (ts[p][1] - ts[p][0]) == 0:
            print(p + 1)
            exit()
    p = a[p]
    t += 1
N,K=list(map(int,input().split()))
A=[0]+list(map(int,input().split()))

visit=set()
order=[-1]*(N+1)
p=1
cnt=0

while not (p in visit):
    visit.add(p)
    order[cnt]=p
    p=A[p]
    cnt+=1

start=order.index(p)
end=cnt
if K>end:
    K-=(end-start)*max(0,(K-start)//(end-start)-1)
p=1
while K>0:
    p=A[p]     
    K-=1

print(p)
import sys


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


N, K = map(int, input().split())
k_bitlen = K.bit_length()
dp = [[0] * N for _ in range(k_bitlen + 1)]
dp[0] = list(map(lambda x: int(x) - 1, input().split()))

for k in range(k_bitlen):
    for i in range(N):
        dp[k + 1][i] = dp[k][dp[k][i]]
pos = 0
for k in range(k_bitlen):
    if K >> k & 1:
        pos = dp[k][pos]
print(pos + 1)
n,k = map(int,input().split())
a = list(map(lambda x: int(x)-1,input().split()))
towns = [0]
added = [-1] * n
added[0] = 0
for i in range(1,k+1):
    tmp = a[towns[-1]]
    pl = added[tmp]
    if pl != -1:
        loop = towns[pl:]
        last = (k-pl) % len(loop)
        print(loop[last]+1)
        exit()
    else:
        added[tmp] = i
        towns.append(tmp)
print(towns[-1]+1)
N, K = map(int, input().split())
A = map(int, input().split())
A = [x-1 for x in A] # 0-indexedに変換

here = 0
visited = set()
route = []

while here not in visited:
    visited.add(here)
    route.append(here)
    here = A[here]

loopStart = route.index(here)

if K <= loopStart:
    ans = route[K]
else:
    lenCycle = len(route) - loopStart
    ans = route[loopStart + (K - loopStart) % lenCycle]

print(ans + 1)
from collections import Counter
n, k = map(int, input().split())
A = list(map(int, input().split()))
C = Counter([])
X, Y = [], []
m = 0
for _ in range(n):
    a = A[m]-1
    C[m] += 1
    X.append(m)
    if C[a] != 0:
        Y = X[X.index(a):]
        X = X[:X.index(a)]
        break
    m = a
# print(X,Y)
x = len(X)
y = len(Y)
if k <= x-1:
    print(X[k]+1)
else:
    k -= x
    print(Y[k-(k//y)*y]+1)
import sys
sys.setrecursionlimit(10**6)
n, k = map(int, input().split())
a = list(map(int, input().split()))

length = [-1] * n
path = [-1, []]
route = []


def func(now, depth):
    global path, length, a, route
    route.append(now)
    if length[now] == -1:
        length[now] = depth
        func(a[now]-1, depth+1)
    else:
        path = [length[now], route[length[now]:depth]]


func(0, 0)

if (k < path[0]):
    print(route[k] + 1)
else:
    tmp = k - path[0]
    tmp %= len(path[1])
    print(path[1][tmp] + 1)
import sys
n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))

x=[-1]*n
x[0]=0
now=0

if k<=n:
    for _ in range(k):
        now=a[now]
    print(now+1)
    sys.exit()

for i in range(1,n+1):
    now=a[now]
    if x[now]==-1:
        x[now]=i
    else:
        k=(k-x[now])%(i-x[now])
        break
for i in range(k):
    now=a[now]
print(now+1)
n,k=map(int,input().split())
a=list(map(int,input().split()))
visited=set()
new=0
c=0
while not new in visited and c<k:
  visited.add(new)
  new=a[new]-1
  c+=1
if c==k:
  print(new+1)
else:
  start=new
  d=0
  visited=set()
  while not new in visited and d<k-c:
    visited.add(new)
    new=a[new]-1
    d+=1
  if k-c==d:
    print(new+1)
  else:
    new=start
    for i in range((k-c)%d):
      new=a[new]-1
    print(new+1)
N, K = map(int, input().split())
A = list(map(lambda s: int(s) - 1, input().split()))

V = [-1] * N
V[0] = 0
H = [0]
for i in range(1, K + 1):
    n = A[H[i - 1]]
    H.append(n)
    if V[n] != -1:
        start_L = V[n]
        L = H[start_L:i]
        break
    V[n] = i

if len(H) == K + 1:
    ans = H[K]
else:
    t = len(L)
    ans = L[(K - start_L) % t]
print(ans + 1)