peterverebics/CodeNet_Python_118 huggingface dataset:
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())
loop_=[1]
l_set=set()
lst=list(map(int,input().split()))

tar=None
for i in range(k):
    num=lst[loop_[-1]-1]
    if num in l_set:
        tar=num
        break
    else:
        loop_.append(num)
        l_set.add(num)

if tar==None:
    print(loop_[k])
    exit()    
loop__=loop_[loop_.index(tar):]

num=k+1-len(loop_)
try:
  print(loop__[num%len(loop__)-1])
except:
  print(1)
n, k, *a = map(int, open(0).read().split())
now = 0
st = set()
ls = []
while not now in st:
    st.add(now)
    ls.append(now)
    now = a[now]-1
bi = ls.index(now)
lp = len(ls) - bi
if k<=bi: print(ls[k]+1)
else:
    k -= bi
    k %= lp
    print(ls[bi+k]+1)
from collections import defaultdict

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

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

visited = [0] * N

current = 0
i = 1
while i <= K:
    current = A[current] - 1
    if visited[current] == 0:
        visited[current] = i
    else:
        loop = i - visited[current]
        num_loop = (K - i) // loop
        i += loop * num_loop

    i += 1

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

path = [1]
chk = {1}
idx = 1
loop_s = 1
loop_e = n

for i in range(1, n + 1):
    if a[idx] not in chk:
        idx = a[idx]
        path.append(idx)
        chk.add(idx)
    else:
        loop_s = path.index(a[idx])
        loop_e = i
        break

if k <= loop_s:
    print(path[k])
else:
    print(path[loop_s + (k - loop_s) % (loop_e - loop_s)])
n,k=map(int,input().split())
a=list(map(int,input().split()))

visited = [-1]*(n+1)
pos = 1
visited[pos] = 0
loop = 0
root=[1]
for i in range(1,n+1):
  pos = a[pos-1]
  if visited[pos] == -1:
    visited[pos] = i
    root.append(pos)
  else:
    loop = i - visited[pos]
    mae = visited[pos]
    break
if k < mae:
  print(root[k])
else:
  index=(k-mae)%loop
  print(root[index+mae])
N,K=map(int, input().split())
a_ls=list(map(int, input().split()))

j=1
step=0
s=set([])
l=[]
loop_s=-1
loop_g=-1

while True:
	s.add(j)
	l+=[j]
	j=a_ls[j-1] #update
	if j in s:
		loop_s=l.index(j)
		loop_g=len(l)
		break


#print(l)
#print(loop_s)
#print(loop_g)

if K<loop_s:
	print(l[K])
elif (K+1-loop_s)%(loop_g-loop_s)!=0:
	print(l[loop_s+(K+1-loop_s)%(loop_g-loop_s)-1])
else:
	print(l[-1])
N,K = map(int, input().split())
A = [*map(lambda x:int(x)-1, input().split())]

cnt, pre = [0]*N, 0
for _ in range(N*2):
    cnt[pre] += 1
    pre = A[pre]

one, loops = 0, 0
for c in cnt:
    if c == 1: one += 1
    elif c > 1: loops += 1

k = max(0, K-one) % loops + min(K, one)

pre = 0
for _ in range(k): pre = A[pre]
print(pre + 1)
N,K = map(int,input().split())
A = [0] + list(map(int,input().split()))

now = 1

while K:
  if K & 1:
    now = A[now]
  A = [A[A[i]] for i in range(len(A))]
  K >>= 1
  
print(now)
n, k = map(int, input().split())
a = list(map(int, input().split()))

pos = 0
visit = [0]*n
move = []
roop = []
while visit[pos] != 2:
    if visit[pos] == 0:
        move.append(pos)
    else:
        roop.append(pos)
    visit[pos] += 1
    pos = a[pos] - 1
if len(move) > k:
    print(move[k]+1)
else:
    print(roop[(k-(len(move)-len(roop))) % len(roop)]+1)
