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

if K <= 200000:
  i = 1
  for _ in range(K):
    i = A[i-1]
  print(i)
  exit()

visited = {1}
order = [1]

i = 1
for count in range(1, 200001):
  i = A[i-1]
  if i in visited:
    break
  else:
    visited.add(i)
  order.append(i)

idx = order.index(i)
order2 = order[idx:]
# print(order2)
mod = (K+1-idx) % len(order2)

print(order2[mod-1])
n,k = map(int,input().split())
A = list(map(int,input().split()))
sub = [0]*n
city = 0
all = []
roop = []
while sub[city] != 2:
  if sub[city] == 0:
    all.append(city+1)
  else:
    roop.append(city+1)
  sub[city] += 1
  city = A[city] -1
if len(all) > k:
  print(all[k])
else:
  print(roop[(k-(len(all)-len(roop)))%len(roop)])
N, K = map(int, input().split())
A = list(map(int, input().split()))
visited = [-1 for _ in range(N)]
visited[0] = 0
k = 0
k = A[k] - 1
count = 0
while visited[k] == -1:
  count += 1
  visited[k] = count
  k = A[k]-1
 
roop = count - visited[k] + 1
beforeroop = visited[k]
 
if K < beforeroop:
  k = 0
  for _ in range(K):
    k = A[k] - 1
  print(k+1)
else:
  K = K - beforeroop
  K = K % roop
 
  for _ in range(K):
    k = A[k]-1
  print(k+1)
N, K = map(int, input().split())
As = list(map(lambda x: int(x)-1, input().split()))

cnt = 1
v = 0
checked = [-1]*N
checked[0] = 0
while True:
    v = As[v]
    if checked[v]!=-1:
        loop = cnt-checked[v]
        road = checked[v]
        break
    cnt += 1
    checked[v] = cnt-1

ite = K if K<=road else road+((K-road)%loop)

v = 0
for i in range(ite):
    v = As[v]

print(v+1)
N, K = map(int, input().split())
A = list(map(int, input().split())) #どこに行くかのリスト
now = 0 #どこにいるか
LOG = {}
roop = 0
count = 0

while now not in LOG and count < K:
    LOG.setdefault(now, count)
    now = A[now] -  1
    count += 1

if count < K:
    roop = len(LOG) - LOG[now]
    K = (K - count) % roop
    count = 0
    while count < K:
        now = A[now] -  1
        count += 1

print(now+1) #行った場所を保存
import math

def input_num():
	ls = input().strip().split(" ")
	return [int(e) for e in ls]

n,k = input_num()
a_ls = input_num()

d = {}
now = 1
result_ls = [now]
cnt = 0
while True:
	d[now] = cnt
	now = a_ls[now-1]
	if now in d:
		bun = d[now]
		break
	result_ls.append(now)
	cnt += 1


if k < bun:
	print(result_ls[k])
else:
	jun = len(result_ls) - bun
	idx = bun + (k-bun)%jun
	print(result_ls[idx])
n, k = map(int, input().split())
arr = [0] + list(map(int, input().split()))

pos = 1

if k <= n:
    for _ in range(k):
        pos = arr[pos]
    print(pos)
    exit()

res = []
visited = dict()
for i in range(n + 1):
    if pos in visited:
        start = visited[pos]
        length = i - visited[pos]
        print(res[start + (k - start) % length])
        exit()
    res.append(pos)
    visited[pos] = i
    pos = arr[pos]
n,k=map(int,input().split())
A=list(map(int,input().split()))
B=[]
B.append(1)
C=[0]*n
C[0]+=1

start=0
finish=0

for i in range(1,n):
	if C[A[B[i-1]-1]-1]!=1:
		B.append(A[B[i-1]-1])
		C[A[B[i-1]-1]-1]+=1
	else:
		start=B.index(A[B[i-1]-1])
		finish=i
		break

roop=finish-start
line=sum(C)-roop

if line+(roop-1)>=k:
	print(B[k])
else:
	print(B[(k-line)%roop+line])
