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

cur_town = 1
visited = {1: 1}
before_loop = [1]
cnt = 2
while True:
    cur_town = A[cur_town - 1]
    if cur_town in visited:
        break
    visited[cur_town] = cnt
    before_loop.append(cur_town)
    cnt += 1

# loop に入る前の回数
before_loop_cnt = visited[cur_town] - 1
if K < before_loop_cnt:
    print(before_loop[K])
else:
    loop_lst = before_loop[visited[cur_town] - 1 :]
    print(loop_lst[(K - before_loop_cnt) % len(loop_lst)])
(N, K) = [int(n) for n in input().split()]
A = [int(n)-1 for n in input().split()]
v = [-1 for _ in range(N)]

D = [0]
v[0] = 0

cur = A[0]
i = 1

while v[cur] == -1:
    D.append(cur)
    v[cur] = i
    if i == K:
        print(cur+1)
        exit()

    i += 1
    cur = A[cur]

K -= v[cur]
K %= (i - v[cur])

print(D[v[cur] + K]+1)
n, k = list(map(int, input().split()))
trans = list(map(int, input().split()))
trans = [-1] + trans
dest = 1
seen = [False] * (n+1)
start = 0
output = 0
t = []
for _ in range(k):
  dest = trans[dest]
  if seen[dest]:
    for i, x in enumerate(t):
      if x == dest:
        start = i
    break
  seen[dest] = True
  t.append(dest)
if len(t) < k:
  s = t[start:]
  output = s[(k - len(t[:start]) - 1) % len(s)]
else:
  output = t[k-1]
print(output)
N,K = map(int,input().split())
A = list(map(lambda x: int(x) -1,input().split()))
gone = [-1]*N
tele = [0]*N
to = 0
cnt = 0
if  N < K:
    while(1):
        tele[cnt] = to
        gone[to] = cnt
        cnt += 1
        to = A[to]
        if gone[to] > 0:
            break

    print(tele[(K-(gone[to]))%(cnt-gone[to]) + gone[to]] + 1) 
else:
    for i in range(K):
        to = A[to]
    print(to + 1)
# coding: utf-8
N,K=map(int,input().split())
A=list(map(int,input().split()))
used=[False for i in range(N)]
used[0]=True
p=1
L=[1]

for i in range(N+1):
    p=A[p-1]
    L.append(p)
    if used[p-1]:
        looper=p
        break
    used[p-1]=True

for i in range(len(L)):
    if L[-(2+i)]==looper:
        a=i+1
        b=len(L)-1-a
        break
if K>b:
    ans=L[b+(K-b)%a]
else:
    ans=L[K]

print(ans)
n,k=map(int,input().split())
A = [int(v)-1 for v in input().split()]

memo = {}
p = 0
for i in range(k):
  if p in memo:
    itvl = i - memo[p]
    rest = (k-i-1)%itvl
    for j in range(rest+1):
      p=A[p]
    print(p+1)
    exit()
  else:
    memo[p]=i
  p=A[p]
print(p+1)
n, k = map(int, input().split())

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

b = [0]*n
pos = 0

j = 0

while True:
	b[pos] += 1
	if b[pos]>1:
		rpos = pos
		pos = a[rpos]-1
		i = 1
		while True:
			if pos == rpos:
				break
			pos = a[pos]-1
			i += 1
		break
	
	pos = a[pos]-1
	j += 1

j = j-i

if k>j:
	kp = (k-j)%i
	pos = rpos
else:
	kp = k
	pos = 0

for _ in range(kp):
	pos = a[pos]-1

