peterverebics/CodeNet_Python_118 huggingface dataset:
# coding: utf-8
# Your code here!
n,k=map(int,input().split())

A=list(map(int,input().split()))
L=[1]
now=1
for _ in range(n):
        now=A[now-1]
        if now  in L:
                index=L.index(now)
                break
        L.append(now)
if k<=index:
        print(L[k+1])
        exit()
R=(k-index)%(len(L)-index)

print(L[index:][R])
n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
route_list = []
current_index = n
while current_index not in route_list:
    route_list.append(current_index)
    value = a[current_index-1]
    current_index = value
cycle_route = route_list[route_list.index(current_index):]
temp = len(route_list[:route_list.index(current_index)])
cycle_route[(k - temp) % len(cycle_route)-1]
import sys

n, k, *aaa = map(int, sys.stdin.buffer.read().split())
shortest = [-1] * n
v = 0
d = 0
while shortest[v] == -1:
    shortest[v] = d
    v = aaa[v] - 1
    d += 1
    if d == k:
        print(v + 1)
        exit()

first = shortest[v]
loop = d - first
remaining = (k - first) % loop
print(v, first, loop, remaining + first, shortest)
ans = shortest.index(remaining + first) + 1
print(ans)
def main():
    N,K = [int(x) for x in input().split()]
    Al = [int(x) for x in input().split()]
    current = 1
    steps = []
    initial_step = 0
    while current not in steps:
        steps.append(current)
        current = Al[current-1]
        initial_step += 1
    loop_step = len(steps) - steps.index(current)
    if len(steps) == initial_step:
        initial_step = 0
    print((K-initial_step)%loop_step)

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

MyList = [int(n) for n in MyList]

ANS = 0
visted = []

def Myfunc(A):
    return MyList[A] - 1