n,k=map(int,input().split())
a=list(map(int,input().split()))
chk=[False]*n
x=0
visit=[0]
chk[x]=True
while chk:
    x=a[x]-1
    if chk[x]:
        break
    else:
        visit.append(x)
        chk[x]=True
l1=0
while visit[l1]!=x:
    l1+=1
l2=len(visit)-l1
if k>=l1:
    print(visit[l1+((k-l1)%l2)]+1)
else:
    print(visit[k]+1)
N, K = map(int, input().split())
A = list(map(int, input().split()))

tele = [0]
cnt = [1] + [0] * (N-1)
while True:
    nt = A[tele[-1]] -1
    if cnt[nt] == 1:
        rest = tele.index(nt) 
        roop = len(tele) - rest
        break
    tele.append(nt)
    cnt[nt] += 1

if K < len(tele):
    print(tele[K]+1)
else:
    K -= rest
    K %= roop
    print(tele[rest+K]+1)
import sys
sys.setrecursionlimit(10 ** 9)

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

r = []
u = [False for _ in range(n)]
def dfs(x):
  if u[x]:
    return x
  else:
    u[x] = True
    r.append(x)
    return dfs(a[x]-1)

b = dfs(0)
for i in range(len(r)):
  if b == r[i]:
    break
#print(b,i,r)
if k >= i:
  print(r[i+(k-i)%(len(r)-i)]+1)
else:
  print(r[k]+1)
n, k = map(int, input().split())
a = list(map(int, input().split()))

visit = []
step = [-1] * (n + 1)
v = 1
while step[v] == -1:
    step[v] = len(visit)
    visit.append(v)
    v = a[v - 1]
cycle = len(visit) - step[v]
nonCycle = step[v]

if k < nonCycle:
    print(visit[k])
else:
    k -= nonCycle
    k %= cycle
    print(visit[nonCycle + k])
n,k=map(int,input().split())
a=list(map(int,input().split()))
for i in range(n):
    a[i]-=1
doubling=[[0 for i in range(n)] for j in range(60)]
for i in range(n):
    doubling[0][i]=a[i]
for i in range(59):
    for j in range(n):
        doubling[i+1][j]=doubling[i][doubling[i][j]]
v=0
for d in range(60):
    if(k>>d &1):
        v=doubling[d][v]

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

t = [[] for _ in range(n)]
u = 0
for i in range(k + 1):
    t[u].append(i)
    if len(t[a[u] - 1]) == 2:
        break
    else:
       u = a[u] - 1
ans = 0
for i in range(n):
    if len(t[i]) == 2 and (k - t[i][1]) % (t[i][1] - t[i][0]) == 0:
        ans = i + 1
        break
    elif len(t[i]) == 1 and t[i][0] == k:
        ans = i + 1
        break
print(ans)
n,k = map(int,input().split())
A = list(map(int,input().split()))
ex = [0]*n
now = 1
for i in range(k) :
    if i == k-1 :
        now = A[now-1]
        print(now)
        exit()
    elif ex[now-1] == 0 :
        ex[now-1] = i+1
        now = A[now-1]
    else :
        J = (k-i)%(i-ex[now-1]+1)
        for j in range(J) :
            now = A[now-1]
        print(now)
        exit()
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=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
lst=[0]
now=0
memo=0
town=[0]*n
town[0]=1
b=0
for i in range(k):
	now=a[now]
	if town[now]==1:
		memo=now
		b=1
		break
	lst.append(now)
	town[now]=1
if b==1:
	i=lst.index(memo)
	l=len(lst)
	if memo!=0:
		lst=lst[i:]
		l-=len(lst)
		print(lst[(k-l)%len(lst)]+1)
	else:
		print(lst[k%l]+1)
else:
	print(now+1)
#

import sys
input=sys.stdin.readline

def main():
    N,K=map(int,input().split())
    A=list(map(lambda x: int(x)-1,input().split()))
    latest=[-1]*N
    latest[0]=0
    now=0
    while(K>0):
        K-=1
        to=A[now]
        if latest[A[now]]!=-1:
            K%=latest[now]-latest[A[now]]+1
        latest[A[now]]=latest[now]+1
        now=to
    print(now+1)
        
if __name__=="__main__":
    main()