print(pos+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = [-1 for _ in range(n)]

curr = 0
cnt = 0
k += 1
while k > 1:
  if b[curr] != -1:
    loop = cnt - b[curr]
    break
  b[curr] = cnt
  cnt += 1
  k -= 1
  curr = a[curr] - 1

if k != 1:
  mod = k % loop
  for i in range((mod-1) % loop):
    curr = a[curr] - 1

print(curr + 1)
from collections import defaultdict

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

loop = [0]
town = 0
visited = defaultdict(lambda: None)
visited[town] = 0

period = 0
for i in range(1, k+1):
    town = a[town]
    if visited[town] != None:
        offset = visited[town]
        period = i - offset 
        break
    else:
        loop.append(town)
        visited[town] = i

if period == 0:
    print(town+1)
else:
    print(loop[offset+((k-offset)%period)]+1)
n,k=map(int,input().split())
a=list(map(int,input().split())) 
 
x=[0]*(n)
before = 0
now = 0
while x[now] == 0:
    x[now]=1+x[before]
    before=now
    now = a[now]-1
    
y=x[now]-x[0]
z=x[before]-y
if k>y:
    l=(k-y)%z+1
else :
    l=k-y+1
for i in range(n):
    if x[i] == l+y:
        print(1+i)
        break
N,K=map(int,input().split())
A=list(map(int,input().split()))

move=[]
for i in range(0,N):
    move.append([])
    A[i]-=1
    move[i].append(A[i])


for j in range(1,64):
    for i in range(0,N):
        move[i].append(move[move[i][j-1]][j-1])
town=0
for i in range(0,64):
    if K%2==1:
        town=move[town][i]
    K//=2

print(town+1)
n,k=map(int,input().split())
l=list(map(int,input().split()))
i=0
cnt=0
lcnt=0
sl=[0]*n
for p in range(k):
  if sl[i]==0:
    sl[i]=1
    i=l[i]-1
    cnt+=1
  elif sl[i]==1:
    sl[i]=2
    lcnt+=1
    i=l[i]-1
  else:
    c=(k-cnt)%lcnt
    if c!=0:
      for tmp in range(c):
        i=l[i]-1
    break
print(i+1)
n,k,*a=map(int,open(0).read().split())
d={}
s=1
while k:k-=1;s=a[s-1];k%=d.get(s,2**61)-k;d[s]=k
print(s)
n, k  = map(int, input().split())
an = list(map(int, input().split()))
bn = [-1]*(n+1)

now=1
bn[1]=0
for i in range(1, k+1):
 #   print(bn)
    now = an[now-1]
    if bn[now] >0:
        rp =i-bn[now]
        p= (k-i)%rp
        for j in range(p):
            now = an[now-1]
        print(now)
        exit(0)
    bn[now]=i
print(now)
tem1=input().split()
N = int(tem1[0])
K = int(tem1[1])
tem2 = input().split()
jud = [0 for i in range(len(tem2))]
now =1
circle=0
for i in range(0,K):
    if jud[now-1]==0:
        jud[now-1]=i
        now = int(tem2[now - 1])
    else:
        circle=i-jud[now-1]
        fact=(K-i-1)%circle
        for j in range(0,fact+1):
            now = int(tem2[now - 1])
        break

print(now)
import copy
n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))

now = 0
while k > 0:
    if k & 1:
        now = a[now]

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

    a = copy.copy(a_next)
    k = k >> 1

ans = now + 1
print(ans)
n, k = map(int, input().split())
arr = list(map(int, input().split()))
visited = [-1] * n
visited[0] = 0

i = 0
ptr = 1
while True:
    ptr = arr[ptr-1]
    i += 1
    k -= 1
    if k == 0:
        break
    if visited[ptr-1] != -1:
        k %= i-visited[ptr-1]
    visited[ptr-1] = i
    if k == 0:
        break

print(ptr)
n, k = map(int, input().split())
tel = list(map(int, input().split()))
log = [1]
record = [0] * n
while(True):
    log.append(tel[log[-1] - 1])
    if(record[log[-1] - 1] == 1):
        break
    record[log[-1] - 1] += 1
for i in range(len(log) - 1):
    if(log[i] == log[-1]):
        star = i
        mod = len(log) - star - 1
        break
if(k < len(log)):
    print(log[k])
else:
    print(log[(k - star) % mod + star])
N = [int(e) for e in input().split()]
nums = [int(t) for t in input().split()]
count=[]
k = 0
check = []
t = nums[0]
for i in range(N[0]):
    count.append(0)
while count[t-1] == 0:
    check.append(t)
    count[t-1] += 1
    k = t -1
    t = nums[k]

s = check.index(t) + 1
if(N[1] < s):
    print(check[N[1] - 1])
else:
    ans = (N[1] - s)%(len(check)+1-s)
    ans1 = check.index(t) + ans
    print(check[ans1])
import sys


sys.setrecursionlimit(10 ** 6)


def dfs(v):
    path.append(v)
    visited[v] = 1
    nv = A[v]
    if visited[nv] == 1:
        return nv
    return dfs(nv)


N, K = map(int, input().split())
A = list(map(lambda x: int(x) - 1, input().split()))
visited = [0] * N
path = []
cycle_start = dfs(0)
cycle_len = len(path) - path.index(cycle_start)
head_len = len(path) - cycle_len
if K <= head_len:
    print(path[K] + 1)