for i in range(K):
    if ANS in visted:
        beANS = K % len(visted)
        ANS = visted[beANS]
        print(ANS)
    ANS = Myfunc(ANS)
    visted.append(ANS)
    if i +1 == K:
        print(ANS+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 = map(int, input().split())
a = list(map(int, input().split()))
now = 1
for i in range(min(k, int(2 * n + (n-1)))):
    now = a[now - 1]
print(now)
n,k=map(int,input().split())
a=list(map(int,input().split()))
d=[]
t=1
for i in range(1,n+1):
    d.append(t)
    if(a[t-1] in d):
        break
    t=a[t-1] 
idx=d.index(a[t-1])
print(d)
d=d[idx:]
print(d[(k-i)%len(d)])
n,m=map(int,input().split())
l=list(map(int,input().split()))
cycle=[1]
i=1
while True:
  i=l[i-1]
  if i in cycle:
    cycle=cycle[cycle.index(i):]
    break
  else:
    cycle+=[i]
print(cycle[(m-i)%len(cycle)])
from collections import deque

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


def check(x):
    return A[x - 1]


x = 1
lis = deque([])
for i in range(n + 2):
    if check(x) in lis:
        break
    else:
        lis.appendleft(check(x))
        x = check(x)

num = lis.index(check(x)) + 1
k = (k - (i - num)) % num
print(lis[- k - (i - num)])
N,k=map(int,input().split())
a=list(map(int,input().split()))
for i in range(N):
    a[i]-=1
if a[0]==0:print(1);exit()
c=dict()
visited=[False]*N
visited[0]=True
c[0]=0
cnt=0
dap=0
n=0
b=[0]
while True:
    n=a[n]
    if visited[n]:
        dap=c[n] 
        break
    visited[n]=True
    b.append(n)
    cnt+=1
    c[n]=cnt
l=len(c)
print(b[(k-dap)%(l-dap)+dap]+1)
n,k =map(int,input().split())
a = list(map(int,input().split()))
l = 0  
count = 0
 
for i in range(10000000) :
    l = a[l]-1 
    count = +1
    if l == 0 :
        x = k%count
        for i in range(x) :
            l = a[l]-1
            
        print(l+1)
        exit()
N, K = map(int,input().split())
A = list(map(int,input().split()))
A = [0] + A
L = []
S = set()
i = 1
cnt = 0
while not i in S or cnt == K:
    S.add(i)
    L.append(i)
    cnt += 1
    i = A[i]

j = L.index(i)
ans = L[(j + (K - j) % (cnt - j)) % cnt]

print(ans)
N,K = map(int,input().split())
A = list(map(int,input().split()))
town = 1
record = [1]
for i in range(K):
    town = A[town-1]
    if town in record:
        break
    record.append(town)
    
roop = len(record)
amari = K%roop


    
print(record[amari])
import sys
input = sys.stdin.readline


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    li = [0]
    nt = 0
    for i in range(n):
        nt = a[nt]-1
        if nt == 0:
            break
        li.append(nt)

    x = k % len(li)
    # print(x)
    print(li[x]+1)


if __name__ == '__main__':
    main()
N,K=map(int,input().split())
A=list(map(int,input().split()))
A=[i-1 for i in A]
c=A[A[0]]
if c==0:
  print(c+1)
  exit()
s=set()
s.add(0)
s.add(A[0])
while True:
  if c in s:
    break
  s.add(c)
  c=A[c]
K%=len(s)
ans=A[0]
for i in range(K):
  ans=A[ans]
print(ans)
n, k = map(int, input().split())
li = list(map(int, input().split()))
 
check = [0] * (n+10)
res = []
next = 0
 
for i in range(k):
    if check[next] == 0:
        check[next] = 1
        next = li[next]-1
    elif check[next] == 1:
        check[next] += 1
        res.append(next+1)
        next = li[next]-1
    else:
        cnt = len(res)
        k -= check.count(1)
        amari = k%cnt
        print(res[amari])
        break
(N,K) = input().split()
N = int(N)
K = int(K)
tel = map(int,input().split())
tel = list(tel)
now = 1
cou = [0]*N
cou[0] = cou[0]+1
print (cou)
for i in range (K):
    now = tel[now-1]
print(now)
N,K = map(int,input().split())
A = list(map(int,input().split()))
flag = 0
position = [0]
while flag == 0:
  temp = position[-1]
  if A[temp] -1 in position:
    break
  position.append(A[temp]-1)
x = K%len(position)
print(position[x]+1)
n,k= map(int,input().split())

t = list(map(int,input().split()))
p = 1
roop = 0
fp = []
fp.append(p)
for i in range(n):
    p = t[p-1]
    if p in fp:
        roop = i+1 - fp.index(p)
        break
    else:
        fp.append(p)
ans = fp[fp.index(p):]
print(ans[(k-fp.index(p))%roop])
n, k = map(int, input().split())
a = list(map(int, input().split()))
p = 1
t = [1]
u = set()
u.add(1)
for i in range(n):
  p = a[p - 1]
  if p not in u:
    t.append(p)
    u.add(p)
  else:
    break
c = t.index(p)
r = len(t) - c
s = len(t) - r
ans = t[(k - s + 1) % r + s - 1]
print(ans)
n,k,*a=map(int,open(0).read().split())
b=[1]
s_b={1}
for i in range(n):
  tmp=a[b[-1]-1]
  if tmp in s_b:
    idx=b.index(tmp)
    break
  else:
    b.append(tmp)
    s_b|={tmp}
mod=len(b[idx:])
d=len(b[:idx])
print(b[d+(k-d)%mod])
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[K + L.index(A[now - 1])])
if len(L) - L.index(A[now - 1]) == 1:
    print("hoge")
N, K = map(int, input().split())
A = list(map(int, input().split()))

roop_num = 1
city = 0
first_city = 0

while True:
    next_city = A[city] - 1
    if next_city == first_city:
        break
    if roop_num == K:
        print(city + 1)
        exit()
    city = next_city
    roop_num += 1

rem_num = K % roop_num
city = 0
num = 0

while True:
    next_city = A[city] - 1
    if num == rem_num:
        print('answer', city + 1)
        exit()
    city = next_city
    num += 1
n,k = map(int, input().split())
a = [int(s)-1 for s in input().split()]

start = 0
for _ in range(k):
    print(start)
    start = a[start]
    print(start)

print(start + 1)
import sys
a,b=map(int,input().split())
b=b+1
l=[0]*(a)
List=list(map(int,input().split()))
i=0
j=1
while l[i]==0 and j<b:
  l[i]+=j
  j+=1
  i=List[i]-1
if j==b:
  print(List[i])
  sys.exit()
roop=j-l[i]
c=(b-j)%roop
for y in range(len(l)):
  if l[y] ==c+l[i]:
    print(y+1)
    sys.exit()
N,K=map(int,input().split())
A=list(map(int,input().split()))
now=1
count=0
a=0
l=[1]
b=0
c=0
while a==0and count<K:
    now=A[now-1]
    count+=1
    if now in l:
        a=1
    else:
        l.append(now)
        