N,K = map(int,input().split())
A_list = list(map(int,input().split()))
# roopがあるときはその範囲

roop = N
first = 0
now = 1
already = [1]
for i in range(N):
  go_town = A_list[now-1]
  already.append(go_town)
  now = go_town

set_A = set(already)
for i in range(len(set_A)+1):
  try:
    set_A.remove(already[i])
  except KeyError:
    first = already.index(already[i])
    roop = already[first:i]
    break

if K < first:
  print(already[K])
else:
  real_K = (K - first) % len(roop)
  print(roop[real_K])
def main():
    import sys
    sys.setrecursionlimit(10 ** 7)

    N, K = map(int, input().split())
    *A, = (int(x) - 1 for x in input().split())

    rs = [-1] * N

    def dfs(cur=0, rest=K):
        if ~rs[cur]:
            loop_range = rs[cur] - rest
            rest %= loop_range

        if rest == 0:
            return cur + 1  # -> 1-ind

        rs[cur] = rest
        return dfs(A[cur], rest - 1)

    print(dfs())


if __name__ == '__main__':
    main()
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))
cities = [0]
c = 0
used = [False] * N
used[0] = True
while not used[A[c]]:
    used[A[c]] = True
    cities.append(A[c])
    c = A[c]
x = cities.index(A[c])
if K < x:
    print(cities[K] + 1)
else:
    K -= x
    K %= len(cities) - x
    print(cities[x + K] + 1)
n, k =[int(i) for i in input().split()]
a = [int(i) - 1 for i in input().split()]

go = dict()
res = []

go[0] = True
res = [0]

now = a[0]
while True:
    if now in go:
        break
    res.append(now)
    go[now] = True
    now = a[now]

# 1
# [0, 1, 2, 3]
# (5 - 1) % (xunhuan)
amari = res.index(now)
xunhuan = res[amari:]
#print(res, xunhuan, amari)

if k <= amari:
    print(res[k] + 1)
else:
    print(xunhuan[ (k - amari) % len(xunhuan)] + 1)
n,k = map(int, input().split())
a = list(map(int, input().split()))
row = []
been = [0]*n
now = 0
nex = 0
t = 0
l = [-1]*n
prev = 0
while been[now] == 0:
    l[now] = t
    row.append(now)
    been[now] = 1
    now = a[now]-1
    t += 1
    if been[now] == 1:
        prev = l[now]
# print(prev,t)
print(row[prev + (k-prev)%(t-prev)]+1 if k > prev else row[k]+1)
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)
import sys
input = sys.stdin.readline

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

for i in range(n):
    A[i] -= 1

B = [0]
F = [-1] * n
F[0] = 0
s = A[0]
i = 0
while F[s] == -1:
    i += 1
    B.append(s)
    F[s] = i
    s = A[s]

# print(F[s], i+1)

if k <= F[s]:
    print(B[k] + 1)
else:
    ind = (k-F[s]) % (i+1-F[s]) + F[s]
    print(B[ind] + 1)
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 = map(int, input().split())
a = list(map(int, input().split()))

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

l = [-1]*n #list for order of teleportation
l[0] = 0
ct = 0 #current town
od = 1 #order of teleportation

while True:
    k -= 1
    ct = a[ct]
    if k == 0:
        break
    if l[ct] == -1:
        l[ct] = od
        od += 1
    else:
        cycle = od - l[ct]
        k %= cycle
        od += 1

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

maxD = K.bit_length() - 1

dp = [[0]*(N) for _ in range(maxD+1)]
for i in range(N):
    dp[0][i] = As[i]
for d in range(1, maxD+1):
    for i in range(N):
        dp[d][i] = dp[d-1][dp[d-1][i]]

iNow = 0  ###
for d in range(maxD+1):
    if K & (1<<d):
        iNow = dp[d][iNow]

print(iNow+1)
n, k = map(int, input().split())
a_list = list(map(int, input().split()))
visit = [1]

for i in range(n):
    visit.append(a_list[visit[-1] - 1])

for i in range(n - 1):
    if visit[-1]  == visit[n - i - 1]:
        break
s = i + 1