else:
    K -= head_len
    print(path[head_len + K % cycle_len] + 1)
n,k = (int(x) for x in input().split())
a = tuple([int(x) for x in input().split()])
 
c = [0] + [-1]*(n)
l = [1]
now = 1
dupl = -1

for i in range(k):
  now = a[now - 1]
  c[now - 1] += 1
   
  if c[now - 1] == 1:
    dupl = now
    break
  else:
    l.append(now)
    
if dupl == -1:
  print(now)
  exit()
       
notloop = l.index(dupl)
length = len(l)-notloop
print(l[(k-notloop)%length+notloop])
from copy import copy
n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]
ans = 0
while k:
    if k & 1:
        ans = a[ans]
    c = [a[a[i]] for i in range(n)]
    a = copy(c)
    k >>= 1
print(ans + 1)
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [a-1 for a in A]

L = 65
D = [[0]*n for _ in range(L+1)]

for i, a in enumerate(A):
    D[0][i] = a

for i in range(L):
    for j in range(n):
        D[i+1][j] = D[i][D[i][j]]

ans = 0
for i in range(L):
    if k & (1 << i):
        ans = D[i][ans]
print(ans+1)
N, K = map(int, input().split())
A = list(map(int, input().split()))

s = [-1] * N
i = 0
cur = 0
m = 0
while True:
    if s[cur] != -1 and m != 1:
        m = i - s[cur]
        K = i + (K - i) % m
    else:
        s[cur] = i
    if i == K:
        print(cur+1)
        exit()
    i += 1
    cur = A[cur] - 1
# input
N, K = map(int, input().split())
A = [0]
A.extend(list(map(int, input().split())))

# process
l = [0]*(N+1)
now = 1
i = 1
while l[now] == 0:
    l[now] = i
    now = A[now]
    i += 1

ans = 0
if K < l[now]:
    ans = l.index(K+1)
else:
    k = (K-l[now]+1)%(len([x for x in l if x!=0])-l[now]+1)
    ans = l.index(l[now]+k)

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

visited = dict()
a = 1
ct = 0
while a not in visited:
    visited[a] = ct
    a = A[a]
    ct += 1
    if ct == K:
        print(a)
        exit()
inv_v = {v: k for k, v in visited.items()}
print(inv_v[(K - visited[a]) % (len(visited) - visited[a]) + visited[a]])
N,K =map(int,input().split())
A=list(map(int,input().split()))

n=[1]
t=1
ord=[-1]*(N+1)
ord[0]=0

while True:
    t=A[t-1]
    if ord[t] != -1:
        f=n.index(t)
        break
    n.append(t)
    ord[t]=0

k=len(n)-f

if K>=f:
    print(n[f+(K-f)%k])

else:
    print(n[K])
n,k = map(int,input().split())
a = list(map(int,input().split()))

machi = 1
toutatu = []
pypyosoi = set()
while not machi in pypyosoi:
	toutatu.append(machi)
	pypyosoi.add(machi)
	machi = a[machi-1]

saigo = machi
num = toutatu.index(saigo)
unchi = toutatu[num:]
cnt = len(unchi)

if num > k:
	print(toutatu[k])
else:
	print(unchi[(k-num)%cnt])

#pypyが遅いのは自分のコードが悪いだけごめんねpypyちゃん
n,k=map(int,input().split())

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

l=[1]
di={}
di[1]=1

for i in a:
    b=a[l[-1]-1]
    if b in di:
        x=b
        break
    l.append(b)
    di[b]=1
t1=0
for j in l:
    if j==x:
        break
    else:
        t1+=1

t2=len(l)-t1
if k<=t1:
    print(l[k])

else:
    aa=(k-t1)%t2
    print(l[t1+aa])
n,k = map(int,input().split())
a = list(map(int,input().split()))
for i in range(n):
    a[i] -= 1
path = [0]
now = 0
visited = {0}
p = 0
while True:
    if a[now] in visited:
        p = a[now]
        break
    now = a[now]
    visited.add(now)
    path += [now]
p_index = 0
for i in range(len(path)):
    if path[i] == p:
        p_index = i
        break

if k <= p_index:
    print(path[k] + 1)
else:
    print(path[p_index + (k-p_index)%len(path[p_index:])] + 1)
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
 
n, k = map(int, input().split())
a = [int(item) - 1 for item in input().split()]
visited = [0] * n
 