N,K = map(int,input().split())
A = list(map(lambda x:int(x)-1,input().split()))

F = [False]*N
L = []
cnt = 0
ans = -1
x = 0
first = -1
while 1:
    if first==x:break
    if cnt==K:
        ans = x
        break
    if F[x]:
        if first==-1:
            first = x
        L.append(x)
    F[x]=True
    cnt += 1
    x = A[x]
if ans==-1:
    N = len(L)
    ans = L[(K-cnt)%N]
print(ans+1)
n,k=map(int,input().split())
A=list(map(int,input().split()))

time=0
color=["white" for _ in range(n+1)]
D=[-1 for _ in range(n+1)]
C,s=[],A[0]
point=0
while 1:
    if color[s]=="white":
        color[s]="gray"
        C.append(s)
        D[s]=time
        time +=1
        s=A[s-1]
    else:
        point=D[s]
        break

loop=C[point:]
if k>len(C):
    k -=point+1
    print(loop[k%len(loop)])
else:print(C[k-1])
import sys
input = sys.stdin.readline

n, k = (int(x) for x in input().split())
seq = [int(x) for x in input().split()]
visited = [[False,0] for _ in xrange(n)]

t = 1
cnt = 0
if n < k:
    while not visited[t-1][0]:
        visited[t-1][0] = True
        visited[t-1][1] = cnt
        t = seq[t-1]
        cnt += 1

    cycle_len = cnt - visited[t-1][1]
else:
    cycle_len = n
for _ in xrange((k-visited[t-1][1]) % cycle_len):
    t = seq[t-1]
print(t)
n,k=map(int,input().split())
a=list(map(int,input().split()))

b=[1]
Flag=True
now=1
arrived=set(b)
memo=1
while Flag:
    b.append(a[now-1])
    if a[now-1] in arrived:
        Flag=False
        memo=a[now-1]
    arrived.add(a[now-1])
    now=a[now-1]

loop=len(b)-b.index(memo)-1

first=b.index(memo)


if k>first:
    p,q=divmod(k-first,loop)
    print(b[first+q])
else:
    print(b[k])
N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N):
    A[i] -= 1
L = [0]
t = 0
F = [-1 for _ in range(N)]
F[0] = 0
c = 1
while F[A[t]] == -1:
    t = A[t]
    F[t] = c
    c += 1
    L.append(t)
loop = c - F[A[t]]
buffer = c - loop
if K < buffer:
    print(L[K] + 1)
else:
    NL = L[buffer:]
    amari = (K - buffer) % loop
    print(NL[amari] + 1)
n,k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]
loop = []
seen = [0]*n
num = 0
flag = 0
while k>0:
    if seen[num]==1:
        flag = 1
        break
    seen[num]=1
    loop.append(num)
    num = a[num]
    k-=1
if flag:
    index = loop.index(num)
    loop = loop[index:]
    print(loop[(k%len(loop))]+1)
else:
    print(num+1)
N, K = map(int, input().split())
A = list(map(int, input().split()))
T = [1]
c = [0]*N

k = 1
st = 0
c[0] = 1
cc = 1
for i in range(N):
    k = A[k-1]
    if c[k-1] == 1:
        st = T.index(k)
        f = T[:st]
        r = T[st:]
        break

    T.append(k)
    c[k-1] += 1
    cc += 1

ff = len(f)
if K < ff:
    print(f[K])
else:
    print(r[(K-ff)%(cc-ff)])
N,K = map(int,input().split())
A = list(map(int,input().split()))
pos = 0
visit = [0] * N
move = []
roop = []
while visit[pos]!=2:
    if visit[pos]==0:
        move.append(pos)
    else:
        roop.append(pos)
    visit[pos] += 1
    pos = A[pos] - 1
if len(move)>K:
    print(move[K]+1)
else:
    print(roop[(K-(len(move)-len(roop)))%len(roop)]+1)
n, k = map(int, input().strip().split())
L = list(map(int, input().strip().split()))
L = [v - 1 for v in L]
 
