peterverebics/CodeNet_Python_118 huggingface dataset:
import sys
input = sys.stdin.readline

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

cnt, town = 0, 1
l, s = [], set()
while True:
    town = a[town - 1]
    cnt += 1
    if cnt == k:
        print(town)
        exit()
    if town in s:
        loop_start = town
        break
    l.append(town)
    s.add(town)


loop_cnt = l.index(loop_start)
loop = l[loop_cnt:]
loop_len = len(loop)
k -= loop_cnt
print(l[loop_cnt:][k%loop_len-1])
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):
        h = to[i][j]
        to[i][j+1] = to[h][j]

v = 0
for i in reversed(range(D)):
    x = 1 << i
    if x <= K:
        v = to[v][i]
        K -= x

print(v + 1)
n,k = map(int, input().split())
a = list(map(lambda x : int(x) -1,  map(int, input().split())))

s = {0}
loop = [0]

while a[loop[-1]] not in s:
    loop.append(a[loop[-1]])
    s.add(loop[-1])

    
idx = loop.index(a[loop[-1]])

if k < len(loop):
    print(loop[k]+1)
else:
    print(loop[(k - idx)% (len(loop) - idx) + idx] + 1)
n,k=map(int,input().split())
a=list(map(int,input().split()))

s=[]
ordn=[]
for i in range(n+1):
    ordn.append(-1)

v=1
while(ordn[v] == -1):
    ordn[v]=len(s)
    s.append(v)
    v=a[v-1]

c=len(s)-ordn[v]
l=ordn[v]

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

visited = [0] * n
i = 0
now = 0
while k > i:
    if visited[now] == 0:
        i += 1
        visited[now] = i
        now = lst[now] 
    else:
        break

lp = (i + 1) - visited[now]
st = i - lp
k_bk = k - st
k_bk = k_bk // lp
lft = k - (k_bk * lp + st)

if lft == 0:
    print(now+1)
else:
    for _ in range(lft):
        now = lst[now]
    print(now+1)
n, k = map(int, input().split())
aaa = list(map(lambda x: int(x) - 1, input().split()))
visited = [0] * n
next_town = 0
order = []
while True:
    if visited[next_town]:
        break
    order.append(next_town)
    visited[next_town] = 1
    next_town = aaa[next_town]
first_loop = len(order)
offset = order.index(next_town)
second_loop = first_loop - offset
if k < first_loop:
    print(order[k] + 1)
else:
    print(order[offset + (k - first_loop) % second_loop] + 1)
n, k = [int(_) for _ in input().split()]
a = [int(_) for _ in input().split()]

t = 0
l = []
c = 0
d = {}
while True:
    d[t+1] = c
    c += 1
    l.append(t+1)
    t = a[t] - 1
    # if t == 0:
    #     break
    if t + 1 in d:
        s = l[d[t+1]:]
        q = d[t+1]
        break
# print(l)
# print(s)
# print(d)
# print(q)

if k <= q:
    print(l[k])
else:
    print(s[(k-q) % len(s)])
n, k = map(int, input().split())
a = list(map(lambda ai: int(ai) - 1, input().split()))

visited = [-1] * len(a)

now = 0
visited[0] = 0
while k > 0:
    k -= 1
    old = now
    now = a[now]
    if visited[now] != -1:
        p = visited[old] + 1 - visited[now]
        k %= p
        visited = [-1] * len(a)
    else:
        visited[now] = visited[old] + 1
print(now + 1)
N, K = map(int, input().split())
a = list(map(int, input().split()))
l = [1]
for i in range(N):
  l.append(a[l[i]-1])
p = l.index(l[N])
if K<=N:
  print(l[K])
else:
  x = (K-N+1)%(N-p)
  if x ==0:
    print(l[N-1])
  else:
    print(l[x+p-1])
n, k = map(int, input().split())
a = [-1] + list(map(int, input().split()))

visited = [0] * (1 + n)
curr = 1
while k:
    if visited[curr]:
        break
    else:
        visited[curr] = k
        curr = a[curr]
        k -= 1
        if k == 0:
            print(curr)
            exit()
repeat = visited[curr] - k
k = k % repeat
while k:
    curr = a[curr]
    k -= 1
print(curr)
n, k = [int(x) for x in input().split()]
a_list = [int(x) - 1 for x in input().split()]