v = 0
visited[0] = 1
pre = [0]
loop = []
start = False
while True:
    v = a[v]
    if visited[v] == 1:
        start = True
    if visited[v] == 2:
        break
    if not start:
        pre.append(v)
    else:
        loop.append(v)
    visited[v] += 1 
 
if k < len(pre):
    print(pre[k] + 1)
    exit()
k -= len(pre)
print(loop[k % len(loop)] + 1)
N,K = map(int,input().split())
A = list(map(int,input().split()))
L = [1]
s = set()
s.add(1)
now = 1
for _ in range(N):
    if A[now - 1] in s:
        K -= L.index(A[now - 1])
        if K >= 0: K %= len(L) - L.index(A[now - 1])
        break
    L.append(A[now - 1])
    s.add(A[now - 1])
    now = A[now - 1]
#print(L)
#print(K)
print(L[K + L.index(A[now - 1])])
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

s = 1
g = 0
i = 1
history = [s]
flag = [False] * n
flag[0] = True

while True:
    g = a[s-1]
    if flag[g-1] == True:
        break
    else:
        flag[g-1] = True
        history.append(g)
        s = g
        i += 1


x = history.index(g)

if k < i:
    ans = history[k]
else:
    d = (k-x) % (i - x)

    ans = history[x + d]

print(ans)
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.insert(0, None)

history = [None] * (N+1)

