AtCoder Beginner Contest 167 - Teleporter

The Kingdom of Takahashi has N towns, numbered 1 through N.
There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.
Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Help the king by writing a program that answers this question.

-----Constraints-----
 - 2 \leq N \leq 2 \times 10^5
 - 1 \leq A_i \leq N
 - 1 \leq K \leq 10^{18}

-----Input-----
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N

-----Output-----
Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

-----Sample Input-----
4 5
3 2 4 1

-----Sample Output-----
4

If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.

Difficulty: MEDIUM

Starter code:

Solutions:
(N, K) = map(int, input().split())
A = list(map(lambda x: int(x) - 1, input().split()))
done = [-1] * N
done[0] = 0
tmp = 0
done[0] = 0
for k in range(1, K + 1):
	tmp = A[tmp]
	if done[tmp] >= 0:
		for _ in range((K - done[tmp]) % (k - done[tmp])):
			tmp = A[tmp]
		print(tmp + 1)
		return
	else:
		done[tmp] = k
print(tmp + 1)


(N, K) = map(int, input().split())
A = [int(i) - 1 for i in input().split()]
X = [-1] * N
i = 0
t = 0
while X[i] < 0:
	X[i] = t
	i = A[i]
	t += 1
X[i] + (K - X[i]) % (t - X[i])
if K <= X[i]:
	print(X.index(K) + 1)
else:
	print(X.index(X[i] + (K - X[i]) % (t - X[i])) + 1)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = []
ord = [-1 for i in range(n + 1)]
v = 1
c = 1
l = 0
while ord[v] == -1:
	ord[v] = len(s)
	s.append(v)
	v = a[v - 1]
c = len(s) - ord[v]
l = ord[v]
if k < l:
	print(s[k])
else:
	k -= l
	k %= c
	print(s[l + k])


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
dic = {}
wk = 1
start = 0
for i in range(1, n + 1):
	wk = a[wk - 1]
	if wk in dic:
		start = dic[wk]
		break
	dic[wk] = i
end = len(dic)
loopcnt = end - start + 1
keys = list(dic.keys())
if k < start:
	print(keys[k - 1])
else:
	chk = (k - start + 1) % loopcnt
	if chk == 0:
		chk = loopcnt
	print(keys[start - 1 + chk - 1])


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
B = [0] * N
B[0] = 1
if B[0] in A:
	c = B[0]
for i in range(1, N):
	B[i] = A[B[i - 1] - 1]
d = B.index(B[-1]) + 1
if K <= N:
	print(B[K])
	exit()
print(B[d - 1 + (K + 1 - d) % (N - d)])


(n, k) = map(int, input().split())
A = list(map(lambda x: int(x) - 1, input().split()))
prev = 0
count = 0
used = {prev: count}
for _ in range(min(k, n)):
	prev = A[prev]
	count += 1
	if prev in used:
		for _ in range((k - used[prev]) % (count - used[prev])):
			prev = A[prev]
		break
	used[prev] = count
print(prev + 1)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
pos = 0
res = [-1] * 3 * 10 ** 5
d = 0
way = []
l = 0
c = 0
for i in range(N):
	A[i] -= 1
for i in range(K + 1):
	if res[pos] != -1:
		l = d - res[pos]
		c = d - l
		res[pos] = d
		break
	res[pos] = d
	way.append(pos)
	d += 1
	pos = A[pos]
ans = 0
if K + 1 <= len(way):
	ans = way[K]
else:
	K -= c
	K = K % l
	ans = way[K + c]
print(ans + 1)


(N, K) = map(int, input().split())
A = [0] + list(map(int, input().split()))
now = 1
while K > 0:
	if K & 1:
		now = A[now]
	a = [A[i] for i in A]
	A = a
	K = K >> 1
print(now)


(n, k) = map(int, input().split())
b = list(map(int, input().split()))
a = 0
for i in range(n):
	b[i] -= 1
if k < n:
	for _ in range(k):
		a = b[a]
	print(a + 1)
	exit()
c = [-1] * n
cc = 0
se = set()
while a not in se:
	c[a] = cc
	cc += 1
	se.add(a)
	a = b[a]
for _ in range((k - c[a]) % (cc - c[a])):
	a = b[a]
print(a + 1)


(n, k) = [int(x) for x in input().split()]
a_list = [int(x) - 1 for x in input().split()]
ci = 0
while k:
	if k & 1:
		ci = a_list[ci]
	k >>= 1
	a_list = [a_list[a_list[i]] for i in range(n)]
print(ci + 1)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
A = [-1] + A
machi = [-1] * (N + 1)
counter = 0
basyo = 1
while counter < K:
	counter += 1
	basyo = A[basyo]
	if machi[basyo] == -1:
		machi[basyo] = counter
	else:
		tmp = counter - machi[basyo]
		counter += (K - counter) // tmp * tmp
print(basyo)


(n, k, *a) = map(int, open(0).read().split())
v = [-1] * n
t = c = 0
while v[t] < 0:
	v[t] = c
	c += 1
	t = a[t] - 1
l = v[t]
c -= l
print(v.index(min(k, l + (k - l) % c)) + 1)


(n, k, *a) = map(int, open(0).read().split())
a = [x - 1 for x in a]
dist = [0] * n
cur = 0
while True:
	nxt = a[cur]
	if k == 0:
		print(cur + 1)
		break
	if dist[nxt]:
		k = (k - dist[nxt]) % (dist[cur] - dist[nxt] + 1) + dist[nxt]
		k -= 1
		dist = [0] * n
		cur = nxt
	else:
		k -= 1
		dist[nxt] = dist[cur] + 1
		cur = nxt


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)


import sys
from collections import deque, defaultdict, Counter
from itertools import accumulate, product, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush
from math import ceil, floor, sqrt, gcd, inf
from copy import deepcopy
import numpy as np
import scipy as sp
INF = inf
MOD = 1000000007
(n, k) = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
tmp = [0 for i in range(n + 1)]
cnt = 0
res = 1
while cnt < k:
	cnt += 1
	if tmp[res] == 0:
		tmp[res] = cnt
	else:
		cnt += (k - cnt) // (cnt - tmp[res]) * (cnt - tmp[res])
		tmp[res] = cnt
	res = A[res - 1]
print(res)


import numpy as np

def main():
	(n, k) = list(map(int, input().split()))
	warps = list(map(int, input().split()))
	warps = [0] + warps
	warps = np.array(warps, dtype=int)
	dp = np.zeros((k.bit_length() + 1, n + 1), dtype=int)
	dp[0, :] = warps
	for h in range(1, len(dp)):
		dp[h] = np.take(dp[h - 1], dp[h - 1])
	node = 1
	for i in range(k.bit_length(), -1, -1):
		if k >> i & 1:
			node = dp[i][node]
	print(node)
main()


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
v = []
w = {}
now = 0
for i in range(K):
	p = A[now]
	now = p - 1
	if p in w:
		j = v.index(p)
		df = i - j
		Kn = (K - i - 1) % df
		p = v[j + Kn]
		break
	else:
		v.append(p)
		w[p] = p
print(p)


(n, k) = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
ii = 0
array = [-1] * n
for i in range(k):
	if array[ii] >= 0:
		r = i - array[ii]
		k -= array[ii]
		m = k % r
		print(array.index(m + array[ii]) + 1)
		exit()
	array[ii] = i
	ii = a[ii]
else:
	print(ii + 1)


(N, K) = list(map(int, input().split()))
A = [int(x) for x in input().split()]
already = set()
already.add(1)
placelist = [1]
numofmove = 0
now = 1
loopstart = 0
loopend = 0
remain = 0
while numofmove < K:
	now = A[now - 1]
	numofmove += 1
	placelist.append(now)
	if now in already:
		loopstart = placelist.index(now)
		loopend = numofmove
		remain = (K - numofmove) % (loopend - loopstart)
		for i in range(remain):
			now = A[now - 1]
		break
	else:
		already.add(now)
print(now)