ci = 0
temp_dict = {0: 0}
temp_list = [0]
for i in range(1, k + 1):
    temp = a_list[ci]
    if temp in temp_dict:
        ci = temp_list[(k - i) % (i - temp_dict[temp]) + temp_dict[temp]]
        break
    temp_dict[temp] = i
    temp_list.append(temp)
    ci = temp
print(ci + 1)
N,K=map(int,input().split())
A=list(map(int,input().split()))

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

count=0
cycle[0]=count
town=0
mod=0
while True:
    count+=1
    K-=1
    town=A[town]
    if K==0:
        mod=1
        break
    if cycle[town]==-1:
        cycle[town]=count
    else:
        mod=count-cycle[town]
        break

K%=mod
while K>0:
    town=A[town]
    K-=1

print(town+1)
n, k = [int(i) for i in input().split()]
a = [int(i)-1 for i in input().split()]
x = list(range(n))

def calc(a, b):
    c = [0] * n
    for i in range(n):
        c[i] = b[a[i]]
    return c

while k:
    if k & 1 == 1:
        x = calc(x, a)
    k //= 2
    a = calc(a, a)

print(x[0]+1)
N, K = map(int, input().split())
A = list(map(int, input().split()))

current = 1
path = [1]
already = [False] * len(A)

while K > 0:
    if already[current - 1]:
        loop = path[path.index(A[current - 1]):]
        current = loop[(K - 1) % len(loop)]
        break

    path += [A[current - 1]]
    already[current - 1] = True
    current = A[current - 1]
    K -= 1

print(current)
from collections import Counter
N, K = map(int,input().split())
tele = list(map(int, input().split()))

#A1から順にワープした時の経由順
now = 0
goal,loop,visit = [],[],[0]*N

while visit[now]!=2:

  if visit[now]==0:
    goal.append(now)
  else:
    loop.append(now)
    
  visit[now] += 1
  now = tele[now]-1

if len(goal)>K:
  print(goal[K]+1)
else:
  amari = len(goal)-len(loop)
  print(loop[(K-amari)%len(loop)]+1)
n, k = map(int, input().split())

a = [0] + list(map(int,input().split()))

explored = [0] * (n+1)
t = 1
cycle = 0
while True:
    if explored[t] == 2:
        k %= cycle
        if k == 0:
            k = cycle
        explored[t] += 1
    elif explored[t] == 1:
        explored[t] += 1
        cycle += 1
    elif explored[t] == 0:
        explored[t] += 1

    k -= 1
    if k == 0:
        break
    else:
        t = a[t]
    

print(a[t])
N, K = map(int, input().split())
a = list(map(int, input().split()))
chk = [0] * N
start = -1
ido = [0]
chk[0] = 1
index = 0
cnt = 0

while True:
    index = a[index] - 1
    if chk[index] == 0:
        chk[index] += 1
        cnt += 1
        ido.append(index)
    else:
        start = index
        break

if cnt >= K:
    print(ido[K] + 1)
    exit()
tmp = ido.index(start)
ido = ido[tmp:]
K -= tmp

print(ido[K % len(ido)] + 1)
N,K=[int(x) for x in input().rstrip().split()]
A=[int(x) for x in input().rstrip().split()]

stack=[]
done=[0 for i in range(2*(10**5))]
done[0]=0
now=0
dou=[]

while(done[now]<2):
  
  if done[now]==0:
    stack.append(now)
  else:
    dou.append(now)
  done[now]+=1
  now=A[now]-1

if K<len(stack):
  print(stack[K]+1)
else:
  print(dou[(K-len(stack)-len(dou))%len(dou)]+1)
n,k = map(int,input().split())
a = list(map(int,input().split()))
now = 1
ss = set([1])
sw = 0
x = 0
for i in range(k):
    now = a[now-1]
    ss.add(now)
    if len(ss) != i+2:
        b = now
        x = i+1
        sw = 1
        break
y = 0
now2 = 1
for j in range(x-1):
    if now2 == b:
        y = j
        break
    now2 = a[now2-1]
if sw == 1:
    L = (k-y)%(x-y)
    for i in range(L):
        now = a[now-1]
print(now)
N,K = map(int,input().split())
A = [0] + list(map(int,input().split()))