next_town = 1
i = 0
skip_flg = False
while i < K:
  if history[next_town] == None:
    history[next_town] = i
  elif skip_flg == False:
    skip_flg = True
    
    i = i + ((K - i)//(i - history[next_town])) * (i - history[next_town])
  if i < K:
    i += 1
    next_town = A[next_town]
print(next_town)
n,k=map(int,input().split())
a=tuple(map(int,input().split()))
flag=[-1]*n
flag[0]=0
x,count=1,0
for i in range(1,n+1):
  x=a[x-1]
  if flag[x-1]<0:
    flag[x-1]=i
  else:
    l=i-flag[x-1]
    count=flag[x-1]
    break
if count>=k:
  x=1
  for _ in range(k):
    x=a[x-1]
  print(x)
  exit()
k=(k-count)%l
for _ in range(k):
  x=a[x-1]
print(x)
def getans(v,K):
    k=0
    now=v
    while K>0:
        if K&1:
            now=doubling[k][now]
        K=K>>1
        k+=1
    return now

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

doubling=[[-1]*N for i in range(61)]
for i in range(N):
    doubling[0][i]=A[i]-1
for k in range(1,61):
    for i in range(N):
        doubling[k][i]=doubling[k-1][doubling[k-1][i]]
print(getans(0,K)+1)
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [x-1 for x in A]

town = 0
cnt = 0

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

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

town = 0

if k >= start_cnt:
    k = (k-start_cnt)%loop+start_cnt

for i in range(k):
    town = A[town]

print(town+1)
N, K = map(int, input().split())
A = list(map(int, input().split()))

flag = [False] * N
counted = [1]

nextT = A[0]
flag[0] = True
for x in range(N):
    if flag[nextT - 1] is False:
        counted.append(nextT)
        flag[nextT - 1] = True
        nextT = A[nextT - 1]
    else:
        break
before_loop_len = counted.index(nextT)
loop_len = x - before_loop_len + 1

if K <= before_loop_len:
    print(counted[K])
else:
    checkK_prep = K - before_loop_len
    checkK = checkK_prep % loop_len
    print(counted[before_loop_len + checkK])
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=tuple(l[l[i]-1]for i in range(n))
        k//=2
    print(now)
if __name__ == "__main__":
    f()
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(place+1)
N,K=map(int,input().split())
P=list(map(int,input().split()))

A={k+1:P[k] for k in range(N)}


U=list()
S=set()
x=1
k=0

while (x not in S) and (k<=K):
    U.append(x)
    S.add(x)
    
    x=A[x]
    k+=1

if k>K:
    print(U[-1])
else:
    d=U.index(x)
    V=U[d:]
    print(V[(K-d)%len(V)])
import sys
import time

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

queue = {}

start = -1
count = 1

now_town = 1

for i in range(K):
    if town_list[now_town - 1] in queue:
        start = town_list[now_town - 1]
        break
    queue[town_list[now_town - 1]] = count
    now_town = town_list[now_town - 1]
    count += 1

if start == -1:
    print(now_town)
    sys.exit()

roop = len(queue) - (queue[start] - 1)
before_len = queue[start]

print(list(queue.keys())[(K - before_len) % roop + before_len - 1])
n,k=map(int,input().split())
a=list(map(int,input().split()))
check=[[] for i in range(n)]
c=0
g=1
tmp=[]
cy=[]
non=[]

while c<n:
    if check[g-1]==[]:
        c+=1
        tmp.append(g)
        check[g - 1].append(c)
        g=a[g-1]
    else:
        cy=tmp[(check[g-1][0]-1):]
        non=tmp[:(check[g-1][0]-1)]
        break
if k<len(non):
    print(non[k])
    exit()
k-=len(non)
k%=len(cy)


print(cy[k])
# D - Teleporter

n, k = map(int, input().split())
a = list(map(int, input().split()))
assert len(a) == n

# なげなわ検出
p = [1]
hash = {1: 0}  # p[hash[1]] == 1
while True:
    x = a[p[-1] - 1]
    if x in hash:
        break
    hash[x] = len(p)  # p[hash[x]] == x
    p.append(x)

# 0, 1, 2, ..., (hash[x], ..., len(p)-1)*
looplen = len(p) - hash[x]
if k < len(p):
    print(p[k])
else:
    print(p[(k - len(p)) % looplen + hash[x]])
N, K = map(int, input().split())

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

now = 1

d = []
set_d = set()

while True:
    if A[now-1] in set_d:
        i = d.index(A[now-1])
        check = d[i:]
        break
    now = A[now-1]
    d.append(now)
    set_d.add(now)

    if len(set_d) >= K:
        break

if K <= len(set_d):
    print(d[K-1])
else:
    K -= (len(set_d)-len(check))
    K = K % len(check)
    print(check[K-1])
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
w=[1]
x=set([1])
for i in range(k):
    t=a[w[-1]-1]
    if t in x: break
    x.add(t)
    w.append(t)
p=w.index(t)
k-=p
loop=len(w)-p
print(w[p+k%loop])
N, K = map(int, input().split())
A = list(map(lambda x: x - 1, map(int, input().split())))
current = 0
memo = {
    0: 0
}
for i in range(1, K + 1):
    current = A[current]
    if current in memo:
        # loop length
        length = i - memo[current]
        tmp = (K - memo[current]) % length
        for j in range(tmp):
            current = A[current]
        break
    else:
        memo[current] = i
print(current + 1)
n,k = map(int,input().split())
tel = [int(e)-1 for e in input().split()]

pl = [0]
for i in range(2*n+1000):
    nxt = tel[pl[-1]]
    pl.append(nxt)

if k < n + 1:
    print(pl[k]+1)
    exit()

loop_end = n
loop_start = n-1
while pl[loop_start] != pl[loop_end]:
    loop_start -= 1

#pl[loop_start] == pl[loop_end]
period = loop_end - loop_start

k %= period

while k < n:
    k += period

print(pl[k]+1)
N,K = map(int, input().split())
A = [int(a)-1 for a in input().split()]

used = [-1]*N
cnt = 0
town = 0
used[0] = 0
while True:
    town = A[town]
    cnt += 1
    if used[town] >= 0:
        cnt = cnt-used[town]
        break
    used[town] = cnt

if K > used[town]:
    K -= used[town]
    K %= cnt
    K += used[town]
ans = used.index(K)+1
print(ans)
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = []
i = 0
B.append(1)
for n in range(N):
  B.append(A[i])
  i = A[i]-1
if K>N:
  start = 0
  num = 0
  for j in range(len(B)):
    if B[len(B)-1] == B[j]:
      start = j
      break
  for j in range(1,len(B)-start):
    if B[start+j] == B[start]:
      num = j
      break
  print(B[((K-start)%num)+start])
else:
  print(B[K])
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,K = map(int, readline().split())
    A = [int(i) - 1 for i in readline().split()]

    p = 0
    while K:
        if K & 1:
            p = A[p]
        A = [A[a] for a in A]
        K >>= 1
    
    print(p + 1)


if __name__ == "__main__":
    main()
N, K = map(int, input().split())
A = input().split()

root = [1]
pastTown = {1:0}
t = 0
nextTown = 1

while 1:
  t += 1
  nextTown = int(A[nextTown - 1])
  if nextTown in pastTown.keys():
    break
  else:
    root.append(nextTown)
    pastTown[nextTown] = t

if nextTown == root[0]:
  town = root[K % len(root)]
else:
  if K <= pastTown[nextTown]:
    town = root[K]
  else:
    tmp = (K - pastTown[nextTown]) % (len(root) - pastTown[nextTown])
    town = root[pastTown[nextTown] + tmp]

print(town)
N,K = map(int,input().split())
A = list(map(int,input().split()))
town = 1
record = [1]
record2 = [0]*N
for i in range(N):
    town = A[town-1]
    if record2[town-1]==1:
        break
    record.append(town)
    record2[town-1] += 1
    
newrecord = record[record.index(town):]
roop = len(newrecord)
newK = K-(len(record)-roop)
if newK>=0:
    answer = newrecord[newK%roop]
else:
    answer = record[K]

print(answer)
from collections import deque
n,k=map(int, input().split())

A = deque(map(int, input().split()))
li = []
li2 = [0]*n
li.append(1)

next = 0

t=0
rep = False

for i in range(k):
    if i == k:
        print(next+1)
        exit()

    if li2[next] == 1:
        t = li.index(next+1)
        rep = True
        li.pop()
        break

    li.append(A[next])
    li2[next] = 1
    next = A[next]-1


tmp = (k-t)%len(li[t:])
print(li[tmp+t])
N, K = list(map(int, input().split()))
A = list(map(lambda x: int(x) - 1, input().split()))

v = [A[:]]
now = 0
for i in range(60):
    tmp = v[-1]
    v.append([tmp[x] for x in tmp])
    if K>>i & 1:
        now = v[i][now]
print(now + 1)
import sys

input = sys.stdin.readline
n,k = map(int,input().split())
a = list(map(int,input().split()))

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

if k <= n:
    print(p[k])
else:
    # 周期の来る前の回数
    num = p.index(p[-1])
    p = p[num:]

    # 周期
    pn = len(p)-1
    k -= num

    # 回数/周期のあまり
    ans = k%pn
    print(p[ans])
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(point):]
ll = len(S)
l = len(loop)
if ll > M:
    print(S[M]+1)