for i in range(n - s):
    if visit[i] == visit[i + s]:
        break

if k < i:
    print(visit[k])
else:
    print(visit[i + (k - i) % s])
N, K = map(int, input().split())
A = list(map(int, input().split()))
line = {}
for i in range(N):
  line[i+1] = A[i]

dp = [[0]*(N+1) for _ in range(71)]
for i in range(1, N+1):
  dp[0][i] = line[i]

for i in range(70):
  for j in range(1, N+1):
    dp[i+1][j] = dp[i][dp[i][j]]

for i in range(1, N+1):
  now = i 
  for j in range(70):
    # Kを2ビットに変換し、1のところだけ行う
    if (K&(1<<j)) != 0:
      now = dp[j][now]

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

ls = [0]*N

count = 0
now = 0
roop = 0

while ls[now] == 0 and count < K:
    ls[now] = 1
    now = A[now] - 1
    count += 1
k = K - count
if k > 0:
    while ls[now] == 1:
        ls[now] = 2
        now = A[now] - 1
        roop += 1

    k = k%roop

    while k > 0:
        now = A[now]-1
        k -= 1

print(now+1)
N, K = map(int,input().split())
A = list(map(int,input().split()))
X = [-1 for j in range(N)]
X[0] = 0
Y = [0]
i = 0
Num = 0
MM = 0
for k in range(1,N):
  i = A[i]-1
  if k == K:
    MM = 1
    P = i
    break
  elif X[i] != -1:
    Num = X[i]
    break
  else:
    X[i] = k
    Y.append(i)
Y = Y[Num:]
B = (K-Num)%len(Y)
if MM == 1:
  print(P+1)
else:
  print(Y[B]+1)
n,k=map(int,input().split())
a=list(map(int,input().split()))
count=0
lst={}
lst2=[]
now=1
c=0

while True:
    lst2.append(now)
    lst[now]=c
    now=a[now-1]
    if now in lst:
        break
    c+=1



amari=lst[now]
looplen=c-lst[now]+1


if k<=amari:
    print(lst2[k])

else:
    newlst=lst2[amari:]
    
    newk=k-amari
    num=newk%looplen
    print(newlst[num])
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

used = set()
history = []

cur = 0
used.add(0)

while True:
    history.append(cur)
    next_cur = A[cur] - 1
    if next_cur in used:
        loop_start = history.index(next_cur)
        break
    used.add(next_cur)
    cur = next_cur

B = history[loop_start:]
if K <= loop_start:
    result = history[K]
else:
    result = B[(K-loop_start) % len(B)]

print(result+1)
N, K = map(int,input().split())
tele = list(map(int, input().split()))

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:
  res= len(goal)-len(loop)
  print(loop[(K-res)%len(loop)]+1)
N, K = map(int, input().split())
A = list(map(int, input().split()))

X = 0
town = 1
counted = [0 for _ in range(N)]
List = [0 for _ in range(N)]
for k in range(N):
  X += 1
  counted[k] = town
  town = A[town-1]
  if List[town-1] == 1:
    ini = counted.index(town)
    Q = X - ini
    break
  else:
    List[town-1] = 1
if K >= ini:    
  num = (K-ini) % Q
  print(counted[ini+num])
else:
  print(counted[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 = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

d = defaultdict(lambda: -1)
d[1] = 0
L = [1]
i = 0
k = i
while i<N:
    if d[A[k]]==-1:
        d[A[k]] = i+1
        L.append(A[k])
        k = A[k]-1
    else:
        p = d[A[k]]
        q = i+1
        break
    i += 1

try:
    print(L[K])
except:
    L = L[p:]
    print(L[(K-p)%(q-p)])
N, K = map(int, input().split())
A_i = list(map(int, input().split()))

place = 1
road = [0 for i in range(N)]
cycle = 0

road[0] = 1

while K != 0:
    place = A_i[place - 1]
    K -= 1
    if road[place - 1] == 1:
        loop_place = place
        break
    road[place - 1] = 1

while K != 0:
    loop_place = A_i[loop_place - 1]
    cycle += 1

    if place == loop_place:
        break

if cycle != 0:
    K = K % cycle

while K != 0:
    place = A_i[place - 1]
    K -= 1

print(place)
def solve(string):
    n, k, *a = map(int, string.split())
    a, t = [_a - 1 for _a in a], [0] * n
    p = c = 0
    while not t[a[p]]:
        c += 1
        t[a[p]] = c
        p = a[p]
        if c == k:
            return str(p + 1)
    k = (k - t[a[p]] + 1) % (t[p] - t[a[p]] + 1)
    for _ in (range(k)):
        p = a[p]
    return str(p + 1)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))