B = [1] + [A[1]]
S = set(B)
i = 1
while i+1 == len(S):
    B.append(A[B[i]])
    S.add(A[B[i]])
    i += 1

for i in range(len(B)-1):
    if B[i] == B[-1]:
        a = i
        break

if K <= a:
    print(B[K])
else:
    print(B[a+ ((K-a) % (len(B)-a-1))])
n,k=map(int,input().split())
a,c=list(map(lambda x: int(x)-1,input().split())),0
for _ in range(60):
  if k%2:
    c=a[c]
  k//=2
  a=[a[a[i]]for i in range(n)]
print(c+1)
#!/usr/bin/python3
import sys
input = lambda: sys.stdin.readline().strip()
n, k = [int(x) for x in input().split()]
a = [[0, *(int(x) for x in input().split())] if p == 0 else [0 for i in range(n + 1)] for p in range(60)]
for p in range(1, 60):
    for i in range(1, n + 1):
        a[p][i] = a[p - 1][a[p - 1][i]]
ans = 1
for p in range(60):
    if k & (1 << p):
        ans = a[p][ans]
print(ans)
N, K = map(int, input().split())
A = list(map(int, input().split()))

visit = [-1]*(N+1)
history = []
v, cnt = 1, 0
while visit[v] == -1:
  visit[v] = cnt
  history.append(v)
  v = A[v-1]
  cnt += 1
period = cnt - visit[v]
l = visit[v]

if K < l:
  print(history[K])
else:
  K -= l
  K %= period
  print(history[l+K])
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

n,k=nii()
a=lnii()

x=0
x_list=[1]
x_table=[0 for i in range(n+1)]
x_table[1]=1

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

  if x_table[nx]!=0:
    inx=x_list.index(nx)
    loop=x_list[inx:]
    zan=k-i

    q=zan%len(loop)

    nx=loop[q-1]
    break

  else:
    x=nx-1
    x_list.append(nx)
    x_table[nx]+=1

print(nx)
N,K=map(int,input().split())
A=list(map(int,input().split()))
l=[-1]*N
l[0]=0
i=1
t=0
while 1:
    t=A[t]-1
    if l[t]!=-1:
        s=i-l[t]
        break
    l[t]=i
    i+=1
if K<i:
    print(l.index(K)+1)