elif ll == M:
    print(point+1)
else:
    print(loop[(M-ll)%l]+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))

s = 1
place = [s]
first = set(place)

for i in range(1, k+1):
    s = a[s-1]
    if s in first:
        loops = place.index(s)
        loope = len(place)
        ans = loops + (k-loope) % (loope-loops)
        print(place[ans])
        exit()
    else:
        place.append(s)
        first.add(s)

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

b = 0
r = k + 1
seen = [-1] * n
seen[0] = 0
p = 1
for i in range(k):
    p = a[p - 1]
    if seen[p - 1] != -1:
        b = seen[p - 1]
        r = i + 1 - b
        for j in range((k - b) % r):
            p = a[p - 1]
        print(p)
        exit()
    seen[p - 1] = i + 1
else:
    print(p)
N,K = map(int,input().split())

A = list(map(int,input().split()))
town = [[] for _ in range(N)]
visited = [0]
flag = [-1] * N

for i in range(N):
  town[i] = [i,A[i] - 1]

f = 0

while flag[f] == -1:
  t = town[f][1]
  flag[f] = t
  f = town[t][0]
  visited.append(t)
  
l = visited[-1]
lv = len(visited)

if K <= lv:
  print(visited[K] + 1)
else:
  cs = visited.index(l)
  cycle = visited[cs:-1]
  lc = len(cycle)
  print(cycle[(K - lv + 1) % lc] + 1)
##i
def main():
    N, K = map(int, input().split())
    D = K.bit_length()
    to = [[0] * D for _ in range(N)]

    for i, a in enumerate(map(int, input().split())):
        to[i][0] = a - 1

    for j in range(D - 1):
        for i in range(N):
            to[i][j+1] = to[to[i][j]][j]

    v = 0
    for i in reversed(range(D)):
        if (K >> i) & 1:
            v = to[v][i]

    print(v + 1)


if __name__ == "__main__":
    main()
n, k = map(int,input().split())
a = list(map(int,input().split()))

s = [False] * n
c = [1]
now = 0
while not s[now]:
    s[now] = True
    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])
import collections
import numpy as np
import sys
import copy

N,K=map(int,input().split())
A=list(map(int,input().split()))
for i in range(N):
    A[i]-=1
B=[0]
visited=[0]*N
visited[0]=1
Flag=True
for i in range(0,N*2):
    next=A[B[i]]
    if visited[next]==0:
        B.append(next)
        visited[next]=1
    else:
        C=B[B.index(next):]
        num=B.index(next)
        break
#print(C)
if K>num:
    Ans=(K-num)%len(C)
    print(C[Ans]+1)
else:
    print(B[K]+1)