seen = set()
intersec = 0
route=[]
while intersec not in seen:
    seen.add(intersec)
    route.append(intersec)
    intersec = L[intersec]

intersecInd = route.index(intersec)

if k<=intersecInd:
    ans=route[k]
else:
    loopSize = len(route) - intersecInd
    ans = route[(k - intersecInd) % (loopSize) + intersecInd]
 
print(ans + 1)
import numpy as np
N, K = (int(i) for i in input().split())  
A = [int(i) - 1 for i in input().split()] 
N = len(A)

def get_keys_from_value(d, val):
    return [k for k, v in d.items() if v == val][0]

now = 0
lis = []
p = {}
for i in range(N):
  now = A[now]
  if now in p:
    break
  
  p[now] = i
  lis.append(now)

if K < i:
  print(lis[K-1] + 1)
else:
  print(lis[(K-i-1) % (i - p[now]) + p[now]] + 1)
n, k = map(int, input().split())
a = [0] + [int(i) for i in input().split(" ")]
b = [0,1] + [0]*(n-1)
c = [1]
nex = 1
for i in range(1,n+1):
    nex = a[nex]
    if b[nex] == 1:
        whe = c.index(nex)
        lo = len(c)-whe
        break
    b[nex] = 1
    c.append(nex)
if whe+1 >= k:
    print(c[k])
else:
    k_ = k - whe
    print(c[whe:][k_%lo])
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))

journey = [1]

for i in range(2 * n + 100):
    next = a[journey[-1]]
    journey.append(next)

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

loop_end = n

loop_start = n - 1

while (journey[loop_start] != journey[loop_end]):
    loop_start -= 1

period = loop_end - loop_start
k %= period

while k < n:
    k += period

print(journey[k])
#!python3

def LI():
    return list(map(int, input().split()))

# input
N, K = LI()
A = LI()


def main():
    n = 0
    x = 1
    visit = [None] * N
    while visit[x - 1] is None:
        if n == K:
            print(x)
            return
        visit[x - 1] = n
        n += 1
        x = A[x - 1]
    
    v = n - visit[x - 1]
    s = (K - n) % v
    ans = visit.index(visit[x - 1] + s) + 1
    print(ans)


if __name__ == "__main__":
    main()
from sys import exit
N,K = map(int,input().split())
town = [None]+list(map(int,input().split()))

flag = [None]+[0]*N
remain = K
piece = 1
cnt = 1
while remain > 0:
    if flag[town[piece]] != 0:
        q = cnt-flag[town[piece]]
        piece = town[piece]
        remain -= 1
        break
    else:
        piece = town[piece]
        flag[piece] = cnt
        remain -= 1
        cnt += 1

if remain == 0:
    print(piece)
    exit(0)

remain %= q

while remain > 0:
    piece = town[piece]
    remain -= 1

print(piece)
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))

seen = [False] * (n+1)
visited = []
nxt = 1
cnt = 0
while not seen[nxt] and cnt != k:
    seen[nxt] = True
    visited.append(nxt)
    nxt = a[nxt]
    cnt += 1

if cnt == k:
    print(nxt)
    exit()
    
idx = visited.index(nxt)
print(visited[idx:][(k-idx)%(len(visited)-idx)])
n,k,*a=map(int,open(0).read().split())
d=[2*k]*n
s=0
while k:s=a[s]-1;d[s]=k=~-k%(d[s]+1-k)
print(s+1)
N, K = map(int,input().split())

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

Visited = [-1]*N
now = 1
Order = [0]*N

loopcount = 1
Order[0] = 1
Visited[0] = 1
for i in range(1,N):
    nt = Map[now-1]
    Order[i] = nt
    loopcount += 1
    if Visited[nt-1] == -1:
        Visited[nt-1] = loopcount
    else:
        break
    now = nt
    

ns = Visited[nt-1]
period = loopcount - ns

if period == 0:
    period = loopcount
    ns = 0

Kmod = K - ns
if Kmod > 0:
    Kmod %= period