else:
    k=K%s
    if (i//s)*s+k in l:
        print(l.index((i//s)*s+k)+1)
    else:
        print(l.index((i//s-1)*s+k)+1)
n, k = map(int, input().split())
A = list(map(int, input().split()))
route = [1]
dest = A[0]
check = {i: False for i in range(n)}
while not check[dest - 1]:
    check[dest - 1] = True
    route.append(dest)
    dest = A[dest - 1]
ind = route.index(dest)
cycle = route[ind:]
if k >= ind:
    ans = (k - ind) % len(cycle)
    print(cycle[ans])
else:
    print(route[k])
n, k = map(int, input().split())
a = tuple(map(lambda n: int(n) - 1, input().split()))

visited = [0] * n
next_town = 0
order = []

while True:
    if visited[next_town]:
        break
    order.append(next_town)
    visited[next_town] = 1
    next_town = a[next_town]

first_loop = len(order)
offset = order.index(next_town)
second_loop = first_loop - offset

if k < first_loop:
    print(order[k] + 1)
else:
    print(order[offset + (k - first_loop) % second_loop] + 1)
from itertools import count

N, K = map(int, input().split())
As = [int(n) for n in input().split()]

visited = []
visited_set = set()
now = 0
num = 0
idx = 0
for i in count():
    if now in visited_set:
        idx = visited.index(now)
        num = len(visited) - idx
        break
    visited_set.add(now)
    visited.append(now)
    now = As[now] - 1

if K <= idx:
    print(visited[K] + 1)
else:
    print(visited[idx + (K - idx) % num] + 1)
n, k = map(int, input().split())
a = list(map(int, input().split()))

b = []
ord = [-1 for x in range(n+1)]
c = 1
l = 1

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

if (k < l):
    print(b[k])
else:
    k -= l
    k %= c
    print(b[l+k])
N, K= list(map(int, input().split()))
A_list = list(map(int, input().split()))

loop = []
trace = [0] * (N+1)
visited = set()

ai = 1
for i in range(N+1):
    if ai in visited:
        ind = trace.index(ai)
        loop = trace[ind:i]
        trace = trace[:ind]
        break
    visited.add(ai)
    
    trace[i] = ai # ??
    ai = A_list[ai-1]


if len(trace) > K:
    print(trace[K])
else:
    ind = K - len(trace)
    ind = ind % len(loop)
    print(loop[ind])
#!/usr/bin/env python3

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

if K <= N:
    p = 0
    for _ 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)
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(i+1)
  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()))

for index, item in enumerate(a):
    a[index] -= 1

mp = dict()
now = 0
step = 0
rec = []
while not now in mp:
    mp[now] = step
    rec.append(now)
    step += 1
    now = a[now]

path = rec[mp[now]:step]

if k < mp[now]:
    print(rec[k] + 1)
else:
    k = (k - mp[now]) % len(path)
    print(path[k] + 1)
N,K=map(int,input().split())
A=[0]+list(map(int,input().split()))
c=1
while K:
  if K&1:
    c=A[c]
  A=[A[A[i]] for i in range(N+1)]
  K>>=1
print(c)
N, K = map(int, input().split())
A = input().split()
A = [int(a) for a in A]
S = [1]
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+1:number+1]
if K <= number:
  print(S[K ])
else:
  z = K - c-1
  y = z % (number - c)
  print(T[y])
n,k = map(int,input().split())
A = [0]+list(map(int,input().split()))
visited = [False for _ in range(n+1)]
pos = 1
x = []
flg = False
for i in range(k):
    if visited[pos] == False:
        x.append(pos)
        visited[pos] = True
        pos = A[pos]
    else:
        flg = True
        pre = x.index(pos)
        loop = x[pre:]
        break
if not flg:
    print(pos)
else:
    print(loop[(k-pre)%len(loop)])
n, k = map(int, input().split())
a = tuple(map(int, input().split()))

i = 0
aa = 0
visited = {}
for i in range(k):
    aa = a[aa] - 1
    if aa in visited:
        s, e = visited[aa], i
        loop = e - s
        k = s + (((k-1) - s) % loop)
        print(list(visited.keys())[k] + 1)
        exit(0)
    visited[aa] = i

print(aa+1)
import sys
input = sys.stdin.readline

n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

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

for i in range(1, n + 5):
    now = a[now] - 1
    if now in visited:
        break
    route.append(now)
    visited.add(now)

start = route.index(now)
loop = i - start

if k <= start:
    print(route[k] + 1)
else:
    k -= start
    k %= loop
    route = route[start:]
    print(route[k] + 1)
N, K = map(int, input().split())
towns = list(map(int, input().split()))
towns.insert(0, -1)
p = 1
tmp_move, first_move, roop_move = [1], [], []
visited = [False for _ in range(N + 1)]
res = -1

while not visited[p]:
    visited[p] = True
    tmp_move.append(towns[p])
    p = towns[p]

index = tmp_move.index(p)
roop_move = tmp_move[index:-1]
first_move = tmp_move[:index]

if K < len(first_move):
    res = first_move[K]
else:
    index = (K - len(first_move)) % len(roop_move)
    res = roop_move[index]

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

pos = [1]

for i in range(2 * n):
	nxt = A[pos[-1]]
	pos.append(nxt)

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

loop_end = n
loop_start = n - 1

while pos[loop_start] != pos[loop_end]:
	loop_start -= 1

period = loop_end - loop_start
m = n - period
k = m + (k - m) % period

print(pos[k])
n, k = map(int, input().split())
towns = [int(s) for s in input().split()]
pos = []
pos_set = set()
cur = towns[0]
i = 0
end = False
while not cur in pos_set:
    i += 1
    if k == i:
        print(cur)
        end = True
        break
    pos.append(cur)
    pos_set.add(cur)
    cur = towns[cur-1]

if not end:
    j = pos.index(cur)
    print(pos[(k-j-1)%(len(pos)-j) + j])
N, K = list(map(int, input().split()))
A = [0] + list(map(int, input().split()))

path = [0]*(N+1)
visited = set()

pos = 1
for i in range(N+1):
    if pos in visited: break
    path[i] = pos
    visited.add(pos)
    pos = A[pos]

loc = path.index(pos)
head = path[:loc]
rep = path[loc:i]

if K < len(head):
    print(head[K])
else:
    print(rep[(K - len(head)) % len(rep)])
N, K = map(int, input().split())
A = tuple(int(x) - 1 for x in input().split())

done = {0}
rt = [0]
loop_point = 0
while True:
    p = rt[-1]
    np = A[p]
    if np in done:
        loop_point = np
        break
    done.add(np)
    rt.append(np)
if K < len(rt):
    ans = rt[K] + 1
else:
    K -= len(rt)
    rt = rt[rt.index(loop_point):]
    ans = rt[K%len(rt)] + 1
print(ans)
N, K = map(int, input().split())
A = list(map(int, input().split()))

v, p, s = [-1] * N, 1, K
while s > 0:
    if v[p - 1] == -1:
        v[p - 1] = s
    else:
        p = v.index(v[p - 1] - s % (v[p - 1] - s)) + 1
        break
    p, s = A[p - 1], s - 1
print(p)
n,k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
v = [0 for i in range(n+1)]
v[0] = 1
r = [1]
p = a[0]
while v[p]==0:
  v[p] = 1
  r.append(p)
  if len(r)==k+1:
    break
  p = a[p-1]
if len(r)==k+1:
  print(p)
else:
  idx = r.index(p)
  k -= idx
  print(r[k%(len(r)-idx)+idx])
N, K = map(int, input().split())
A = list(map(lambda a: int(a) - 1, input().split()))

T = [A]
for _ in range(64):
    prev = T[-1]
    T.append([prev[a] for a in prev])

now = 0
for d, P in enumerate(T):
    if ((1 << d) & K) > 0:
        now = P[now]
print(now + 1)
[n,k]=list(map(int,input().split()))
alist=list(map(int,input().split()))

#足跡
footprint=[0]*n

omoide=[]
now=0
counter=1
while True:
    if footprint[now]!=0:
        break
    footprint[now]=counter
    omoide.append(now)
    goto=alist[now]-1
    now=goto
    counter+=1

joso=footprint[now]-1
shuki=counter-footprint[now]

if k>joso:
    k-=joso
    k=k%shuki

    ans=omoide[joso+k]+1

else:
    ans=omoide[k]+1
    
print(ans)
N, K = map(int, input().split())
*A, = map(int, input().split())
A = [a-1 for a in A]

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

i -= 1
ans = 0
while i >= 0:
    if K & (1 << i) > 0:
        ans = to[i][ans]
    i -= 1
print(ans+1)
n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))

ans=-1
l=[-1]*n
now=0
i=1
result=[0]
while i<=k:
    now=a[now]
    if l[now]!=-1:
        
        length=i-l[now]
        d=(k-i)%length+l[now]
        ans=l.index(d)
        break
    l[now]=i
    i+=1
    
if ans!=-1:
    print(ans+1)
else:
    print(now+1)
N,K=map(int,input().split())
A=list(map(int,input().split()))
dic={}
goto=A[0]
for i in range(K):
    tmp=goto
    goto=A[tmp-1]
    if not tmp in dic:
        dic[tmp]=i
    else:
        bef=dic[tmp]
        loop=i-bef
        rem=K-1-i
        rem=rem%loop
        for j in range(rem):
            tmp=goto
            goto=A[tmp-1]
        break

print(tmp)
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 = [*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 + min(K, one)

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

visited = set()
current = 0
answer = 0
count = 0
while current not in visited and count < K:
    visited.add(current)
    count += 1
    current = A[current]
    answer = current

if count == K:
    print(answer + 1)
    exit(0)

# print(count)
start = A[current]
ary = [start]
loop_size = 1
while start != current:
    loop_size += 1
    start = A[start]
    ary.append(start)

# print(ary)
# print('loop_size', loop_size)
print(ary[(K - count) % loop_size - 1] + 1)
inpl = lambda: list(map(int,input().split()))
N, K = inpl()
A = inpl()
for i in range(N):
    A[i] -= 1
P = dict()
k = 0
former = True
i = 1
while i <= K:
    if k in P.keys() and former:
        loop = i - P[k]
        K = i + (K-i) % loop
        former = False
    else:
        P[k] = i
    k = A[k]
    i += 1
else:
    print(k+1)
n, k = map(int, input().split())
a = [0]+list(map(int, input().split()))

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

for i in range(k):
    num = a[num]
    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])