import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
def resolve():
    n, k = map(int, input().split())
    b = list(map(lambda x : int(x) - 1, input().split()))

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

l = [A[0]]
s = {A[0]}
a = A[0] - 1
for _ in range(K):
    if A[a] not in s:
        l.append(A[a])
        s.add(A[a])
        a = A[a] - 1
    else:
        a = A[a]
        break
n = len(l)
b = 0
for i in range(n):
    if l[i] == a:
        del l[0:i]
        b = i
        break
T = len(l)
S = (K - b) % T
ans = l[S - 1]

print(ans)
N, K = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
A = [0] + A

n = 1
done = [0 for i in range(N+1)]
if K > N:
    for i in range(N):
        n = A[n]
        if done[n] != 0:
            a = done[n]
            b = i + 1 - a
            break
        done[n] = i + 1

    K = (K - a) % b + a
    n = 1
for i in range(K):
    n = A[n]
print(n)
n, k = map(int, input().split())
a = list(map(lambda x: int(x)-1, input().split()))

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

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

d = li.index(num)
ans = (k-d)%(len(li)-d)+d
print(li[ans]+1)
N, K= [int(i) for i in input().split()]
An = [int(i) for i in input().split()]

i = 0
checked = [0 for _ in range(N)]
c = 0
while checked[i] == 0:
  c += 1
  checked[i] = c
  i = An[i] - 1
if checked[i] >= K:
  i = 0
  for k in range(K):
    i = An[i] - 1
  print(i + 1)
else:
  period = c - checked[i] + 1
  diff = K - checked[i] + 1
  mod_diff = diff % period
  for k in range(mod_diff):
    i = An[i] - 1
  print(i + 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]
    K = K >> 1

print(now)
n,k = map(int,input().split())
# k = int(input())
a = 1
B = [b-1 for b in map(int,input().split())]
d = [-1]*n
d[a-1] = 0
now = a-1
while True:
    if d[B[now]] == -1:
        d[B[now]] = d[now] + 1
        now = B[now]
    else:
        loop = d[now] - d[B[now]] + 1
        start = d[B[now]]
        break
print(d.index((k-start)%loop + start) + 1 if start < k else d.index(k) + 1)
N,K=map(int,input().split())
A=list(map(lambda x:int(x)-1,input().split()))
seen=[-1]*N
i=0
cnt=0
iri=-1

while True:
    if(cnt==K):
        print(i+1)
        exit()
    if seen[i]!=-1:
        roop=cnt-seen[i]
        iri=i
        K-=seen[i]
        K=K%roop
        break
    seen[i]=cnt
    cnt+=1
    i=A[i]
now=iri
for i in range(K):
    now=A[now]


print(now+1)
import sys
sys.setrecursionlimit(10**6)

n, k = map(int, input().split())
A = list(map(int, input().split()))
L = [1]
S = {1}

def f(L, S):
    last = L[-1]-1
    if A[last] not in S:
        L.append(A[last])
        S |= {A[last]}
        return f(L, S)
    else:
        return L
P = f(L, S)
a = A[P[-1]-1]
j = P.index(a)
r = len(P)
l = r - j

if k+1 <= j:
    print(P[k])
else:
    print(P[j+(k-j)%l])
# https://atcoder.jp/contests/abc167/tasks/abc167_d

def resolve():
    n, k = map(int, input().split())
    a = [0] + list(map(int, input().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]]
        a = na
        k >>= 1

    print(c)

if __name__ == '__main__':
    resolve()