print(Order[ns+Kmod])
n,k,*a=map(int,open(0).read().split())
d=[2*k]*-~n
s=1
while k:k-=1;s=a[s-1];k%=d[s]-k;d[s]=k
print(s)
from collections import defaultdict
n,k=map(int,input().split())
a=list(map(int,input().split()))

order = []
now = 1
count = 0
d = defaultdict(int)


for i in range(n):
    if d[now]!=0:
        s=d[now]-1
        g=i
        break
    order.append(now)
    d[now] = i+1
    now = a[now-1]


if k<=s:
    print(order[k])
else:
    k-=s
    loop = order[s:g]
    print(loop[k%len(loop)])
N, K = map(int, input().split())
A = list(map(int, input().split()))
R = {1:0}
i, t = A[0], 1

while K > t:
    r = R.get(i)
    if r == None:
        R[i] = t
        i = A[i-1]
        t += 1
    else:
        K -= r
        K %= t - r
        for k in range(K):
            i = A[i-1]

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

x=1
while K:
    if K&1: x=A[x-1]
    K>>=1

    A2 = [None]*N
    for i,a in enumerate(A):
        A2[i]=A[A[i]-1]
    A=A2

print(x)
n,k=map(int,input().split())
l=list(map(int,input().split()))
double=[[0]*(n+1) for i in range(61)]
for i in range(1,n+1):
    double[0][i]=l[i-1]
for i in range(1,61):
    for j in range(n+1):
        double[i][j]=double[i-1][double[i-1][j]]
ans=1
for i in range(61):
    if k>>i&1:
        ans=double[i][ans]
print(ans)
n, m = map(int, input().split())
lis = list(map(lambda z: int(z) - 1, map(int, input().split())))

s = {0}
res = [0]

while lis[res[-1]] not in s:
    res.append(lis[res[-1]])
    s.add(res[-1])

x, y = res.index(lis[res[-1]]), len(res)

if m < y:
    print(res[m] + 1)
else:
    mm = (m - x) % (y - x)
    print(res[mm + x] + 1)
N,K = map(int,input().split())
A = list(map(int,input().split()))

counter = 0
place_list = []
place_set = set()
next = 1
while True:
    
    if  next in place_set:
        break
        
    else:
        place_list.append(next)
        place_set.add(next)
        next = A[next-1]
        
    counter += 1

duration_cycle = len(place_list)- place_list.index(next)
if K <len(place_list):
    print(place_list[K])
else:
    print(place_list[(K-place_list.index(next))%duration_cycle+place_list.index(next)])
N,K=map(int,input().split())
A=[int(x)-1 for x in input().split()]
B=[0]
visited=[False]*N
for g,here in enumerate(B):
	if visited[here]:
		break
	visited[here]=True
	B.append(A[here])
s=B.index(here)
r=(K-s)%(g-s)
ans=B[s:][r]+1 if K>=s else B[K]+1
print(ans)
n,k = map(int,input().split())
a = list(map(int,input().split()))
t=[[0 for i in range(n)] for j in range(64)]
for i in range(64):
    for j in range(n):
        if i == 0:
            t[i][j]=a[j]
        else:
            t[i][j]=t[i-1][t[i-1][j]-1]
ans=1
for i in range(64):
    r = k%2
    k = k//2
    if r == 1:
        ans = t[i][ans-1]
    if k == 0:
        print(ans)
        exit()
N,K = map(int,input().split())
A = list(map(lambda x: int(x) - 1, input().split()))
P = [-1] * N 
cur = 0
P[cur] = 0
route = [1]

for step in range(K):
  cur = A[cur]
  route.append(cur + 1)
  if P[cur] >= 0:
    l_start = P[cur]
    l_end = step + 1
    l_len = l_end - l_start
    break
  else:
    P[cur] = step + 1
else:
  print(cur + 1)
  quit()