(N, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
count = 0
stack = set()
visited = []
visited_set = set()
endf = False
current = 0
while True:
	i = A[current] - 1
	visited_set.add(current)
	visited.append(current)
	if not i in visited_set:
		current = i
		count += 1
	else:
		leng = len(visited_set)
		roop_len = leng - visited.index(i)
		header_len = leng - roop_len
		break
	if count >= K:
		endf = True
		print(i + 1)
		break
if not endf:
	roop_ind = (K - header_len + 1) % roop_len
	if roop_ind == 0:
		print(visited[-1] + 1)
	else:
		print(visited[header_len + roop_ind - 1] + 1)


(N, K) = map(int, input().split())
A = [int(i) for i in input().split()]
R = [False] * (N + 1)
D = []
p = 1
while True:
	if R[p]:
		break
	D.append(p)
	R[p] = True
	p = A[p - 1]
m = D.index(p)
c = len(D) - m
if K <= m:
	print(D[K])
else:
	print(D[m + (K - m) % c])


(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)


(n, k) = map(int, input().split())
num = list(map(int, input().split()))
t = [-1] * n
count = 0
s = 1
while t[s - 1] == -1:
	if count == k:
		break
	t[s - 1] = count
	count += 1
	s = num[s - 1]
else:
	z = (k - t[s - 1]) % (count - t[s - 1])
	s = t.index(t[s - 1] + z) + 1
print(s)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
tele = [1]
went = set(tele)
now = 1
cnt = 0
flag = True
for i in range(k):
	now = a[now - 1]
	if i != k - 1 and now in went:
		tele = tele[tele.index(now):]
		cnt = i + 1
		flag = False
		break
	tele.append(now)
	went.add(now)
if flag == True:
	print(tele[-1])
else:
	k -= cnt
	cnt = k % len(tele)
	print(tele[cnt])


(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) = map(int, input().split())
A = [0] + list(map(int, input().split()))
T = {}
T[1] = 1
t = 1
for i in range(2, K + 1):
	a = A[t]
	t = a
	K -= 1
	if a in T:
		l = list(T.keys())[T[a] - 1:]
		print(l[K % len(l)])
		break
	else:
		T[a] = i
else:
	print(A[t])


(N, K) = map(int, input().split())
m = 60
A = list(map(int, input().split()))
A = [A[i] - 1 for i in range(N)]
D = [A]
for i in range(m):
	temp = []
	for j in range(N):
		temp.append(D[i][D[i][j]])
	D.append(temp)
now = 0
for i in range(m):
	if K >> i & 1 == 1:
		now = D[i][now]
print(now + 1)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
V = [-1] * N
X = 0
V[0] = 0
T = 0
while T < K:
	T += 1
	X = A[X] - 1
	if V[X] != -1:
		t = T - V[X]
		k = (K - T) % t
		for i in range(k):
			X = A[X] - 1
		break
	V[X] = T
print(X + 1)


(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())
arr = list(map(int, input().split()))
for i in range(n):
	arr[i] -= 1
flag = format(k, 'b')
flag = flag[::-1]
pos = 0
for i in range(len(flag)):
	if flag[i] == '1':
		pos = arr[pos]
	tmp = []
	for j in range(n):
		tmp.append(arr[arr[j]])
	arr = tmp
print(pos + 1)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
p = 1
if k <= n:
	for i in range(k):
		p = a[p - 1]
	print(p)
	exit()
t = [-1] * n
t[0] = 0
for i in range(1, n):
	p = a[p - 1]
	if t[p - 1] != -1:
		break
	t[p - 1] = i
d = i - t[p - 1]
k -= i
k %= d
for i in range(k):
	p = a[p - 1]
print(p)


(n, k) = map(int, input().split())
A = [int(x) - 1 for x in input().split()]
B = [0]
visited = [False] * n
for (i, b) in enumerate(B):
	if visited[b]:
		break
	visited[b] = True
	B.append(A[b])
j = B.index(b)
r = (k - j) % (i - j)
ans = B[j:][r] + 1 if k >= j else B[k] + 1
print(ans)


(n, k) = map(int, input().split())
a = [int(x) - 1 for x in input().split()]
now = 0
b = [0] * n
while k != 0:
	if k & 1:
		now = a[now]
	k >>= 1
	for i in range(n):
		b[i] = a[a[i]]
	a = b[:]
print(now + 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) = [int(s) for s in input().split()]
ls = [int(s) for s in input().split()]
town = set()
town.add(1)
town_ls = [1]
now = 1
for i in range(min([K, N])):
	now = ls[now - 1]
	if now in town:
		break
	town.add(now)
	town_ls.append(now)
if i + 1 == K:
	print(now)
else:
	L = i + 1
	S = town_ls.index(now)
	loop = L - S
	x = (K - S) % loop + S
	print(town_ls[x])


(N, K) = map(int, input().split())
A = list(map(lambda x: int(x) - 1, input().split()))
res = list(range(N))
while K:
	if K & 1:
		res = [res[A[i]] for i in range(N)]
	A = [A[A[i]] for i in range(N)]
	K >>= 1
print(res[0] + 1)


(n, k) = map(int, input().split())
a = [0] + list(map(int, input().split()))
c = [0] * (n + 1)
c[1] = 1
q = [0, 1]
while True:
	i = a[q[-1]]
	if c[i]:
		break
	c[i] = c[q[-1]] + 1
	q += [i]
if c[i] < k:
	k -= c[i] - 1
	t = c[q[-1]] - c[i] + 1
	print(q[c[i] + k % t])
else:
	print(q[k + 1])


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
z = 0
b = [1]
c = [0] * n
for i in range(n * 3):
	j = [a[b[i] - 1]]
	b.extend(j)
	c[a[b[i] - 1] - 1] = c[a[b[i] - 1] - 1] + 1
	if c[a[b[i] - 1] - 1] == 2:
		z = z + 1
if k <= n:
	y = b[k]
else:
	l = (k - n) % z
	y = b[n + l]
print(y)


(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()))
button = list(map(int, input().split()))
visit = [-1] * n
hisitory = [-1]
push = 0
f = 0
for i in range(k):
	visit[push] = i + 1
	hisitory.append(push)
	push = button[push] - 1
	if visit[push] != -1:
		f = 1
		start = visit[push]
		last = push
		end = i + 1
		break
if f == 0:
	print(button[hisitory[-1]])
else:
	geta = start - 1
	loopk = k - geta
	loop = end - start + 1
	modk = loopk % loop
	if modk == 0:
		modk = loop
	print(button[hisitory[geta + modk]])


(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(d[b[c] + (k - b[c]) % (i - b[c])])
		exit()
	b[c] = i
	c = a[c - 1]
print(d[-1])


(N, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
A = [a - 1 for a in A]
town = [-1] * N
move = 0
now = 0
while move < K:
	town[now] = move
	now = A[now]
	move += 1
	if town[now] != -1:
		break
cnt = (K - move) % (move - town[now])
for i in range(cnt):
	now = A[now]
print(now + 1)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
A = [A[k] - 1 for k in range(N)]
now = 0
for k in range(70):
	if K >> k & 1 == 1:
		now = A[now]
	A = [A[A[l]] for l in range(N)]
print(now + 1)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
order = [-1] * (n + 1)
s = []
v = 1
while order[v] == -1:
	order[v] = len(s)
	s.append(v)
	v = a[v - 1]
c = len(s) - order[v]
l = order[v]
if k < l:
	print(s[k])
else:
	k -= l
	k %= c
	print(s[l + k])


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
a = [i - 1 for i in a]
d = [1] * n
p = 0
b = []
while d[p]:
	b.append(p)
	d[p] = 0
	p = a[p]
avant = b.index(p)
loop_count = len(b) - avant
c = b[avant:]
if k <= avant:
	print(b[k] + 1)
else:
	k -= avant
	k %= loop_count
	print(c[k] + 1)


def __starting_point():
	(n, k) = list(map(int, input().split()))
	A = [0]
	AA = list(map(int, input().split()))
	A = A + AA
	B = set()
	cnt = 0
	ind = 1
	startind = 0
	D = []
	while True:
		if A[ind] not in B:
			B.add(ind)
			D.append(ind)
			cnt += 1
			ind = A[ind]
		else:
			B.add(ind)
			D.append(ind)
			cnt += 1
			startind = A[ind]
			break
	C = set()
	ind = startind
	ANS = []
	while True:
		if A[ind] not in C:
			C.add(ind)
			ind = A[ind]
			ANS.append(ind)
		else:
			C.add(ind)
			ANS.append(A[ind])
			break
	if k >= cnt:
		tmp = k - cnt
		tmp = tmp % len(C)
		print(ANS[tmp - 1])
	else:
		print(D[k])
__starting_point()


(N, K) = map(int, input().split())
A = [-1] + list(map(int, input().split()))
id = [-1] * (N + 1)
x = 1
l = 0
a = []
while id[x] == -1:
	a.append(x)
	id[x] = l
	l += 1
	x = A[x]
c = l - id[x]
if K < l:
	print(a[K])
else:
	K -= l
	print(a[id[x] + K % c])


(N, K, *A) = map(int, open(0).read().split())
t = [[-1] * N for _ in range(61)]
t[0] = [A[i] - 1 for i in range(N)]
for k in range(1, 61):
	for i in range(N):
		t[k][i] = t[k - 1][t[k - 1][i]]
u = 0
for k in range(61):
	if K >> k & 1:
		u = t[k][u]
print(u + 1)


f = lambda : map(int, input().split())
(N, K) = f()
(*A,) = [0] + list(f())
F = [0] * (2 * 10 ** 5 + 1)
G = [1]
F[1] = 1
i = 0
while i <= N:
	n = A[G[-1]]
	if F[n]:
		break
	F[n] = 1
	G.append(n)
	i += 1
t = G.index(A[G[-1]])
if t <= K:
	G = G[t:]
	K = (K - t) % len(G)
print(G[K])


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = 1
ans = 0
v = {}
for j in range(len(a)):
	if ans == k:
		print(s)
		exit()
	if s - 1 in v:
		break
	else:
		v.update({s - 1: ans})
		s = a[s - 1]
		ans += 1
lens = ans - v[s - 1]
k = (k - ans) % lens
for j in range(k):
	s = a[s - 1]
print(s)


(N, K) = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
check = [0] * N
ima = 0
for i in range(K):
	if check[A[ima] - 1] != 0:
		loopen = ima
		check[ima] = i + 1
		looplen = check[ima] - check[A[ima] - 1] + 1
		loopend = i
		break
	if i == K - 1:
		print(A[ima])
		return
	check[ima] = i + 1
	ima = A[ima] - 1
offset = (K - (i - looplen) - 1) % looplen
ima = 0
for i in range(loopend - looplen + offset + 1):
	ima = A[ima] - 1
print(ima + 1)


(n, k) = map(int, input().split())
b = [bi - 1 for bi in list(map(int, input().split()))]
a = 0
f = [0] * n
c = 1
for i in range(k):
	if f[a]:
		k %= c - f[a]
		for i in range(k):
			a = b[a]
		print(a + 1)
		break
	f[a] = c
	k -= 1
	c += 1
	a = b[a]
else:
	print(a + 1)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
dist = [0 for i in [0] * (n + 1)]
idx = 1
d = 1
while not dist[idx] and d < k + 1:
	dist[idx] = d
	idx = a[idx - 1]
	d += 1
if d == k + 1:
	print(idx)
else:
	rem = dist[idx] - 1
	cyc = d - dist[idx]
	k -= rem
	k %= cyc
	for i in range(k):
		idx = a[idx - 1]
	print(idx)


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

def index_multi(l, x):
	return [i for (i, _x) in enumerate(l) if _x == x]
t = 0
lis = [1]
count = [0] + [-1] * (n - 1)
for i in range(2 * 10 ** 5 + 1):
	lis.append(a[t])
	t = a[t] - 1
	count[t] += 1
l_start_number = 0
for i in range(n):
	if count[i] > 0:
		l_start_number = i + 1
		break
l_start = index_multi(lis, l_start_number)[0]
l_goal = index_multi(lis, l_start_number)[1] - 1
loop_lis = []
for i in range(l_start, l_goal + 1):
	loop_lis.append(lis[i])
loop = len(loop_lis)
if k >= l_start:
	count = (k - l_start) % loop
	print(loop_lis[count])
else:
	print(lis[k])


(N, K) = map(int, input().split())
A = [0] + list(map(int, input().split()))
now = 1
route = []
dic = {}
cnt = 0
entry = []
loop = []
while True:
	cnt += 1
	now = A[now]
	route.append(now)
	if now in dic:
		entry = route[:dic[now]]
		loop = route[dic[now]:]
		break
	dic[now] = cnt
if K <= len(entry):
	print(entry[K - 1])
else:
	K -= 1
	K -= len(entry)
	K %= len(loop)
	print(loop[K])


(n, k) = list(map(int, input().split()))
m = list(map(int, input().split()))
a = []
p = 1
vis = [0] * n
while vis[p - 1] == 0:
	a.append(p)
	vis[p - 1] = 1
	p = m[p - 1]
l = a.index(p)
if k < len(a):
	print(a[k])
else:
	k -= l
	print(a[l + k % (len(a) - l)])


(n, k) = map(int, input().split())
a = [int(i) for i in input().split()]
d = {0: 1}
b = {1: 0}
p = 0
for i in range(k):
	d[i + 1] = a[p]
	b[a[p]] = i + 1
	p = a[p] - 1
	if a[p] in b:
		k = (k - b[a[p]]) % (i + 2 - b[a[p]]) + b[a[p]]
		break
print(d[k])


import numpy as np

def main():
	(n, k) = list(map(int, input().split()))
	warps = list(map(int, input().split()))
	warps = [0] + warps
	warps = np.array(warps, dtype=int)
	order_num = np.zeros(len(warps) + 1, dtype=int)
	path_history = []
	node = 1
	while order_num[node] == 0:
		path_history.append(node)
		order_num[node] = len(path_history)
		node = warps[node]
	begin = order_num[node]
	if k <= begin:
		print(path_history[k])
	else:
		leng = len(path_history) - order_num[node] + 1
		index = (k - (begin - 1)) % leng
		print(path_history[begin - 1 + index])
main()


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
ls = [a[:]]
for i in range(70):
	lsa = [0] * n
	for j in range(n):
		lsa[j] = ls[-1][ls[-1][j] - 1]
	ls.append(lsa)
ans = 1
for i in range(71)[::-1]:
	if k & 1 << i:
		ans = ls[i][ans - 1]
print(ans)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
dic = {}
list_town = []
set_town = set()
town = 1
for (i, v) in enumerate(A):
	dic[i + 1] = v
for _ in range(K):
	list_town.append(town)
	set_town.add(town)
	town = dic[town]
	if town in set_town:
		stop_town = town
		break
if N <= K:
	list_first_split = list_town[:list_town.index(stop_town)]
	list_second_split = list_town[list_town.index(stop_town):]
	print(list_second_split[(K - len(list_first_split)) % len(list_second_split)])
else:
	print(town)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
t = {1: 0}
g = {0: 1}
(c, d) = (1, 0)
while d != k:
	d += 1
	c = a[c - 1]
	if c in t:
		print(g[t[c] + (k - t[c]) % (d - t[c])])
		exit()
	else:
		t[c] = d
		g[d] = c
print(c)


(n, k, *a) = map(int, open(0).read().split())
d = [2 ** 61] * -~n
s = 1
while k:
	k -= 1
	s = a[s - 1]
	k %= d[s] - k
	d[s] = k
print(s)


(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)])