N, K = map(int, input().split())
Alist = list(map(int, input().split()))
path = [0]
next = Alist[0]
seen = set()
seen.add(0)
while (next-1) not in seen:
    seen.add(next-1)
    path.append(next-1)
    next = Alist[next-1]
base = path.index(next-1)
loop_length = len(path) - base
loop = path[base:base+loop_length+1]
# print(path)
# print(loop, loop_length, base)
if K <= base:
    print(path[K]+1)
else:
    K -= base
    print(loop[K%loop_length]+1)
#!/usr/bin/env python3
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = [1]
c = 1
for i in range(n):
    ans.append(a[ans[i] - 1])
b = ans.index(ans[n])
if k < b:
    for l in range(k):
        c = a[c - 1]
else:
    for l in range((k-b)%(n-b)+b):
        c = a[c - 1]
print(c)
N,K = map(int,input().split())
As = list(map(int,input().split()))
towns = [0]*N
route = [0]
pre = 0
i = 0
towns[0] = 1
for _ in range(K):
    i = As[i]-1
    if towns[i]>0:
        pre = route.index(i)
        route = route[pre:]
        break
    route.append(i)
    towns[i] = 1
mod = len(route)
print(route[(K-pre)%mod]+1)
import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N, K = rl()
A = rl()

edges = {}
for i, a in enumerate(A):
	edges[i] = a-1