print(l)
b=l.index(now)
c=count-b
if count==K:
    print(now)
else:
    n= ((K-b)%c)
    ncount=0
    while ncount <n:
        now=A[now-1]
        ncount+=1
    print(now)
n,k = map(int,input().split())
a = list(map(int,input().split()))
res= []
t = a[0]
while t not in res:
    res.append(t)
    t = a[t-1]
b = res.index(t)
c = (k-b)%(len(res)-b)
res_0 = res[b:]
print(res_0[c-1])
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = [1]
x = 1

for i in range(n):
  x = a[x-1]
  if not(x in b):
    b.append(x)
  else:
    c = b.index(x)
    break

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

l = []
move = 1
l.append(move)
ind = 0
flag = [False]*(N+1)
flag[1] = True
for i in range(N):
    move = A[move-1]
    if flag[move]:
        # ind = l.index(move)
        # l = l[ind:]
        break
    flag[move] = True
    l.append(move)

# print("ind:{}".format(ind))
# print("l:{}".format(l))
ind = l.index(move)
# l = l[ind:]
rest = (K-ind) % (len(l) - ind) + ind

# print(rest)
print(l[rest])
n, k = map(int, input().split())
A = list(map(int, input().split()))

ex = [1]
lo = 1
for i in range(k):
    lo = A[lo - 1]
    if lo in ex:
        a = ex.index(lo)
        x = len(ex) - a
        b = (k - len(ex)) % x
        print(ex[a + b])
        break
    ex.append(lo)
n, k = map(int, input().split())
a = list(map(int, input().split()))

route =[1]
cur = 1
for i in range(k):
    goto = a[cur - 1]
    if goto in route:
        pre_goto = route.index(goto)
        dif = len(route) - pre_goto
        rest_step = (k - pre_goto) % dif
        print(a[pre_goto + rest_step])
        break
    route.append(goto)
    cur = goto
else:
    print(a[k])
import sys


n,k=map(int,input().split())

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

log=[]

now=0

cnt=0


while(1):
  if len(log)>1:
    if now in log:
      f=log.index(now)
      z=k-cnt #要調整

      loop=cnt

      b=z%(loop-f)

      #print(cnt,f,z,loop,b)
      print(log[f+b]+1)
      #print(log)
      sys.exit()


  log.append(now)
  now=a[now]-1
  cnt+=1
N,K=map(int,input().split())
A = list(map(int,input().split()))
B = [0]*N
loop=0
now=0
C=[]
n=0
while(True):
    if B[now]==0:
        B[now]=1
        C.append(now+1)
        loop+=1
        now=A[now]-1
    else:
        n=A[now]
        break
if loop<=K:
    res=(K-C.index(n)+1)%(loop-C.index(n)+1)
    D=C[C.index(n)-1:]
    print(D[res%len(D)])
else:
    print(C[K])
N, K = map(int, input().split())
A = list(map(int, input().split()))

chk = [0]*(N+1)
town = 1
rt = []
roop = 0

while True:
    if chk[town] == 1:
        break
    rt.append(town)
    chk[town] = 1
    town = A[town-1]

if K <= rt.index(town):
    print(rt[K-1])
else:
    roop = (K-rt.index(town)) % (len(rt)-rt.index(town))
    print(rt[rt.index(town)+roop])
n,k = map(int,input().split())
a = [int(x) for x in input().split()]
num = 0
for _ in range(k):
    num = a[num-1]
print(a[num-1])
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))

def has_duplicates(seq):
    return len(seq) != len(set(seq))

visited = [0]
flag = False
loopStart = 0
loopCycle = 0

for i in range(n):
    visited.append(a[visited[-1]]-1)
    if has_duplicates(visited):
        flag = True
        loopStart = visited.index(visited[-1])
        loopCycle = len(visited) - 1 - loopStart
        break
    if i == k - 1:
        print(visited[i+1]+1)
        break

if flag:
    print(visited[k % loopCycle]+1)
# coding: utf-8
# Your code here!
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = []
check = {}
key = -1
p = 0
while True:
    ans.append(p)
    check[p] = 0
    p = a[p]-1
    if p in check:
        key = ans.index(p)
        break
r = ans[key:]
mod = (k-key)%len(r)
print(r[mod])
N,K=map(int,input().split())
A=[0]+list(map(int,input().split()))