(N, K) = map(int, input().split())
A = [~-int(x) for x in input().split()]
(p, c) = ([-1] * N, 0)
for i in range(N + 1):
	if p[c] == -1:
		p[c] = i
		c = A[c]
	else:
		break
print(1 + p.index(p[c] + (K - p[c]) % (i - p[c]) if K >= p[c] else K))


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
adj = [1] * n
b = [1]
now = 0
while adj[now]:
	adj[now] = 0
	now = a[now] - 1
	b.append(now + 1)
loop_start = b.index(b[-1])
loop = b[loop_start:-1]
p = len(loop)
if k < loop_start:
	print(b[k])
else:
	k -= loop_start
	k %= p
	print(loop[k])


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
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()))
d = 60
dp = [[-1] * n for i in range(d + 1)]
for (i, aa) in enumerate(a):
	dp[0][i] = aa - 1
for i in range(d):
	for j in range(n):
		dp[i + 1][j] = dp[i][dp[i][j]]
ans = 0
for i in range(d):
	if k & 1 << i:
		ans = dp[i][ans]
print(ans + 1)


(n, k, *a) = map(int, open(0).read().split())
a = [0] + a
p = 1
while k:
	if k % 2:
		p = a[p]
	a = [a[b] for b in a]
	k //= 2
print(p)


(N, K, *A) = map(int, open(0).read().split())
x = 1
C = [None] * N
C[0] = 0
c = 0
while c < K:
	x = A[x - 1]
	c += 1
	if C[x - 1] is None:
		C[x - 1] = c
	else:
		a = C[x - 1]
		b = c - a
		K = (K - a) % b + a + b
		while c < K:
			x = A[x - 1]
			c += 1
		break
print(x)


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()))
count = 0
now = 1
city = [1]
city2 = {1}
while count < K:
	count += 1
	now = A[now - 1]
	if now in city2:
		break
	else:
		city.append(now)
		city2.add(now)
if count == K:
	print(now)
else:
	n = city.index(now)
	K -= n
	count -= n
	K %= count
	print(city[n + K])


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
vis = [0] * N
p = 1
Town = []
while vis[p - 1] == 0:
	Town.append(p)
	vis[p - 1] = 1
	p = A[p - 1]
l = Town.index(p)
if K < len(Town):
	print(Town[K])
else:
	print(Town[l + (K - l) % len(Town[l:])])


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = []
ord = [-1] * (n + 1)
v = 1
while ord[v] == -1:
	ord[v] = len(s)
	s.append(v)
	v = a[v - 1]
c = len(s) - ord[v]
l = ord[v]
ans = 0
if k < l:
	ans = s[k]
else:
	k -= l
	k %= c
	ans = s[l + k]
print(ans)


(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):
	v.append([v[i][v[i][j]] for j in range(N)])
	if K >> i & 1:
		now = v[i][now]
print(now + 1)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
a = [i - 1 for i in a]
g = [-1] * n
i = 0
j = 0
while g[j] == -1:
	if i == k:
		print(j + 1)
		exit()
	g[j] = i
	i += 1
	j = a[j]
for i in range((k - g[j]) % (i - g[j])):
	j = a[j]
print(j + 1)


import sys
readline = sys.stdin.readline
(N, K) = map(int, readline().split())
A = [0] + list(map(int, readline().split()))
now = 1
while K > 0:
	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())
l = list(map(int, input().split()))
now = 1
while k:
	(k, f) = divmod(k, 2)
	if f:
		now = l[now - 1]
	l = [l[l[i] - 1] for i in range(n)]
print(now)


(n, k) = list(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))
		return
print(now)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
G = [1]
for n in range(N - 1):
	G.append(A[G[n] - 1])
if len(G) < K:
	a = G.index(G[-1])
	b = len(G) - a - 1
	K = (K - a) % b + a
print(G[K])


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
B = [1]
C = [1] + [0] * (N - 1)
i = 0
while C[A[i] - 1] == 0:
	B.append(A[i])
	C[A[i] - 1] = 1
	i = A[i] - 1
ans = 0
for j in range(len(B)):
	if B[j] == A[i]:
		ans += j
if len(B) - ans != 0 and K - ans > 0:
	ans += (K - ans) % (len(B) - ans)
elif K - ans <= 0:
	ans = K
print(B[ans])