mem = {}
k = 0
n = 0
while True:
	if k == K:
		print(n+1)
		exit()
	if n in mem:
		break
	mem[n] = k
	k += 1
	n = edges[n]

p = k-mem[n]
idx = (K - k) % p

for i in range(idx):
	n = edges[n]
print(n+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))
i = 0
ans = 0
root = [-1] * n
flag = [False] * n
j = 0
while flag[a[i] - 1] == False:
    flag[a[i] - 1] = True
    root[j] = a[i]
    i = a[i] - 1
    j += 1
s_i = root.index(a[i])
x = 0
for i in range(k % (j - s_i)):
    ans = root[x]
    x = root[x] - 1

print(ans)
n,k=map(int,input().split())
a=[int(x) for x in input().rstrip().split()]

l=[0 for i in range(n)]
cnt=0

dist=[]
now=0
while(True):
  
  if l[now]+1==3:
    break
  dist.append(now)

  l[now]+=1
  now=a[now]-1


k=k-l.count(1)
g=l.count(2)
# print(dist)
# print(l)


dist=dist[l.count(1):]

print(dist[k%g]+1)
def main():
    N , K = list(map(int,input().split()))
    A = list(map(int,input().split()))
    A.insert(0,0)
    local = 1
    tmp = []
    tmp.append(local)
    for k in range(N):
        local = A[local]
        if local in tmp:
            result = len(tmp) - tmp.index(local)
            break
        else:
            tmp.append(local)
    
    for k in range((K-tmp.index(local)) % result):
        local = A[local]
    print(local)

if __name__ == '__main__':
    main()
n, k = map(int, input().split())
a = [int(x)-1 for x in input().split()]

visited = [0] * n
visit_list = [0]
visit = 0
while True:
    visit_list.append(a[visit])
    visit = a[visit]
    if visited[visit] == 1:
        break
    visited[a[visit]] = 1

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

def ml(a):
	cnt=1
	nxt=a[0]-1
	done.append(0)
	done.append(nxt)
	while(True):
		nxt = a[nxt]-1
		if nxt in done: 
			m = done.index(nxt) - 1
			l = cnt - m
			return (m+1,l)
		else: done.append(nxt)
		cnt+=1

# print(is_1(a))
m,l = ml(a)
num = (k-m)%l

# print(f"m={m},l={l}")
# print(f"done={done}")
# print(f"m+num={m+num}")
print(done[m+num]+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))
now = 1
for i in range(min(k,int(2*n-1))):
    now = a[now-1]
print(now)
n,k = map(int, input().split()) #int
a = input().replace(" ","") #str
chokudai = "1" #str
fp = "1" #str
telenum = 0 #int
while True:
  chokudai = a[int(chokudai)-1]
  telenum += 1
  rooph = fp.find(chokudai) #int
  if rooph != -1:
    rooplen = len(fp)-rooph
    break
  else:
    fp = fp+chokudai
print(fp[rooph+k%rooplen])