print(route[l_start + ((K - l_start) % l_len)])
def main():
    N, K = [int(x) for x in input().split()]
    a = [int(x)-1 for x in input().split()]

    pos = 0
    while K > 0:
        if K&1:
            pos = a[pos]
        # doubling
        a = [a[a[i]] for i in range(N)]
        K >>= 1
    print(pos+1)


if __name__ == '__main__':
    main()
N, K = map(int, input().split())
P = list(map(int, input().split()))
for i in range(N):
    P[i] -= 1
M = 60
step = [[0]*(M+1) for _ in range(N)]

for i in range(N):
    step[i][0] = P[i]

for i in range(1, M+1):
    for j in range(N):
        c = step[j][i-1]
        c = step[c][i-1]
        step[j][i] = c

c = 0
for j in range(M+1):
    if K % 2:
        c = step[c][j]
    K //= 2
print(c+1)
N,K=map(int,input().split())
A=[int(a)-1 for a 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)
N, K = map(int, input().split())
A = list(map(lambda a: int(a) - 1, input().split()))
INF = 10**18

minDist = [INF] * N
minDist[0] = 0

now = 0
R = 0
while minDist[now] < INF:
    to = A[now]
    if minDist[to] < INF:
        R = minDist[now] + 1 - minDist[to]
        break
    minDist[to] = minDist[now] + 1
    now = to

if minDist.count(K) > 0:
    print(minDist.index(K) + 1)
    exit()

K -= minDist[now]
M = K % R
for _ in range(M):
    now = A[now]
print(now + 1)
n,k,*a=map(int,open(0).read().split())
b=[-1]*(n+1)
b[1]=0
c=a[0]
d=[0]*min(k+1,10**7)
d[0]=1
for i in range(1,k+1):
    d[i]=c
    if b[c]!=-1:
        # print(i,c,b[c])
        print(d[b[c]+(k-b[c])%(i-b[c])])
        exit()
    b[c]=i    
    c=a[c-1]
print(d[-1])
N, K = [int(x) for x in input().split()]
dest = [int(x) for x in input().split()]
route = [1]
flag = [1] + [0] * (N-1)
t = 1
while True:
  if flag[dest[t-1]-1] == 0:
    t = dest[t-1]
    route.append(t)
    flag[t-1] = 1
  else:
    loop = route.index(dest[t-1])
    L_loop = len(route) - loop
    break
  #print(route)
  #print(flag)
ans = 0
if K < len(route):
  ans = route[K]
else:
  K = K - len(route)
  ans = route[loop + (K % L_loop)]
print(ans)
N, K = map(int, input().split())
A = list(map(int, input().split()))
visited = [False]*N
path = [0]
at = 0
loop = 0
visited[0]=True
for i in range(N):
    at = A[at]-1
    if visited[at]:
        loop = i+1-path.index(at)
        break
    else:
        visited[at]=True
        path.append(at)
if K < len(path):
    print(path[K]+1)
else:
    print(path[(K-path.index(at))%loop+path.index(at)]+1)
N, K = map(int, input().split())
A = [int(a) - 1 for a 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)
n, k = map(int, input().split())
a = list(map(int, input().split()))

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

for i in range(k):
    num = a[num-1]
    if flag[num-1]:
        li.append(num)
        flag[num-1] = False
    else:
        break

d = li.index(num)
ans = (k-d) % (len(li)-d)+d
print(li[ans])
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

def l_in():
    return list(map(int, input().split()))

n,k=l_in()
A=l_in()

cnt=0
current = 0

memo={0: 0}

while cnt < k:
    n = A[current]-1
    cnt += 1

    if n in memo:
        delta = cnt - memo[n]

        p = (k-cnt)//delta
        cnt += p*delta
    else:
        memo[n] = cnt
    current = n
    
print(current+1)
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))

root = [1]
setroot = set(root)

for i in range(N):
    if A[root[-1]-1] in setroot:
        start = root.index(A[root[-1]-1])
        break
    setroot.add(A[root[-1]-1])
    root.append(A[root[-1]-1])

roop = root[start:]