counter=1
now=1
gone_set={1}
gone_list=[1]
place=1
while counter<=N:
    next=A[now]
    if next in gone_set:
        start=gone_list.index(next)
        loop=len(gone_list)-start
        last=(K-start)%loop
        place=gone_list[start+last]
        break
    else:
        gone_set.add(next)
        gone_list.append(next)
        now=next
        counter+=1

print(place)
n, k = map(int, input().split())

a = list(map(int, input().split()))
done = [0]*n
stay = 1

for i in range(1, k+1):
    if done[stay-1] != 0:
        break
    done[stay-1] = i
    stay = a[stay-1]
    #print(stay)

nokori = k + 1 - i
loop = i - done[stay-1]

for i in range(nokori%loop):
    stay = a[stay-1]

print(stay)
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(N):
    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(queue[now_town])
    sys.exit()

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

print(list(queue.keys())[(K - before_len) % roop + before_len - 1])
N,K = map(int,input().split())
A = list(map(int,input().split()))
n = 1
B = [0]*N
for i in range(2*N+1):
    B[n-1] = i+1
    n = A[n-1]
    if B[n-1] != 0:
        t = i+2
        break
c = t-B[n-1]
s = (K+1-B[n-1])%c
for i in range(s):
    n = A[n-1]
print(n)
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(str(nex))
        lo = len(c)-whe
        break
    b[nex] = 1
    c += str(nex)
if whe+1 >= k:
    print(c[k])
else:
    k_ = k - whe
    print(c[whe:][k_%lo])
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))

d = a[0]
b = [d]

for i in range(k):
    dn = a[d-1]
    if b[0]!=dn:
        d = dn
        b.append(d)
    else:
        break
        
print(b[k%len(b)])
N, K = map(int, input().split())
A = input().split()
A = [int(a) for a in A]
S = []
a = 1
number = 0
numbers=[-1 for _ in range(N)]
for _ in range(N + 1):
    b = A[a - 1]
    if numbers[b-1] != -1:
      c=numbers[b-1]
      break
    numbers[b-1]=number
    number += 1
    S.append(b)
    a = b
T = S[c:number]
if K <= number:
  S.insert(0,1)
  print(S[K - 1])
else:
  z = K - c - 1
  y = z % (number - c)
  print(T[y])
N, K = map(int, input().split())

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

T = []

a = A[0]

while a not in T:
    T.append(a)
    a = A[a-1]

T_index = T.index(a)
p = (K-T_index)%(len(T)-T_index)
if K <= T_index:
    print(T[T_index-1])
else:
    if p == 0:
        print(T[-1])
    else:
        print(T[T_index + p - 1])
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([k,v.index(l+(k-l)%(r-l))][k>l]+1)
N,K=map(int,input().split())
L=list(map(int,input().split()))
cnt=0
i=0
S=[]
while not(L[i] in S):
    S.append(i+1)

    i=L[i]-1
    cnt=cnt+1
S.append(i+1)

print(S[K%(cnt+1)])
n,k = map(int, input().split())
a=list(map(int,list(input().split())))

exist = [0]

cnt = 1
while(True):
    if a[exist[-1]-1] in exist:
        idx = exist.index(a[exist[-1]-1])
        exist.append(a[exist[-1]-1])
        break
    exist.append(a[exist[-1]-1])
    cnt += 1

k = k-idx

d = k % (cnt-idx)
print(a[idx+d])
def main():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    import math
    r = math.ceil(math.log2(k))
    idx = 0
    a = [i-1 for i in range(n)]
    for i in range(r):
        if (k>>i)&1 == 1:
            idx = a[idx]
        a = [a[a[j]] for j in range(n)]
    print(idx+1)    

if __name__ == '__main__':
    main()
N,K = map(int, input().split())
A = [*map(lambda x:int(x)-1, input().split())]

index = [-1]*N
pre, path_cnt = 0, 0
for i in range(N):
    if index[pre] >= 0: break
    else:
        index[pre] = i
        path_cnt += 1
    pre = A[pre]

loops = path_cnt - index[pre]
one = index[pre]
k = max(0, K-one) % loops

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

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

if cy == -1:
    print(d[k-1])
    exit()

noncyi = d.index(cy)
cyi = len(d) - noncyi
if k < noncyi:
    print(d[k-1])
else:
    k -= noncyi
    print(d[noncyi + k%cyi]+1)
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]==True:
        loop = i+1-path.index(at)
        break
    else:
        visited[at]=True
        path.append(at)