(N, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
ord = [-1 for _ in range(N)]
ord[0] = 0
v = A[0] - 1
root = [0]
while ord[v] == -1:
	ord[v] = len(root)
	root.append(v)
	v = A[v] - 1
l = ord[v]
c = len(root) - l
if K < l:
	ans = root[K]
else:
	K -= l
	K %= c
	ans = root[l + K]
print(ans + 1)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
s = []
d = [-1] * (N + 1)
v = 1
(c, l) = (1, 0)
while d[v] == -1:
	d[v] = len(s)
	s.append(v)
	v = A[v - 1]
c = len(s) - d[v]
l = d[v]
if K < l:
	print(s[K])
else:
	K -= l
	K %= c
	print(s[l + K])


(N, K) = map(int, input().split())
a = list(map(int, input().split()))
s = []
ord = [-1] * (N + 1)
c = 1
l = 0
v = 1
while ord[v] == -1:
	ord[v] = len(s)
	s.append(v)
	v = a[v - 1]
c = len(s) - ord[v]
l = ord[v]
if K < l:
	print(s[K])
else:
	K -= l
	K %= c
	print(s[l + K])


(n, k) = map(int, input().split())
a = [[int(x) - 1 for x in input().split()]]
for i in range(1, k.bit_length()):
	ith = [a[-1][a[-1][i]] for i in range(n)]
	a.append(ith)
ans = 0
for i in range(k.bit_length()):
	if k >> i & 1:
		ans = a[i][ans]
print(ans + 1)


def f():
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	now = 1
	for d in range(k.bit_length()):
		(k, f) = divmod(k, 2)
		if f:
			now = l[now - 1]
		l = tuple((l[i - 1] for i in l))
	print(now)
f()


(n, k) = map(int, input().split())
ls = list(map(lambda x: int(x) - 1, input().split()))
ved = [-1] * n
ved[0] = 0
cur = 0
i = k
while i > 0:
	cur = ls[cur]
	i -= 1
	if ved[cur] >= 0:
		i = i % (k - i - ved[cur])
	else:
		ved[cur] = k - i
print(cur + 1)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
a = [i - 1 for i in a]
v = [-1 for _ in range(n)]
p = 0
x = 0
c = 0
while k > 0 and v[x] < 0:
	v[x] = p + 1
	p = v[x]
	x = a[x]
	c += 1
	k -= 1
z = c - v[x] + 1
k = k % z
while k > 0:
	k -= 1
	x = a[x]
print(x + 1)


(n, k) = map(int, input().split())
A = list(map(int, input().split()))
L = [1]
S = {1}
while A[L[-1] - 1] not in S:
	last = L[-1] - 1
	L.append(A[last])
	S |= {A[last]}
a = A[L[-1] - 1]
j = L.index(a)
r = len(L)
l = r - j
if k + 1 <= j:
	print(L[k])
else:
	print(L[j + (k - j) % l])


def delete_head_zeros(n):
	n = str(n)
	l = len(n)
	if '.' in n:
		l = n.find('.')
	head_zeros = 0
	for i in range(l - 1):
		if n[i] == '0':
			head_zeros += 1
		else:
			break
	return n[head_zeros:]

def bigint_compare(a, b):
	a = delete_head_zeros(a)
	b = delete_head_zeros(b)
	if len(a) > len(b):
		return 1
	elif len(a) < len(b):
		return -1
	elif a > b:
		return 1
	elif a < b:
		return -1
	else:
		return 0

def bigint_plus(a, b):
	a = str(a)
	b = str(b)
	d = max([len(a), len(b)])
	a = '0' * (d - len(a)) + a
	b = '0' * (d - len(b)) + b
	ans = ''
	carry = 0
	for i in range(d):
		s = int(a[-i - 1]) + int(b[-i - 1]) + carry
		carry = s // 10
		ans = str(s % 10) + ans
	else:
		if carry:
			ans = str(carry) + ans
	return ans

def bigint_minus(a, b):
	a = str(a)
	b = str(b)
	M = []
	m = []
	sign = ''
	if len(a) > len(b) or (len(a) == len(b) and a >= b):
		[M, m] = [a, b]
	else:
		[M, m] = [b, a]
		sign = '-'
	m = '0' * (len(M) - len(m)) + m
	ans = ''
	borrow = 0
	for i in range(len(M)):
		s = int(M[-i - 1]) - int(m[-i - 1]) - borrow
		if s < 0:
			borrow = 1
			s += 10
		else:
			borrow = 0
		ans = str(s) + ans
	return sign + delete_head_zeros(ans)

def bigint_multiply(a, b):
	a = str(a)
	b = str(b)
	md = []
	for j in range(len(b)):
		carry = 0
		mj = ''
		for i in range(len(a)):
			m = int(a[-i - 1]) * int(b[-j - 1]) + carry
			carry = m // 10
			mj = str(m % 10) + mj
		else:
			if carry:
				mj = str(carry) + mj
		md.append(mj)
	ans = 0
	for k in range(len(md)):
		ans = bigint_plus(md[k] + '0' * k, ans)
	return ans

def bigint_divide(a, b, d=0):
	a = str(a)
	b = str(b)
	d = int(d)
	if d < 0:
		d = 0
	ans = ''
	r = ''
	for i in range(len(a) + d):
		q = 0
		if i < len(a):
			r += a[i]
		elif i == len(a):
			ans += '.'
			r += '0'
		else:
			r += '0'
		if bigint_compare(r, b) == -1:
			ans += str(q)
		else:
			while bigint_compare(r, b) >= 0:
				r = bigint_minus(r, b)
				q += 1
			ans += str(q)
	return delete_head_zeros(ans)

def main():
	(N, K) = input().split(' ')
	A = [int(a) - 1 for a in input().split(' ')]
	visited = [0] * int(N)
	towns = []
	now = 0
	while visited[now] == 0:
		towns.append(now)
		visited[now] = 1
		now = A[now]
	else:
		loop_begin_idx = towns.index(now)
		loop_end_idx = len(towns) - 1
		loop_n = loop_end_idx - loop_begin_idx + 1
	if int(K) < len(towns):
		print(towns[int(K)] + 1)
		return 0
	K_from_loop = bigint_minus(K, loop_begin_idx)
	q = bigint_divide(K_from_loop, loop_n)
	r = bigint_minus(K_from_loop, bigint_multiply(loop_n, q))
	print(towns[loop_begin_idx + int(r)] + 1)
main()


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
count = [-1] * (N + 1)
pos = 1
num = 0
while count[pos] == -1:
	count[pos] = num
	pos = A[pos - 1]
	num += 1
loop = max(count) - count[pos] + 1
if num > K:
	print(count.index(K))
elif loop == 1 and num <= K:
	print(count.index(max(count)))
else:
	start = num - loop
	ans = (K - num) % loop
	print(count.index(start + ans))


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
s = 1
q = [0] * n
q[0] = 1
r = [1]
for i in range(k):
	s = l[s - 1]
	if q[s - 1] == 0:
		q[s - 1] += 1
		r.append(s)
		if i == k - 1:
			print(s)
	else:
		x = r.index(s)
		mod = len(r) - x
		t = (k - x) % mod
		print(r[x + t])
		break


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


(N, K) = map(int, input().split())
A = list(map(lambda c: int(c) - 1, input().split()))
V = [-1] * N
i = 0
l = 0
while True:
	if V[i] != -1:
		break
	V[i] = l
	i = A[i]
	l += 1
	if l == K:
		print(i + 1)
		raise SystemExit
cyc = l - V[i]
K -= V[i]
K %= cyc
for _ in range(K):
	i = A[i]
print(i + 1)


(n, k, *A) = map(int, open(0).read().split())
B = set()
C = []
p = 0
while True:
	p = A[p] - 1
	if p in B:
		C.append(p)
		break
	B.add(p)
	C.append(p)
x = C.index(p)
if k - 1 < x:
	print(C[k - 1] + 1)
else:
	D = C[x:len(C) - 1]
	print(D[(k - 1 - x) % len(D)] + 1)


(N, K) = map(int, input().split())
A = [int(x) - 1 for x in input().split()]
dist = {0: 0}
t = i = 0
while K > 0:
	K -= 1
	i += 1
	t = A[t]
	if t in dist:
		K %= i - dist[t]
	else:
		dist[t] = i
print(t + 1)


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) = list(map(int, input().split()))
A = list([int(x) - 1 for x in 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)


from collections import deque

def solve(A, K):
	a = deque()
	seen = [False for _ in range(len(A))]
	cur = 0
	while True:
		if seen[cur]:
			while a[0] != cur:
				K -= 1
				a.popleft()
				if K == 0:
					return a[0] + 1
			break
		a.append(cur)
		seen[cur] = True
		cur = A[cur]
	return a[K % len(a)] + 1
(N, K) = map(int, input().split())
A = list(map(int, input().split()))
A = [v - 1 for v in A]
print(solve(A, K))


from collections import defaultdict
(N, K) = map(int, input().split())
A = [int(i) for i in input().split()]
used_dict = defaultdict(int)
used_dict[0] += 1
now_town = 0
flag = False
for i in range(min(N, K)):
	now_town = A[now_town] - 1
	if used_dict[now_town]:
		used_dict[now_town] += 1
		break
	used_dict[now_town] += 1
else:
	flag = True
if not flag:
	now_town2 = 0
	cnt = 0
	while 1:
		if now_town2 == now_town:
			for _ in range((K - cnt) % (i - cnt + 1)):
				now_town = A[now_town] - 1
			break
		now_town2 = A[now_town2] - 1
		cnt += 1
print(now_town + 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 = [int(v) - 1 for v in input().split()]
p = 0
memo = {}
for i in range(K):
	if p in memo:
		interval = i - memo[p]
		rest = (K - i - 1) % interval
		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) = list(map(int, input().split()))
A = list([int(x) - 1 for x in input().split()])
cnts = [None] * N
pos = 0
cnt = 0
while cnt < K:
	if cnts[pos] != None:
		loop_size = cnt - cnts[pos]
		cnt += (K - cnt - 1) // loop_size * loop_size
		cnts = [None] * N
	cnts[pos] = cnt
	pos = A[pos]
	cnt += 1
print(pos + 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(v.index(min(k, l + (k - l) % (r - l))) + 1)


def main():
	(N, K) = map(int, input().split())
	A = [0] + list(map(int, input().split()))
	i = 1
	pas = [i]
	pas_set = set(pas)
	k = 1
	while k <= K:
		if A[i] in pas_set:
			rps = pas.index(A[i])
			ans = A[i]
			break
		pas.append(A[i])
		pas_set.add(A[i])
		ans = A[i]
		i = A[i]
		k += 1
	if k >= K:
		print(ans)
	else:
		rpnum = (K - rps) % (k - rps)
		print(pas[rps + rpnum])
main()


(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 = list(map(int, input().split()))
l = [1]
for i in range(n):
	l.append(a[l[i] - 1])
p = l.index(l[-1])
if k <= n:
	print(l[k])
	exit()
x = (k - n + 1) % (n - p)
if x == 0:
	print(l[n - 1])
else:
	print(l[x + p - 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) = [int(i) for i in input().split()]
AS = [int(i) for i in input().split()]
visited = set([1])
cnt = 0
t = [1]
p = 1
while AS[p - 1] not in visited and cnt < K:
	a = AS[p - 1]
	visited.add(a)
	t.append(a)
	p = a
	cnt += 1
if cnt == K:
	print(t[-1])
	import sys
	return
f_cnt = t.index(AS[p - 1])
loop_cnt = cnt + 1 - f_cnt
print(t[f_cnt + (K - f_cnt) % loop_cnt])


(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(len(A))]
	K >>= 1
print(c)


def f(x):
	return int(x) - 1
(n, k) = map(int, input().split())
A = list(map(f, input().split()))
B = [-1] * n
a = 0
i = 0
while True:
	if B[a] != -1:
		j = B[a]
		break
	i += 1
	B[a] += i
	a = A[a]
if k < i:
	print(B.index(k) + 1)
else:
	k = (k - j) % (i - j) + j
	print(B.index(k) + 1)


(n, k, *a) = map(int, open(0).read().split())
a = [None] + a
done = [None] + [0] * n
t = 0
p = 1
while not done[p]:
	done[p] = t
	p = a[p]
	t += 1
loop = t - done[p]
orig = done[p]
if k - orig > 0:
	k = (k - orig) % loop + orig
p = 1
while k:
	p = a[p]
	k -= 1
print(p)


import bisect, collections, copy, itertools, math, string
import sys

def I():
	return int(sys.stdin.readline().rstrip())

def LI():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def S():
	return sys.stdin.readline().rstrip()

def LS():
	return list(sys.stdin.readline().rstrip().split())

def main():
	(n, k) = LI()
	a = [None] + LI()
	explored = {1}
	lst = [1]
	lenth = 0
	start = 0
	junkan = []
	now = 1
	while lenth == 0:
		nxt = a[now]
		if nxt in explored:
			index = lst.index(nxt)
			junkan = [lst[-1]] + lst[index:-1]
			start = index
			lenth = len(junkan)
		else:
			explored.add(nxt)
			lst.append(nxt)
		now = nxt
	ans = 0
	if k < start:
		ans = lst[k]
	else:
		r = (k - start + 1) % lenth
		ans = junkan[r]
	print(ans)
main()


from collections import deque
import sys
(n, k) = map(int, input().split())
A = list(map(int, input().split()))
A = list(map(lambda x: x - 1, A))
seen = [0] * n
li = deque()
pos = 0
while True:
	if seen[pos]:
		while li[0] != pos:
			li.popleft()
			k -= 1
			if k == 0:
				print(li[0] + 1)
				return
		break
	li.append(pos)
	seen[pos] = 1
	pos = A[pos]
print(li[k % len(li)] + 1)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
b = 0
s = a[0]
q = 1
f = [1, a[0]]
d = set(f)
for _ in range(n):
	s = a[s - 1]
	if s in d:
		break
	else:
		f.append(s)
		d.add(s)
g = f.index(s)
t = len(f[g:])
if k < len(f) - t:
	print(f[k])
else:
	u = (k + t - len(f)) % t
	print(f[g:][u])


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
alist = []
cnt = 0
i = 1
blist = [0] * (n + 1)
while blist[a[i - 1]] == 0:
	i = a[i - 1]
	alist.append(i)
	blist[i] = 1
	cnt += 1
loop1 = alist.index(a[i - 1])
if k < loop1 + 1:
	print(alist[k - 1])
else:
	print(alist[(k - loop1 - 1) % (cnt - loop1) + loop1])


def main():
	(n, k) = map(int, input().split())
	a = [int(i) - 1 for i in input().split()]
	visit = [False] * n
	visit[0] = True
	(loop, loopstart, loopout, cur) = (0, -1, False, 0)
	i = 0
	while i < k:
		cur = a[cur]
		if not loopout and visit[cur]:
			if loopstart == cur:
				k = (k - sum(visit)) % loop
				loopout = True
				i = 0
				continue
			elif loopstart == -1:
				loopstart = cur
			loop += 1
		else:
			visit[cur] = True
		i += 1
	print(cur + 1)

def __starting_point():
	main()
__starting_point()


(n, k) = [int(x) for x in input().split()]
p = [int(x) - 1 for x in input().split()]
alp = [0 for _ in range(n)]
a = [0]
i = 0
alp[0] = 1
while alp[p[i]] == 0:
	a.append(p[i])
	alp[p[i]] = 1
	i = p[i]
l = a.index(p[i])
if k <= l:
	print(1 + a[k])
else:
	print(1 + a[l + (k - l) % (len(a) - l)])


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
b = [0] * (n + 1)
count = 0
now = 1
f = 1
for _ in range(k):
	if b[now]:
		f = 0
		break
	count += 1
	b[now] = count
	now = a[now - 1]
if f:
	print(now)
	return
x = count - b[now] + 1
y = (k - b[now] + 1) % x
for i in range(y):
	now = a[now - 1]
print(now)


(N, K) = (int(i) for i in input().split())
A = [int(i) for i in input().split()]
V = [-1] * N
W = [-1] * N
x = 0
V[0] = 0
W[0] = 0
for i in range(1, K + 1):
	x = A[x] - 1
	W[i] = x
	if V[x] == -1:
		V[x] = i
	else:
		x = W[V[x] + (K - i) % (i - V[x])]
		break
print(x + 1)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
e = [False] * (N + 1)
idx = 1
move = []
while True:
	if e[idx]:
		break
	move.append(idx)
	e[idx] = True
	idx = A[idx - 1]
m = move.index(idx)
c = len(move) - m
if K <= m:
	print(move[K])
else:
	print(move[m + (K - m) % c])


import numpy as np

def main():
	(n, k) = list(map(int, input().split()))
	warps = list(map(int, input().split()))
	warps = [0] + warps
	order_num = [0 for i in range(len(warps))]
	path_history = []
	node = 1
	while order_num[node] == 0:
		path_history.append(node)
		order_num[node] = len(path_history)
		node = warps[node]
	begin = order_num[node]
	if k <= begin:
		print(path_history[k])
	else:
		leng = len(path_history) - order_num[node] + 1
		index = (k - (begin - 1)) % leng
		print(path_history[begin - 1 + index])
main()


def __starting_point():
	(n, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	start = 1
	loop_start = 0
	loop_end = 0
	steps = []
	steps_map = {}
	steps.append(start)
	cur = start
	for i in range(n):
		cur = a[cur - 1]
		if cur in steps_map:
			loop_start = steps_map[cur]
			loop_end = i + 1
			break
		else:
			steps.append(cur)
			steps_map[cur] = i + 1
	if k <= loop_start:
		print(steps[k])
	else:
		temp = (k - loop_start) % (loop_end - loop_start)
		print(steps[temp + loop_start])
__starting_point()


(n, k) = map(int, input().split())
a = [[0] * n for _ in range(100)]
A = list(map(int, input().split()))
for (i, j) in enumerate(A):
	a[0][i] = j - 1
i = 1
while 2 ** i < k:
	for (x, y) in enumerate(a[i - 1]):
		a[i][x] = a[i - 1][y]
	i += 1
x = 0
for j in range(i):
	if k >> j & 1:
		x = a[j][x]
print(x + 1)


from collections import defaultdict

def main():
	(n, k) = map(int, input().split(' '))
	a = list(map(int, input().split(' ')))
	s = []
	ord = [-1] * (n + 1)
	u = 1
	while ord[u] == -1:
		ord[u] = len(s)
		s.append(u)
		u = a[u - 1]
	l = len(s) - ord[u]
	if k < ord[u]:
		print(s[k])
	else:
		print(s[(k - ord[u]) % l + ord[u]])

def __starting_point():
	main()
__starting_point()


(N, K) = map(int, input().split())
A = [int(x) for x in input().split()]
visited = [-1] * N
loop = list()
cur = 1
cnt = 0
while visited[cur - 1] == -1:
	visited[cur - 1] = cnt
	loop.append(cur)
	cnt += 1
	cur = A[cur - 1]
	if cnt == K:
		print(cur)
		break
else:
	loop = loop[visited[cur - 1]:]
	T = cnt - visited[cur - 1]
	ans = loop[(K - cnt) % T]
	print(ans)


(N, K) = map(int, input().split())
A = list(map(lambda x: int(x) - 1, input().split()))
db = [[0] * N for _ in range(100)]
db[0] = A[:]
for i in range(1, 100):
	for j in range(N):
		db[i][j] = db[i - 1][db[i - 1][j]]
ans = p = 0
while K > 0:
	if K % 2 == 1:
		ans = db[p][ans]
	p += 1
	K >>= 1
print(ans + 1)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
t = [[0] * n for i in range(60)]
for i in range(n):
	t[0][i] = a[i] - 1
for i in range(1, 60):
	for j in range(n):
		t[i][j] = t[i - 1][t[i - 1][j]]
now = 0
for i in range(60):
	if k >> i & 1:
		now = t[i][now]
print(now + 1)


import sys
readline = sys.stdin.readline
(N, K) = map(int, readline().split())
A = [0] + list(map(int, readline().split()))
c = 1
while K:
	if K & 1:
		c = A[c]
	na = [0] * (N + 1)
	for i in range(N + 1):
		na[i] = A[A[i]]
	(na, A) = (A, na)
	K >>= 1
print(c)


(N, K) = map(int, input().split())
cc = list(map(int, input().split()))
memo = []
aa = ['.'] * N
a = 1
while aa[a - 1] == '.':
	memo.append(a)
	aa[a - 1] = 'v'
	a = cc[a - 1]
if len(memo) > K:
	print(memo[K])
else:
	b = memo.index(a)
	m = len(memo) - b
	mm = (K - b) % m
	del memo[:b]
	print(memo[mm])


(a, b) = map(int, input().split())
c = [0] + list(map(int, input().split()))
d = set()
e = []
mir = 1
while 1:
	if mir in d:
		g = e.index(mir)
		f = e[g:]
		e = e[:g]
		break
	else:
		d.add(mir)
		e.append(mir)
		mir = c[mir]
if b < len(e):
	print(e[b])
else:
	b = b - len(e)
	print(f[b % len(f)])


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
b = 1
c = [0] * n
c[0] = 1
m = 0
while c[b - 1] < 2 and k > 0:
	b = a[b - 1]
	c[b - 1] += 1
	k -= 1
while c[b - 1] < 3 and k > 0:
	b = a[b - 1]
	c[b - 1] += 1
	m += 1
if k == 0:
	print(b)
else:
	o = k % m
	for i in range(o):
		b = a[b - 1]
	print(b)


(n, k) = map(int, input().split())
now = 0
a = list(map(int, input().split()))
s = []
od = [-1] * (n + 1)
c = 1
l = 0
v = 1
while od[v] == -1:
	od[v] = len(s)
	s.append(v)
	v = a[v - 1]
c = len(s) - od[v]
l = od[v]
if k < l:
	print(s[k])
else:
	k -= l
	k %= c
	print(s[l + k])


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
c = 0
d = {}
d[1] = 0
dd = {}
dd[0] = 1
now = 1
while True:
	now = a[now - 1]
	c += 1
	if now in d:
		break
	d[now] = c
	dd[c] = now
s = d[now]
if k <= s:
	print(dd[k])
	exit()
p = c - s
k -= s
k %= p
k += s
print(dd[k])


(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
if k < len(t):
	ans = t[k]
elif (k - c + 1) % r == 0:
	ans = t[-1]
else:
	ans = t[(k - c + 1) % r + c - 1]
print(ans)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
dic = {}
for (i, v) in enumerate(A):
	dic[i + 1] = v
town = 1
s = set()
l = []
for _ in range(K):
	l.append(town)
	s.add(town)
	town = dic[town]
	if town in s:
		stop_twon = town
		break
if N <= K:
	list_first_split = l[:l.index(stop_twon)]
	list_second_split = l[l.index(stop_twon):]
	print(list_second_split[(K - len(list_first_split)) % len(list_second_split)])
else:
	print(town)


import sys
from itertools import chain

def solve(N: int, K: int, A: 'List[int]'):
	M = [-1 for _ in range(N)]
	M[0] = 0
	current = 1
	n = 1
	while n <= K:
		current = A[current - 1]
		if M[current - 1] == -1:
			M[current - 1] = n
		else:
			loop_len = n - M[current - 1]
			rest = K - n
			rest = rest % loop_len
			K = n + rest
		n += 1
	return current

def main():
	tokens = chain(*(line.split() for line in sys.stdin))
	N = int(next(tokens))
	K = int(next(tokens))
	A = [int(next(tokens)) for _ in range(N)]
	answer = solve(N, K, A)
	print(answer)

def __starting_point():
	main()
__starting_point()


(N, K) = map(int, input().split())
lsA = [0] + list(map(int, input().split()))
ls = []
set1 = set()
a = 1
for i in range(N + 1):
	if a in set1:
		loop = ls[ls.index(a):]
		lenloop = len(loop)
		startls = ls[:ls.index(a)]
		break
	ls.append(a)
	set1.add(a)
	a = lsA[a]
if K < len(startls):
	ans = startls[K]
else:
	ans = loop[(K - len(startls)) % lenloop]
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
b = 0
if k <= n:
	for i in range(k):
		b = a[b] - 1
	print(b + 1)
	exit(0)
c = [0]
for i in range(n):
	b = a[b] - 1
	c.append(b)
d = c[:-1].index(c[-1])
c = c[d:-1]
k = k - d
print(c[k % len(c)] + 1)


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


(n, k) = map(int, input().split())
a = list(map(lambda s: int(s) - 1, input().split()))
cur = 0
while k > 0:
	if k % 2 == 1:
		cur = a[cur]
	next = [a[a[i]] for i in range(n)]
	a = next
	k //= 2
print(cur + 1)


(n, k) = map(int, input().split())
b = list(map(int, input().split()))
v = [None] * n
w = [0] * n
(i, c) = (0, b[0])
while v[c - 1] is None:
	w[i] = c
	v[c - 1] = i
	i += 1
	c = b[c - 1]
head = v[c - 1]
loop = i - v[c - 1]
if k - 1 < head:
	print(w[k - 1])
else:
	print(w[head + (k - 1 - head) % loop])


(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)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
b = [False] * n
pos = 0
for i in range(min(n, k)):
	if b[pos] != False:
		loop = i - b[pos] + 1
		break
	b[pos] = i + 1
	pos = a[pos] - 1
	k -= 1
if k > 0:
	for i in range(k % loop):
		pos = a[pos] - 1
print(pos + 1)


import sys
(n, k, *aaa) = map(int, sys.stdin.buffer.read().split())
aaa = [0] + aaa
p = 1
while k:
	if k & 1:
		p = aaa[p]
	aaa = [aaa[a] for a in aaa]
	k >>= 1
print(p)


import numpy as np

def main():
	(N, K) = map(int, input().split())
	A = [0]
	A += list(map(int, input().split()))
	A = np.array(A)
	i = 1
	pas = np.zeros(N)
	pas[0] = i
	k = 1
	pass_set = set(pas)
	while k <= K:
		if A[i] in pass_set:
			rps = np.where(pas == A[i])[0]
			ans = A[i]
			break
		pas[k] = A[i]
		pass_set.add(A[i])
		ans = A[i]
		i = A[i]
		k += 1
	if k >= K:
		print(ans)
	else:
		rpnum = (K - rps[0]) % (k - rps)
		print(int(pas[rps[0] + rpnum]))
main()


(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()))
check = [0] * (N + 1)
li = []
(i, j) = (1, 1)
while True:
	k = A[i - 1]
	if check[k] == 1:
		n = li.index(k) + 1
		break
	check[k] = 1
	li.append(k)
	i = k
	j += 1
if K < n:
	print(li[K - 1])
else:
	print(li[(K - n) % (j - n) + n - 1])


(n, k) = map(int, input().split())
A = list(map(int, input().split()))
s = []
order = [-1] * (n + 1)
l = 1
while order[l] == -1:
	order[l] = len(s)
	s.append(l)
	l = A[l - 1]
new = s[order[l]:]
t = len(new)
first = len(s) - t
if k > first:
	print(new[(k - first) % t])
else:
	print(s[k])


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
v = [0] * n
v[0] = 1
i = 1
l = [1]
while True:
	i = a[i - 1]
	l.append(i)
	if v[i - 1]:
		t = i
		break
	else:
		v[i - 1] = 1
pre = l.index(t)
loop = len(l) - pre - 1
if pre >= k:
	print(l[k])
else:
	k = k - pre
	mod = k % loop
	print(l[pre + mod])


(n, d) = map(int, input().split())
x = [0] + list(map(int, input().split()))
N = n + 1
y = list(range(N))
while d:
	if d & 1:
		y = [x[y[i]] for i in range(N)]
	x = [x[x[i]] for i in range(N)]
	d >>= 1
res = [0] * N
print(y[1])


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
result = [1]
temp = 1
for i in range(n):
	temp = a[temp - 1]
	result.append(temp)
temp = result.index(result[len(result) - 1])
c = len(result) - temp - 1
if k < n:
	print(result[k])
else:
	print(result[temp + (k - temp) % c])


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = [-1] * n
c = []
ans = 0
for i in range(k):
	s[ans] = i
	c.append(ans)
	ans = a[ans] - 1
	if s[ans] >= 0:
		t = i - s[ans] + 1
		ans = c[(k - i - 1) % t + s[ans]]
		break
print(ans + 1)


import sys
import numpy as np

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

def main():
	(n, k) = list(map(int, input().split()))
	warps = list(map(int, input().split()))
	warps = [0] + warps
	warps = np.array(warps, dtype=int)
	dp = np.zeros((k.bit_length() + 1, n + 1), dtype=int)
	dp[0, :] = warps
	for h in range(1, len(dp)):
		dp[h] = dp[h - 1][dp[h - 1]]
	node = 1
	for i in range(k.bit_length(), -1, -1):
		if k >> i & 1:
			node = dp[i][node]
	print(node)
main()


(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) = map(int, input().split())
Alist = list(map(int, input().split()))
already = [1]
alreadyset = {1}
last = 1
while True:
	if Alist[last - 1] in alreadyset:
		start = already.index(Alist[last - 1])
		break
	already.append(Alist[last - 1])
	alreadyset.add(Alist[last - 1])
	last = Alist[last - 1]
roop = len(already) - start
if K - len(already) < 0:
	print(already[K])
else:
	index = (K - start) % roop + start
	while not start <= index < len(already):
		index = index % roop + start
	print(already[index])


(N, K) = map(int, input().split())
A = list(map(lambda x: int(x), input().split()))
A = [a - 1 for a in A]
now = 0
while K:
	if K % 2:
		now = A[now]
	A = [A[a] for a in A]
	K //= 2
print(now + 1)


(N, K) = map(int, input().split())
A = list(map(lambda a: int(a) - 1, input().split()))
now = 0
for d in range(64):
	if 1 << d & K > 0:
		now = A[now]
	A = [A[a] for a in A]
print(now + 1)


(n, k) = [int(i) for i in input().split()]
l = [int(i) - 1 for i in input().split()]
i = 0
m = [-1] * n
p = 0
while m[i] == -1:
	if p == k:
		break
	m[i] = p
	p += 1
	i = l[i]
else:
	z = (k - m[i]) % (p - m[i])
	i = m.index(m[i] + z)
print(i + 1)


f = lambda : map(int, input().split())
(n, k) = f()
a = list(f())
lv = [-1] * n
lv[0] = 0
p = 0
for i in range(n):
	t = a[p] - 1
	if lv[t] >= 0:
		(l, r) = (lv[t], i + 1)
		break
	lv[t] = i + 1
	p = t
print(lv.index(l + (k - l) % (r - l) if k > l else k) + 1)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
l = [0] * n
i = 0
cnt = 0
while l[i] == 0:
	l[i] = a[i]
	i = a[i] - 1
	cnt += 1
start = i + 1
i = 0
pre = 0
while i + 1 != start:
	i = a[i] - 1
	pre += 1
loop = cnt - pre
if pre + loop < k:
	k = (k - pre) % loop + pre
i = 0
for _ in range(k):
	i = a[i] - 1
print(i + 1)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
d = {}
for i in range(1, n + 1):
	d[i] = 0
l = [1]
x = 1
if n >= k:
	for i in range(k):
		x = a[x - 1]
	print(x)
	return
for i in range(n):
	x = a[x - 1]
	if d[x] == 1:
		k -= l.index(x)
		print(l[l.index(x):][k % (l.index(x) - len(l))])
		return
	l.append(x)
	d[x] += 1
print(x)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = []
o = [-1] * (n + 1)
i = 1
c = 1
l = 0
while o[i] == -1:
	o[i] = len(s)
	s.append(i)
	i = a[i - 1]
c = len(s) - o[i]
l = o[i]
if k < l:
	print(s[k])
else:
	k -= l
	k %= c
	print(s[l + k])


(N, K) = list(map(int, input().split()))
l = list(map(int, input().split()))
roop = [1]
s = set([1])
now = l[0]
while now not in s:
	roop.append(now)
	s.add(now)
	now = l[now - 1]
start = roop.index(now)
if len(roop) <= K:
	K -= start
	K = K % (len(roop) - start)
	print(roop[start + K])
else:
	print(roop[K])


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
t_zero = [-1] * N
t = 0
a = 1
while t_zero[a - 1] == -1:
	if t == K:
		break
	t_zero[a - 1] = t
	t += 1
	a = A[a - 1]
else:
	k = (K - t_zero[a - 1]) % (t - t_zero[a - 1])
	a = t_zero.index(t_zero[a - 1] + k) + 1
print(a)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
a = list(map(lambda x: x - 1, a))
visited = [-1] * n
now = 0
for i in range(k):
	if visited[now] != -1:
		loop = i - visited[now]
		k = (k - (i - loop)) % loop
		for _ in range(k):
			now = a[now]
		break
	visited[now] = i
	now = a[now]
print(now + 1)


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())
t = list(map(int, input().split()))
g = [1]
h = {1}
for i in range(N - 1):
	x = t[g[i] - 1]
	g.append(x)
	if x in h:
		break
	h.add(x)
if K > len(g):
	a = g.index(g[-1])
	b = len(g) - a - 1
	K = (K - a) % b + a
print(g[K])


def input2():
	return map(int, input().split())

def input_array():
	return list(map(int, input().split()))
(n, k) = input2()
A = input_array()
judge = [True] * n
tmps = [1]
count = 0
for i in range(2 * 10 ** 5 + 1):
	if judge[count] == True:
		judge[count] = False
		count = A[count] - 1
		tmps.append(count + 1)
	else:
		break
loop_st = tmps.index(tmps[-1])
loops = tmps[loop_st:-1]
if k < loop_st:
	print(tmps[k])
else:
	k = k - loop_st
	k %= len(loops)
	print(loops[k])


(N, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
visited = [0 for _ in range(N)]
first_visit = [0 for _ in range(N)]
now = 0
flag = True
for i in range(10 ** 5 * 5):
	if first_visit[now] == 0:
		first_visit[now] = i
	visited[A[now] - 1] += 1
	now = A[now] - 1
	if i == K - 1:
		print(now + 1)
		flag = False
		break
if flag:
	num = 0
	for i in range(N):
		if visited[i] > 2:
			num += 1
	for i in range(N):
		if visited[i] >= 2:
			if K % num == first_visit[i] % num:
				print(i + 1)
				break


def main():
	(n, k) = map(int, input().split())
	inlis = list(map(int, input().split()))
	flag = 0
	trans = 0
	now = 1
	indic = dict()
	indic[1] = 0
	while flag < 1:
		if trans < k:
			trans += 1
			tsugi = inlis[now - 1]
			now = tsugi
			if tsugi not in indic:
				indic[tsugi] = trans
			else:
				flag = 1
				loop = trans - indic[tsugi]
				yokei = indic[tsugi]
		if trans == k:
			print(tsugi)
			return
	loopamari = (k - yokei) % loop
	for _ in range(loopamari):
		tsugi = inlis[now - 1]
		now = tsugi
	print(now)

def __starting_point():
	main()
__starting_point()


(n, k) = map(int, input().split())
A = list(map(int, input().split()))
c = 1
path = []
d = {}
l = 0
while l <= k:
	if c in d:
		p = d[c]
		pattern_l = l - p
		k -= (k - p) // pattern_l * pattern_l
	path.append(c)
	l += 1
	d[c] = l - 1
	c = A[c - 1]
print(path[k])


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = 1
c = 0
d = {}
t = []
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()))
i = l = 1
l = 0
F = [0] * n
L = []
while 1:
	i = A[i - 1]
	if F[i - 1] > 0:
		break
	else:
		L.append(i)
		F[i - 1] = l
		l += 1
print(L[F[i - 1]:][(k - F[i - 1]) % (l - F[i - 1]) - 1] if k > F[i - 1] else L[k - 1])


(N, K) = map(int, input().split())
s = input().split(' ')
p = 1
l = -1
dict = {}
for i in range(K):
	if s[p - 1] in dict:
		x = dict[s[p - 1]]
		l = i
		break
	else:
		dict[s[p - 1]] = i
	p = int(s[p - 1])
if l != -1:
	for i in range((K - l) % (l - x)):
		p = int(s[p - 1])
print(p)


(n, K) = list(map(int, input().split()))
A = list([int(x) - 1 for x in input().split()])
done = [-1 for _ in range(n)]
tmp = 0
done[0] = 0
for k in range(1, K + 1):
	tmp = A[tmp]
	if done[tmp] >= 0:
		for i in range((K - done[tmp]) % (k - done[tmp])):
			tmp = A[tmp]
		print(tmp + 1)
		return
	else:
		done[tmp] = k
print(tmp + 1)


(N, K, *A) = map(int, open(0).read().split())
f = [0] * N
l = 1
ff = 0
f[l - 1] = K
while K > 0:
	l = A[l - 1]
	K -= 1
	if f[l - 1] != 0:
		K = K % (f[l - 1] - K)
		break
	f[l - 1] = K
while K > 0:
	l = A[l - 1]
	K -= 1
print(l)


(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(lambda x: int(x) - 1, input().split()))
i = 0
visited = set([0])
while K > 0:
	i = A[i]
	K -= 1
	if i in visited:
		break
	visited.add(i)
if K == 0:
	print(i + 1)
	return
ls = [i]
j = i
while True:
	j = A[j]
	if j == i:
		break
	ls.append(j)
print(ls[K % len(ls)] + 1)


(N, K) = map(int, input().split())
A = list(map(int, ('0 ' + input()).split()))
c = [0] * (N + 1)
i = 1
cnt = 0
f = True
while cnt < K:
	if f and (c[A[i]] or A[i] == 1):
		loop = cnt - c[A[i]] + 1
		cnt += (K - cnt - 1) // loop * loop
		f = False
	cnt += 1
	i = A[i]
	c[i] = cnt
print(i)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = [-1] * (n + 1)
tmp = []
here = 1
count = 0
while True:
	if b[here] != -1:
		roop = count - b[here]
		if k < count:
			print(tmp[k])
		else:
			new_tmp = tmp[b[here]:]
			s = k - b[here]
			print(new_tmp[s % roop])
		break
	b[here] = count
	count += 1
	tmp.append(here)
	here = a[here - 1]


(n, k, *a) = map(int, open(0).read().split())
a = [0] + a
ans = 1
while k:
	if k & 1:
		ans = a[ans]
	a = [a[i] for i in a]
	k >>= 1
print(ans)


D = 60
f = lambda : [*map(int, input().split())]
(n, k) = f()
l = f()
t = [[0] * n for _ in range(D)]
for i in range(n):
	t[0][i] = l[i] - 1
for i in range(D - 1):
	for j in range(n):
		t[i + 1][j] = t[i][t[i][j]]
a = 0
for i in range(D):
	if k % 2:
		a = t[i][a]
	k //= 2
print(a + 1)


(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
l = [-1] * (n + 1)
tmp = []
pos = 1
cnt = 0
while 1:
	if l[pos] != -1:
		t = cnt - l[pos]
		if k < cnt:
			print(tmp[k])
		else:
			print(tmp[l[pos] + (k - cnt) % t])
		break
	l[pos] = cnt
	cnt += 1
	tmp.append(pos)
	pos = arr[pos - 1]


import math
(N, K) = map(int, input().split())
P = [i - 1 for i in map(int, input().split())]
lgk = int(math.log(K, 2))
last = P
point = 0
for j in range(lgk + 1):
	if K >> j & 1:
		point = last[point]
	res = []
	for i in last:
		res.append(last[i])
	last = res
print(point + 1)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
a = [x - 1 for x in a]
nxt = [[x for _ in range(60)] for x in a]
for j in range(1, 60):
	for i in range(n):
		nxt[i][j] = nxt[nxt[i][j - 1]][j - 1]
cur = 0
for j in range(59, -1, -1):
	if k >> j & 1:
		cur = nxt[cur][j]
print(cur + 1)


([n, k], [*a]) = [[*map(int, t.split())] for t in open(0)]
count = 0
visited = dict()
tmp = 1
while count < k:
	visited[tmp] = count
	count += 1
	tmp = a[tmp - 1]
	if tmp in visited:
		k = (k - count) % (count - visited[tmp])
		visited = {tmp: 0}
		count = 0
print(tmp)


(N, K) = map(int, input().split())
A = [int(i) - 1 for i in input().split()]
now = 0
while K:
	if K % 2:
		now = A[now]
	tmp = A[:]
	for i in range(N):
		tmp[i] = A[A[i]]
	A = tmp[:]
	K //= 2
print(now + 1)


f = lambda : map(int, input().split())
(N, K) = f()
(*A,) = list(f())
F = [-1] * (2 * 10 ** 5 + 1)
G = []
n = 1
while F[n] == -1:
	F[n] = len(G)
	G.append(n)
	n = A[n - 1]
t = len(G) - F[n]
l = F[n]
if l <= K:
	K -= l
	K %= t
	K += l
print(G[K])


(N, K) = map(int, input().split())
t = list(map(int, input().split()))
g = [1]
for i in range(N - 1):
	g.append(t[g[i] - 1])
if K > len(g):
	a = g.index(g[-1])
	b = len(g) - a - 1
	K = (K - a) % b + a
print(g[K])


(n, k) = list(map(int, input().split()))
a = [0] + list(map(int, input().split()))
journey = [1]
for i in range(2 * n + 1000):
	nxt = a[journey[-1]]
	journey.append(nxt)
if k < n + 1:
	print(journey[k])
	return
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])


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
route = [1]
for i in range(N):
	route.append(A[route[i] - 1])
prepart = route.index(route[-1])
cycle = len(route) - prepart - 1
if K > N:
	K = (K - prepart) % cycle + prepart
print(route[K])


from collections import defaultdict

def main():
	(n, k) = map(int, input().split(' '))
	a = list(map(lambda i: int(i) - 1, input().split(' ')))
	s = [0]
	d = defaultdict(lambda : 0)
	x = a[0]
	for i in range(n):
		s.append(x)
		x = a[x]
	bb = None
	for i in range(n):
		d[s[i]] += 1
		if d[s[i]] == 2:
			bb = s[i]
			break
	cc = s.index(bb)
	s[cc] = -1
	dd = s.index(bb)
	loop_len = dd - cc
	s[cc] = s[dd]
	if bb == None or k < cc:
		print(s[k] + 1)
	else:
		y = (k - cc) % loop_len
		print(s[y + cc] + 1)

def __starting_point():
	main()
__starting_point()


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
t_zero = [-1] * N
t = 0
a = 1
while t_zero[a - 1] == -1:
	if t == K:
		break
	t_zero[a - 1] = t
	t += 1
	a = A[a - 1]
else:
	k = (K - t_zero[a - 1]) % (t - t_zero[a - 1])
	for _ in range(k):
		a = A[a - 1]
print(a)


(n, k) = map(int, input().split())
alist = list(map(int, input().split()))
memo = []
p = 1
vis = ['.'] * n
while vis[p - 1] == '.':
	memo.append(p)
	vis[p - 1] = 'v'
	p = alist[p - 1]
loop = memo.index(p)
if k < len(memo):
	print(memo[k])
else:
	print(memo[loop + (k - loop) % (len(memo) - loop)])


(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)
			return
repeat = visited[curr] - k
k = k % repeat
while k:
	curr = a[curr]
	k -= 1
print(curr)


url = 'https://atcoder.jp/contests/abc162/tasks/abc162_d'
import itertools

def main():
	(n, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	route = []
	istuka = [0] * (n + 1)
	next = 1
	for _ in range(k):
		if istuka[next] == 1:
			break
		else:
			istuka[next] = 1
			route.append(next)
		next = a[next - 1]
	if n >= k:
		print(next)
		return
	ans = route[route.index(next):]
	idx = (k - route.index(next)) % len(ans)
	print(ans[idx])

def __starting_point():
	main()
__starting_point()


(N, K) = map(int, input().split())
A = tuple(map(int, input().split()))
L = [0 for i in range(N + 1)]
L[1] = 1
x = 1
t = 1
while K > 0:
	t += 1
	K -= 1
	x = A[x - 1]
	if L[x] != 0:
		p = t - L[x]
		K %= p
		break
	else:
		L[x] = t
while K > 0:
	x = A[x - 1]
	K -= 1
print(x)


(N, K) = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = [-1] * -~N
p = 1
for i in range(K):
	if B[p] >= 0:
		break
	B[p] = i
	p = A[p]
else:
	print(p)
	return
print(B.index((K - B[p]) % (i - B[p]) + B[p]))


(n, m) = map(int, input().split())
a = [[0] + list(map(int, input().split()))]
for k in range(62):
	a.append([a[k][a[k][i]] for i in range(n + 1)])
res = 1
for k in list(range(62))[::-1]:
	if m >= 2 ** k:
		res = a[k][res]
		m -= 2 ** k
print(res)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
visited = [0 for i in range(N + 1)]
p = 1
path = [p]
visited[p] = 1
p = A[p - 1]
while visited[p] == 0:
	path.append(p)
	visited[p] = 1
	p = A[p - 1]
i = path.index(p)
if K < i:
	print(path[K])
else:
	K -= i
	print(path[i + K % (len(path) - i)])


(N, K) = map(int, input().split())
A = [0] + list(map(int, input().split()))
now = 1
path = [1]
for i in range(N + 2):
	now = A[now]
	path.append(now)
if i >= K:
	print(path[K])
	return
now = path.pop()
lo_start = i - 1
while path[lo_start] != now:
	lo_start -= 1
loop = path[lo_start:]
ans = loop[(K - path.index(now)) % len(loop)]
print(ans)


from copy import deepcopy

def main():
	(n, k) = map(int, input().split())
	a = [int(x) - 1 for x in input().split()]
	visited = set()
	i = 0
	cnt = 0
	while True:
		if i in visited:
			break
		visited.add(i)
		cnt += 1
		i = a[i]
		if cnt == k:
			print(i + 1)
			return
	k -= cnt
	period = list()
	period.append(i)
	i = a[i]
	while True:
		if i == period[0]:
			break
		period.append(i)
		i = a[i]
	mod = k % len(period)
	ans = period[mod]
	print(ans + 1)

def __starting_point():
	main()
__starting_point()


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
c = [0 for i in range(n)]
l = 1
u = 0
f = True
while True:
	u += 1
	l = a[l - 1]
	if u == k:
		print(l)
		f = False
	if c[l - 1]:
		break
	else:
		c[l - 1] = u
k = (k - c[l - 1]) % (u - c[l - 1])
while k:
	k -= 1
	l = a[l - 1]
if f:
	print(l)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
B = [0] * N
point = 1
(m, l) = (0, 0)
if K > N:
	for i in range(N):
		if B[point - 1] == 0:
			B[point - 1] = i + 1
			point = A[point - 1]
		else:
			m = i + 1 - B[point - 1]
			l = B[point - 1]
			break
	if m != 0:
		m = (K - l) % m
	else:
		m = N
else:
	m = K
ans = 1
for i in range(l + m):
	ans = A[ans - 1]
print(ans)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
c = 0
List = []
check = [-1] * N
tmp = 1
for i in range(min(N + 1, K)):
	tmp2 = A[tmp - 1]
	List.append(tmp2)
	if check[tmp2 - 1] >= 0:
		tmp3 = check[tmp2 - 1]
		del List[-1]
		break
	else:
		check[tmp2 - 1] = i
		tmp = tmp2
		c += 1
if c >= K:
	print(List[K - 1])
else:
	List2 = List[tmp3:]
	if c == len(List2):
		print(List2[K % len(List2) - 1])
	else:
		K -= c
		print(List2[K % len(List2) - 1])


(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]
	a = [a[ai] for ai in a]
	k >>= 1
print(ans + 1)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
x = 0
t = 0
d = [0] * N
v = [-1] * N
while t == 0 or x != 0:
	v[x] = t
	t += 1
	if v[A[x] - 1] >= 0:
		break
	x = A[x] - 1
	d[t] = x
c = t - v[A[x] - 1]
print(d[K] + 1 if K < t else d[(K - t) % c + v[A[x] - 1]] + 1)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
visited = []
o = (n + 1) * [-1]
(cycle, l) = (1, 0)
v = 1
while o[v] == -1:
	o[v] = len(visited)
	visited.append(v)
	v = a[v - 1]
cycle = len(visited) - o[v]
l = o[v]
if k < l:
	print(visited[k])
else:
	k -= l
	k %= cycle
	print(visited[l + k])


((N, K), A) = [[*map(int, line.split())] for line in open(0)]
kiseki = [0] * N
now = 0
for i in range(N):
	kiseki[i] = now
	now = A[now] - 1
p = kiseki.index(now)
ans = p + (K - p) % (N - p) if p < K else K
print(kiseki[ans] + 1)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
t = 0
count = 0
visited = [0] * n
l_count = 0
for _ in range(k):
	visited[t] += count
	count += 1
	t = a[t] - 1
	if visited[t] != 0:
		break
k = (k - count) % (count - visited[t])
for _ in range(k):
	t = a[t] - 1
print(t + 1)


(n, k) = map(int, input().split())
A = list(map(int, input().split()))
a = [0] * n
a[0] = 1
nxt = A[0] - 1
i = 2
while 1:
	a[nxt] = i
	nxt = A[nxt] - 1
	i += 1
	if a[nxt] != 0:
		g = i - a[nxt]
		t = a[nxt] - 1
		break
if t >= k:
	print(a.index(k + 1) + 1)
else:
	k -= t
	u = k % g
	print(a.index(t + u + 1) + 1)


(n, k) = map(int, input().split())
lt = [0] + [int(x) for x in input().split()]
h = {1}
g = [1]
for _ in range(n):
	x = lt[g[-1]]
	g.append(x)
	if x in h:
		break
	h.add(x)
if k > len(g):
	i = g.index(g[-1])
	b = len(g) - i - 1
	k = (k - i) % b + i
print(g[k])


(N, K, *A) = map(int, open(0).read().split())
a = 1
while K > 0:
	if K & 1:
		a = A[a - 1]
	A = [A[A[i] - 1] for i in range(N)]
	K >>= 1
print(a)


import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
import numpy as np

def main():
	(n, k, *a) = map(int, read().split())
	cur = 1
	path = [0] * (n + 1)
	while k:
		if path[cur]:
			break
		else:
			path[cur] = k
			cur = a[cur - 1]
			k -= 1
			if k == 0:
				print(cur)
				return
	cycle = path[cur] - k
	k = k % cycle
	while k:
		cur = a[cur - 1]
		k -= 1
	print(cur)

def __starting_point():
	main()
__starting_point()


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
l = [1]
seen = {}
k = 1
for i in range(K):
	if not k in seen.keys():
		seen[k] = i
		k = A[k - 1]
		l.append(k)
	else:
		r_start = seen[k]
		roop = i - r_start
		K = (K - r_start) % roop + r_start
		break
ans = l[K]
print(ans)


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)


f = lambda : map(int, input().split())
(N, K) = f()
(*A,) = list(f())
F = [-1] * (2 * 10 ** 5 + 1)
G = []
i = 1
while F[i] == -1:
	F[i] = len(G)
	G.append(i)
	i = A[i - 1]
t = F[i]
if K < t:
	print(G[K])
else:
	G = G[t:]
	print(G[(K - t) % len(G)])


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
B = [True] * N
C = [1]
now = 0
while B[now]:
	B[now] = False
	now = A[now] - 1
	C.append(now + 1)
start = C.index(C[-1])
D = C[start:-1]
L = len(D)
if K < start:
	print(C[K])
else:
	K -= start
	K %= L
	print(D[K])


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
kiseki = [-1] * n
i = 0
v = 1
while kiseki[v - 1] == -1:
	kiseki[v - 1] = i
	v = l[v - 1]
	i += 1
saisyo = kiseki[v - 1]
kuri = i - saisyo
if saisyo > k:
	print(kiseki.index(k) + 1)
else:
	amari = (k - saisyo) % kuri
	print(kiseki.index(amari + saisyo) + 1)


from collections import defaultdict
(N, K) = list(map(int, input().split()))
N_List = list(map(int, input().split()))
N_Dict = defaultdict(int)
N_Dict[1] = 1
Pos = N_List[0]
for i in range(N):
	N_Dict[Pos] += 1
	if N_Dict[Pos] == 2:
		break
	else:
		Pos = N_List[Pos - 1]
CPos = Pos
flg = 0
Loop_List = [CPos]
while flg == 0:
	CPos = N_List[CPos - 1]
	if CPos != Pos:
		Loop_List.append(CPos)
	else:
		flg = 1
FL = len(N_Dict) - len(Loop_List)
if FL <= K:
	ans = Loop_List[(K - FL) % len(Loop_List)]
else:
	ans = list(N_Dict.keys())[K]
print(ans)


(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())
arr = list(map(int, input().split()))
i = 1
n = 0
visited = {i: n}
while k > 0:
	n += 1
	k -= 1
	i = arr[i - 1]
	if i in visited:
		break
	visited[i] = n
if k == 0:
	print(i)
else:
	k %= n - visited[i]
	while k > 0:
		k -= 1
		i = arr[i - 1]
	print(i)


D = 60
(n, k, *l) = map(int, open(0).read().split())
to = [[0] * n for _ in range(D)]
for i in range(n):
	to[0][i] = l[i] - 1
for i in range(D - 1):
	for j in range(n):
		to[i + 1][j] = to[i][to[i][j]]
a = 0
for i in range(D):
	if k % 2:
		a = to[i][a]
	k //= 2
print(a + 1)


(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)


(_, k, *a) = map(int, open(0).read().split())
a = [0] + a
p = 1
while k:
	p = ~-k % 2 * p or a[p]
	a = [a[b] for b in a]
	k //= 2
print(p)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
rec = [0]
i = 0
flag = [0 for _ in range(n + 1)]
while True:
	if flag[rec[i]] == 0:
		flag[rec[i]] = 1
		rec.append(a[rec[i]] - 1)
	else:
		start = rec.index(a[rec[i]] - 1)
		loop = rec[start:]
		pre = rec[:start]
		break
	i += 1
count = (k - len(pre)) % len(loop)
if k <= len(pre):
	print(pre[k] + 1)
else:
	print(loop[count] + 1)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
x = 1
l = [1]
f = [1] * n
f[0] = 0
for i in range(k):
	x = a[x - 1]
	if f[x - 1]:
		l.append(x)
		f[x - 1] = 0
	else:
		break
y = l.index(x)
res = (k - y) % (len(l) - y) + y
print(l[res])