if K <= start:
    print(root[K])
else:
    print(root[(K-start)%len(roop)+start])
n, k = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))
bit = k.bit_length()
table = [[0]*n for _ in range(bit)]
table[0] = A

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

ans = 0
for i in reversed(range(bit)):
    if (1 << i) <= k:
        k -= 1 << i
        ans = table[i][ans]
print(ans+1)
X = input().split()
N = int(X[0])
K = int(X[1])

H = input().split()
A = [int(z) for z in H]
B = []
s = []

for z in range(N):
	B.append(0)
	s.append(0)

C = 1
B[0] +=1
s[0] = 0

for i in range(K):
	C = A[C-1] 
	B[C-1] += 1
	if(B[C-1] == 2):
		break
	s[C-1] = i+1

r = i+1 - s[C-1]

if not r == 0:
	#uに余りを入れる
	t = (K - s[C-1])
	u = t % r
	for g in range(u):
		C = A[C-1] 

print(C)
n,k = map(int, input().split())
a = [0] + list(map(int, input().split()))

now = 1
check = [-1]*(n+1)

for i in range(k):
  if check[now] == -1:
    check[now] = i
    now = a[now]
  else:
    loop = i - check[now]
    afterloop = (k-(i-loop))%loop
    print(check.index((i-loop) + afterloop))
    exit()
print(now)
n,k=map(int,input().split())
mati=list(map(int,input().split()))
count=0
i=mati[0]
matidic={}
frag=1
while frag:
    count+=1

    if i in matidic:
        frag=0
        bef=matidic[i]
        roop=count-bef

    else:
        matidic[i]=count
    i = mati[i - 1]

#print(roop,bef,matidic)
#print(k%roop)
if bef<k:
    ans=[i for i,j in matidic.items() if j==(k-bef)%roop+bef]
else:
    ans=[i for i,j in matidic.items() if j==k]
print(ans[0])
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())
l1 = list(map(int,input().split()))
l = list(i-1 for i in l1)
bit = list(0 for i in range(n))
bit[0] =1
mi = [0]
m = l[0]
er = 0
while er ==0:
  if bit[m]==1:
    a = mi.index(m)
    r = mi[a:]
    er  =1
  else:
    bit[m] = 1
    mi.append(m)
    m = l[m]
 # print(bit)
be = mi[:a]+r
#print(be,r)
if k< len(be):
  print(be[k]+1)
else:
  w = k-len(be)+1
  u = (w%len(r))-1
  print(r[u]+1)
n,k = map(int,input().split())

A = list(map(int,input().split()))
t = 0
c = 0
ans = [0] * n
L = []
s = 0
for _ in range(k):
    if ans[t] == 1:
        n = len(L) - L.index(t)
        temp = (k-c)%n
        t = L[L.index(t):][temp]
        break
    else:
        ans[t] = 1
        L.append(t)
        t = A[t]-1
        c += 1
print(t+1)
from collections import defaultdict
n,k=map(int,input().split())
l=list(map(int,input().split()))
d=defaultdict(int)
for i in range(n):
    d[i+1]=l[i]
visited=set()
l=[]
i=1
while True:
    l.append(i)
    if i not in visited:
        visited.add(i)
    else:
        break
    i=d[i]
first=l.index(l[-1])
if k<first:
    print(l[k])
else:
    k=(k-first)%((len(l)-1)-first)
    k+=first
    print(l[k])
N,K = map(int,input().split())
A = list(map(int,input().split()))
s = 1
B = []
set_ = set()
for i in range(N):
    if A[s-1] not in set_:
        set_.add(A[s-1])
        B.append(A[s-1])
        s = A[s-1]
        
    else:
        e = i
        break
idx = B.index(A[s-1])
if K-1<idx: print(B[K-1])
else:
    print(B[idx + (K-idx-1)%(e-idx)])
n,k=map(int,input().split())
a=list(map(int,input().split()))

now=0
visited=set()
visited.add((0))
visited2=[0]