print(path[(K-path.index(at))%loop+path.index(at)]+1)
n, k = map(int,input().split())
town = list(map(int,input().split()))
cnt = 1
step = 1
now = town[0]
via = [1]
while((now not in via)&(step<=n)):
  via.append(now)
  now = town[now -1]
  cnt += 1
  step += 1

loop = via[via.index(now):]
print(loop[(k - cnt)%len(loop)])
n, k = map(int, input().split())
a = list(map(int, input().split()))

pos = -1
seen = [False] * n
last = [0] * n
hist = []
cycle = []
i = 0
cnt = 0
while(seen[i] == False):
    seen[i] = True
    last[i] = cnt
    cnt += 1
    hist.append(i)
    ne = a[i]-1
    i = ne
    if (seen[i] == True):
        pos = i

preCircle = len(hist.copy())

cycle = hist[last[pos]:]

preCircle = last[pos]

if (k < preCircle):
    print(hist[-k-1] + 1)
else:
    k -= preCircle
    ans = k % len(cycle)
    print(cycle[ans] + 1)
N,K = map(int, input().split())
A = list(map(int, input().split()))

limit = K
now = 1
log = []
count = 1

while limit >= 0 and now not in log:
    if 1 not in log:
        log.append(1)
    #print("通過1",now,limit,log)
    now = A[now - 1]
    log.append(now)
    limit -= 1
    count += 1

if limit >= 0:
    #print("通過2",now,limit,log)
    log2 = log[log.index(now):]
    limit = limit % len(log2)

while limit >= 0:
    #print("通過3",now)
    now = A[now - 1]
    limit -= 1

print(now)
n, k = map(int, input().split())
A = list(map(int, input().split()))

if k > n:
    idx = (k%n)-1
else:
    idx = k
print(A[A[idx] - 1])
n,k = map(int, input().split())
a = [0] + list(map(int, input().split()))
r = [1]
k += 1
now = 1
for i in range(n):
   now = a[now]
   if now in r: 
     break
   r.append(now)
I = r.index(now)
if I != 0: 
  k -= I
  r = r[I:]
print(r[-1] if k%len(r)==0 else r[k%len(r)-1])
n,k=map(int,input().split())
ai = list(input().split())
i = (k%n)-1
a=int(ai[i])-1
print(ai[a])
n,k = map(int,input().split())
A = list(map(int,input().split()))
dp = [0] * (n+1)
B = []
f = 1
c = 1
ans = n
bal = 0
for i in range(n):
    if dp[f-1]:
        ans = c-dp[f-1]
        bal = dp[f-1]
        break
    else:
        dp[f-1]+=c
        B.append(f)
        f = A[f-1]
        c+=1
bal-=1
k-= bal
# print(B,bal)
k = k%ans
print(B[k+bal])
import math

n, k = map(int, input().split(" "))
li = list(map(int, input().split(" ")))

crnt = 1
st = set()
wl = []
key = crnt
cy = 0
mg = 0


for i in range(n):
    if crnt in st:
        key = crnt
        cy = i - wl.index(crnt)
        mg = wl.index(crnt)
        break
    st.add(crnt)
    wl.append(crnt)
    crnt = li[crnt - 1]

k -= mg
if cy != 0:
    k %= cy
crnt = key
for i in range(k):
    crnt = li[crnt - 1]

print(crnt)
n, k = map(int, input().split())
a = list(map(int, input().split()))
l = []
ne = a[0] - 1
for i in range(2 * n + 1000):
    ne = a[ne] - 1
    if ne in l:
        break
    l.append(ne)
print(l[(k + 1) % len(l)] + 1)
n,k=map(int,input().split())
a=list(map(int,input().split()))
for i in range(n):
    a[i]-=1
#print(a)
    
b=0
c=[-1]*n
c[0]=0
for i in range(n):
#   print(b,a[b])
   b=a[b]
   if c[b]>=0:
#       print("i:",i)
       break
   else:
       c[b]=i+1

ki=c[b]+1
kc=(i+1)-c[b]
k2=(k-ki)%kc+ki
#print("ki:",ki,"kc:",kc,"k2:",k2)
b=0
for i in range(k2):
    b=a[b]
print(b+1)
n, k = map(int, input().split())
towns = list(map(int, input().split()))
place_count = [0] * (n + 1)
next_place = 1
teleport_count = 0
for i in range(n * 2):
  place_count[next_place] += 1
  next_place = towns[next_place - 1]
one_move_place = place_count.count(1)
for place in place_count:
  if place > 1:
    teleport_count += 1