n,k=map(int,input().split())
a=[0]+list(map(int,input().split()))
# 1から
rec=[-1]*(n+1)
rec[1]=0
now=1
wp=0
flag=False
while k>wp:
    rec[now]=wp
    nx=a[now]
    rest=k-wp
    loop=rec[now]-rec[nx]+1
    if rest<loop:
        flag=True

    if rec[nx]==-1 or flag:
        wp+=1
        now=nx
    else:
        rest%=loop
        wp=k-rest
        flag=True
print(now)
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=map(int,input().split())
A=list(map(int,input().split()))
mod=K%N
ANS=[-1 for i in range(N)]
a=0
count=1
for i in range(N):
  a=A[a]-1
  if ANS[a]==-1:
    ANS[a]=count
    count+=1
  else:
    roop=count-ANS[a]
    break
if K<count:
  ans=ANS.index(K)+1
else:
  b=(K-ANS[a])%roop+ANS[a]
  ans=ANS.index(b)+1
print(ans)
import sys
sys.setrecursionlimit(10**9)

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

stack = []
s = set()
def dfs(a, k):
  stack.append(a)
  s.add(a)
  if k == K:
    return a
  next_a = A[a]
  if next_a in s:
    idx = stack.index(next_a)
    i = (K - (k + 1)) % (k + 1 - idx)
    return stack[idx + i]
  else:
    res = dfs(next_a, k+1)
  return res

print(dfs(1, 0))
import sys
readline = sys.stdin.readline

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

count = 0
a = 0
visited = {0:0}
while True:
    count += 1
    K -= 1
    a = A[a]
    if K == 0:
        print(a + 1)
        exit(0)
    if a in visited:
        T = count - visited[a]
        K = K % T
        break
    else:
        visited[a] = count

for i in range(K):
    a = A[a]

print(a + 1)
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
dp = [[None]*(n+1) for i in range(64)]
for i in range(1, n+1):
    dp[0][i] = a[i]
for i in range(63):
    for j in range(1, n+1):
        dp[i+1][j] = dp[i][dp[i][j]]

k = format(k, "064b")
now = 1
for i in range(64):
    if k[i] == "1":
        now = dp[~i][now]

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

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())
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]
N, K = map(int, input().split())

array = input().split()
for i in range(len(array)):
    array[i] = int(array[i])

now = 1

history = []
sethistory = set()
start_number = -1

for i in range(K):
    history.append(now)
    sethistory.add(now)
    now = array[now - 1]
    if(now in sethistory):
        start_number = now
        break

if not(start_number == -1):
    start_ind = history.index(start_number)
    length = len(history) - start_ind
    mod = (K - start_ind) % length
    now = history[start_ind + mod]

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

a = [0] + a
now = 1
cities = [now]
done = set()
done.add(now)
for i in range(n):
    now = a[now]
    if now in done:
        break
    done.add(now)
    cities.append(now)
idx_s = cities.index(now)
# print(idx_s)
f = (len(cities) - idx_s)
# print(f)
if k > idx_s:
    d = k - idx_s
    idx = d%f
    ans = cities[idx + idx_s]
    print(ans)
else:
    print(cities[k])
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)
    exit()
x=count-b[now]+1
y=(k-b[now]+1)%x
for i in range(y):
    now=a[now-1]
print(now)
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())
T=str(bin(K))
S=len(T)-2
A = list(map(int,input().split()))
lst=[[0 for e in range(N+1)] for f in range(S)]
for i in range(1,N+1):
 lst[0][i]=A[i-1]
for j in range(1,S):
 for i in range(1,N+1):
  lst[j][i]=lst[j-1][lst[j-1][i]]
ans=1
for k in range(S):
 if int(T[k+2])==1:
  ans=lst[S-1-k][ans]
print(ans)
n,k = map(int, input().split())
a = list(map(int, input().split()))
now = 1
used = [0]*n
for i in range(1,2*n):
    now = a[now-1]
    if (used[now-1]==0):
        used[now-1]=i
    else:
        bef = used[now-1]
        loop = i-used[now-1]
        break
if (bef>=k):
    print(used.index(k)+1)
else:
    over = (k-bef)%loop
    #print(bef,over,loop,used)
    print(used.index(bef+over)+1)
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])