while True:
  k-=1
  now=a[now]-1

  if k==0:
    print(now+1)
    exit()

  
  if now in visited:
    visited.add((now))
    visited2.append(now)
    break

  visited.add((now))
  visited2.append(now)


xxx=visited2[-1]
cnt=[]
for i in range(len(visited2)):
  if visited2[i]==xxx:
    cnt.append(i)


k%=cnt[1]-cnt[0]


if k!=0:
  for i in range(k):
    now=a[now]-1
  

print(now+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))

route = []
town = set()
now = 1

while True:
    now = a[now-1]
    if now in town:
        break
    town.add(now)
    route.append(now)

count = route.index(now)
if count >= k:
    print(route[k-1])
else:
    route = route[count:]
    num = (k-count)%len(route)
    print(route[num-1])
n,k,*a=map(int,open(0).read().split())
v=[0]+[-1]*n
t=a[0]-1
c=1
while v[t]<0:
  v[t]=c
  t=a[t]-1
  c+=1
l=v[t]
c-=l
print(v.index(min(k,l+(k-l)%c))+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = 1
c = 0
d = {}
t = []
# 順番をdictで持つ
while not s in d.keys():
    d[s] = c
    t.append(s)
    c += 1
    s = a[s-1]
l = c - d[s]
m = len(d.keys()) - l
if m >= k:
    print(t[k])
else:
    print(t[(k - m) % l + m])
n,k = map(int,input().split())
a = list(map(int,input().split()))
dic = {}
for key, a in zip(range(1, n+1), a):
    dic[key] = a
 
log = [-1]*(n+1)
next = dic[1]
step = 1
log[next] = step
k -= 1
while k:
    next = dic[next]
    step += 1
    k -= 1
    if log[next] != -1:
        k %= log[next] - step
    else:
        log[next] = step
print(next)
N, K = map(int, input().split())
*A, = map(int, input().split())
s = 0
l = 0
n = 0
h = [0]*N
while True:
    h[s] = l
    s = A[s]-1
    l += 1
    if h[s]:
        n = l-h[s]
        break

if K < h[s]:
    s = 0
else:
    K -= h[s]
    K = K % n

while K > 0:
    s = A[s]-1
    K = K-1
print(s+1)
N, K, *A = map(int, open(0).read().split())

# Destination
t = [[-1] * N for _ in range(61)]
t[0] = [v - 1 for v in A]
for k in range(60):
    for i in range(N):
        t[k + 1][i] = t[k][t[k][i]]

u = 0
for k in range(61):
    if K >> k & 1:
        u = t[k][u]
        
print(u + 1)
N,K=map(int,input().split())
A=list(map(int,input().split()))

F=[-1]*N
l=0
prev=0
while True:
  if F[prev]!=-1:
    break
  
  if l==K:
    print(prev+1);exit()
  F[prev]=l
  l+=1
  prev=A[prev]-1
  #print(l,prev)
l = (K-F[prev])%(l-F[prev])
#print(l)
for i in range(l):
  prev=A[prev]-1
print(prev+1)
n, k = map(int, input().split())
A = list(map(int, input().split()))
L = [0] * n
L[0] = 1
i = A[0] - 1
j = 1
while j < k:
    if L[i] < 2:
        L[i] += 1
    else:
        break
    i = A[i] - 1
    j += 1
if j == k:
    print(i + 1)
else:
    loop = L.count(2)
    k = (k - L.count(1)) % loop
    while k > 0:
        i = A[i] - 1
        k -= 1
    print(i + 1)
import sys

n, k = map(int, input().split())
ara = [0]
ara += list( map(int, input().split()) )

now = 1
vis = []
taken = [0]*(n+1)

while taken[now] == 0:
	vis.append(now)
	taken[now] = 1
	now = ara[now]

cycleLen = len(vis) - vis.index(now)

if k < vis.index(now):
	print(vis[k])
	sys.exit()

k -= vis.index(now)

k %= cycleLen

print(vis[k + vis.index(now) ])