next_place = 1
for _ in range(one_move_place + (k - one_move_place) % teleport_count):
  next_place = towns[next_place - 1]
print(next_place)
n, k = map(int, input().split())
a = list(map(int, input().split()))
m = [-1]*n
t = 0
i = 0
while True:
  if m[t] != -1:
    r = i - m[t]
    break
  m[t] = i
  t = a[t]-1
  i += 1
c = r
while True:
  if c > m[t]:
    break
  c += c
b = k%r + c
b 
t = 0
for i in range(b):
  t = a[t]-1
print(t+1)
n,k=map(int,input().split())
a=list(map(int, input().split()))
iti=1
count=0
sokuseki=[0]*n
while True:
    iti=a[iti-1]
    count+=1
    if sokuseki[iti-1]:
        m=((k-count)//(count-sokuseki[iti-1]))
        k=k-count-m*(count-sokuseki[iti-1])
        count=0
    sokuseki[iti-1]+=count
    if count==k:
        print(iti)
        exit()
N,K = (int(x) for x in input().split())
A = list(map(int, input().split()))
route = []
temp = 0
for i in range(N):
    route.append(temp)
    temp = A[temp]-1
    if temp == 0:
        break
if K >= len(route):
    print(route[K%len(route)]+1)
else:
    print(route[K]+1)
import sys
N, K = map(int, input().split())
A = list(map(int, sys.stdin.readline().split()))
visited = [0] * N
visited[0] = 1
ikisaki = 0
c = 1
while c < N:
    ikisaki = A[ikisaki] - 1
    c += 1
    if visited[ikisaki]:
        loop = c - visited[ikisaki]
        c = N
    else:
        visited[ikisaki] = c

amari = (K - (visited[ikisaki] - 1)) % loop
new = [i - visited[ikisaki] for i in visited]
print(new.index(amari) + 1)
from collections import defaultdict
n,k=map(int, input().split())
A=[int(i)-1 for i in input().split()]
now=0
loop=[]
dd = defaultdict(int)

while k!=0:
    k-=1
    now = A[now]
    if dd[now]>0:
        break
    else:
        dd[now]+=1
    if k==0:
        print(now)
        exit()
    loop.append(now)
loop = loop[loop.index(now):]
loop.append(now)
t = len(loop)-1
a=(k%t)
while a>0:
    a-=1
    now = A[now]
print(now+1)
n,k= map(int,input().split())
A  = [0]
A += list(map(int,input().split()))
B = [-1 for _ in range(n+1)]
B[1] = 0
now =1
cnt = 0
cic = 0
for i in range(1,n+1):
    now = A[now]
    cnt +=1
    if B[now] ==-1:
        B[now] = cnt
    else:
        cic = cnt - B[now]
        road = B[now] 
        break

if k <= road:
    print(B.index(k)+1) 
else:
    print(B.index(((k-road)%cic+road)))
paramN, paramK = list(map(lambda x: int(x), input().split(' ')))
paramA = list(map(lambda x: int(x), input().split(' ')))

route = [1]
for i in range(paramN):
    route.append(paramA[route[len(route) - 1] - 1])

loop_start = 0
for i, r in enumerate(route):
    if r in route[:i]:
        loop_start = i
        break

x = route[loop_start]
loop_start_0 = route.index(x)

y = paramK - loop_start_0
z = y % (loop_start - loop_start_0)
print(route[loop_start_0 + z])
N, M = map(int, input().split())
P = [i -1 for i in map(int,input().split())]
ss = set([])
point = 0
for i in range(N):
    if point in ss:
        break
    ss.add(point)
    point = P[point]
S = [0]
points = 0
for i in range(len(ss)-1):
    points = P[points]
    S.append(points)
loop = S[S.index(points):]
ll = len(S)
l = len(loop)
if ll > M:
    print(S[M]+1)
elif ll == M:
    print(points)
else:
    print(loop[(M-ll)%l]+1)
n,k=map(int,input().split())
A = list(map(int,input().split()))

C=[0]*n
C[0]=1
c=0
loopc=0
i=0
while i<n :
    newi = A[i]-1
    C[newi]+=1
    if C[newi]>=3 :
        rs=newi
        break
    elif C[newi]==2 :
        loopc+=1
    i=newi
    c+=1

mod = (k-(c-loopc))%(loopc+1)
# print( C, c, loopc, mod)

i=rs
while mod>0 :
    newi = A[i]-1
    i=newi
    mod-=1

print( i+1 )