peterverebics/CodeNet_Python_118 huggingface dataset:
import math
i=int(input())
alphabet=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
ans=""
for k in range (100):
    j=i%26-1
    ans=alphabet[j]+ans
    if i<26:
        break
    else:
        i=i//26
print(ans)
N = int(input())
alp = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

amari = []
ans = ''
while N > 26:
    ans = alp[N % 26 - 1] + ans
    N = N // 26

ans = alp[N-1] + ans
print(ans)
n = int(input())
cnt = 0
arr = []
ans = ""
moji = "zabcdefghijklmnopqrstuvwxy"
flg = False
if n % 26 == 0:
    flg = True

for i in range(100):
    arr.append(n % 26)
    n = n // 26
    if n == 0:
        cnt = i
        break

arr.reverse()

if flg:
    print("z" * cnt)
else:
    for i in arr:
        ans +=moji[i]
    print(ans)
N = int(input())
ans = []

num = N
while(True):
  if num > 26:
    ans.append(num%26)
    num = num//26
  else:
    ans.append(num)
    break
answer = ""
for n in ans[::-1]:
  answer += chr(96+n)
print(answer)
N = int(input())
r = []
for k in range(13):
    kk = 12-k
    m = 26**kk
    w = N // m
    if w > 0:
        N -= m * w
        r.append(chr(w+96))
print(''.join(r))
n = int(input())
a = "abcdefghijklmnopqrstuvwxyz"
s = []
while n > 26:
    s.append(a[n%26 - 1])
    n //= 26
s.append(a[n%26 - 1])
print(''.join(s[::-1]))
word_lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
n = int(input())
w_lst = []
x = n
p = 1
q = 0
while x >= 0:
    s = n % 26
    w_lst.append(s)
    q += 26**p
    x = n - q
    n = x
    p += 1

ans_lst = []
for d in w_lst:
    ans_lst.append(word_lst[d-1])
print("".join(ans_lst))
from collections import deque
n = int(input())
stack = deque()
ans =[]
while n > 0:
    if 1<= n <= 26:
        stack.append(n)
        break
    mod = n%26
    stack.append(mod)
    n = n//26
while stack:
    tmp = stack.pop()
    alp = chr(tmp+96)
    ans.append(alp)
anss = ''.join(ans)
print(anss)
n = int(input())

out = ""

from string import ascii_lowercase

while n:
    out = ascii_lowercase[n % 26 - 1] + out
    n //= 26

print(out)
import sys
input = sys.stdin.readline

a = int(input())

def base10to(n, b):
    if (int(n/b)):
        return base10to(int(n/b), b) + chr(n%b + 96)
    return chr(n%b + 96)

print(base10to(a,26))
N=int(input())

ans=[]
while(1):
    if N==0:
        break
    ans.append(str(N%26))
    N//=26

new_ans= ans[::-1]
#print(chr(97)) #97→a

ANS=""
for i in new_ans:
    ANS+=chr((96+int(i)))
#print(new_ans)
print(ANS)
def base10to(n, b):
    if (int(n/b)):
        return base10to(int(n/b), b) + Alp[n%b]
    return Alp[n%b-1]
Alp = ["","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","i","s","t","u","v","w","x","y","z"]
print(base10to(int(input()),26))
n = int(input())

ans = ""
k = 0
while n // (26**(k)) > 0:
    ans += str(chr((n % 26**(k + 1) // 26**(k)) + 96))
    n = n - (n % (26**(k)))
    k += 1

print(ans[::-1])
N=int(input())
ans=""
while N > 0:
    ans = chr(N % 26 + 96) + ans
    N //= 26
    
print(ans)
n = int(input())
d = ['z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
a = ''
f = 0
if n % 26 == 0:
  f = 1
  while n > f:
    a = d[n % 26] + a
    n //= 26
    print(a)
n = int(input())
s = ""
while n > 0:
  s += chr(96 + (n % 26))
  n = (n - 1) // 26
print(s[::-1])
n = int(input())
a = ""
while n > 0:
  n, r = divmod(n, 26)
  if r == 0:
    r = 26
  a += chr(r-1 + ord('a'))
print(a[::-1])
n=int(input())
ans=[]
while True:
  i=n//26
  if i == 0:
    break
  ans.append(n%26)
  n=i
alp=[chr(ord('a') + i) for i in range(26)]
ans.reverse()
ans=[alp[i-1] for i in ans]+[alp[n-1]]
for i in ans:
  print(i,sep="",end="")
N = int(input())
ret = ''
while N > 0:
  i = 1
  while N / i > 26:
    i *= 26
  ret += chr(ord('a') + N // i - 1)
  N %= i
print(ret)
n = int(input())

l = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
m = []

a = 26**10

while a>0:
  b = int(n/a)
  if b > 0:
    b -= 1
    m.append(l[b])
  n = int(n%a)
  a /= 26

name=''.join(m)
print(name)
n=int(input())
n-=1
i=26
c=1
while n-i>0:
    n-=i
    i*=26
    c+=1
al=list('abcdefghijklmnopqrstuvwxyz')
ans=[]
for i in range(c):
    ans.append(al[n%26])
    n=n//26
ans.reverse()
print(''.join(ans))
n=int(input())
alphabet="abcdefghijklmnopqrstuvwxyz"
ans=[]
while n>0:
    ans.append(alphabet[n%26-1])
    n=n//26
print("".join(ans))
n = int(input())

su=[26**i for i in range(1,10)]
from itertools import accumulate
wa = list(accumulate(su)) 
import bisect
c = 0
if n>=27:
    c=bisect.bisect_left(wa, n)
    n=n-wa[c-1]

k=26
bi=[]
for i in range(c+1):
    bi.append(n % k)
    n = n//k

bi[0] += -1
al=[chr(ord('a') + i) for i in range(26)]

li = [al[i] for i in bi]
li.reverse()

mojiretu = ''.join(li)

print(mojiretu)
from collections import deque
def add(x):
    return x+97

N = int(input())
i = 1
while N >= 26**i:
    N = N - 26**i
    i += 1
    
N -= 1
shinsu = deque([])
for j in range(i):
    shinsu.appendleft(N%26)
    N //= 26

shinsu = list(map(add, shinsu))
ans = list(map(chr, shinsu))
print(''.join(ans))
import math

n = int(input())

ans_list = []

def waru(n):
    amari = n%26
    kotae = n / 26
    ans_list.append(math.floor(amari))
    if kotae < 1:
        return 0
    else:
        waru(kotae)

waru(n)

ans_list.reverse()

alph = [chr(i) for i in range(97, 97+26)]


for i in ans_list:
    print(alph[i-1], end="")
N = int(input())
ans = ''
for i in range(1, 99):
  if N <= 26 ** i:
    N -= 1
    for j in range(i):
      ans += chr(ord('a') + N % 26)
      N //= 26
      break
  else:
    N -= 26 ** i
print(ans[::-1]) # reversed
n = int(input())
num2alpha = lambda c: chr(c+64)
def g(x):
    L = []
    while x > 0:
        amari = x%26
        L.append(amari)
        x = x//26
    L.reverse()
    answer = ''
    for i in L:
        answer += num2alpha(i).lower()
    return print(answer)
g(n)
x=int(input())
res=''
s=[]
while x>0:
    d=x%26
    res+=chr(ord('a')+d-1)
    x//=26
res=res[::-1]
print(res)
n = int(input())


l = []

nc = n

while nc > 0:
    l.append(nc % 26)
    nc //= 26

ans = ''

for i in range(len(l)):
    ans += chr(l[-(1+i)] + 96)

print(ans)
N=int(input())
ans=''
for i in reversed(range(11)):
    if 26**i<=N:
        s_num=N//(26**i)
        if s_num==0:
            s='z'
        else:
            s=chr(s_num+96)
        ans=ans+s
        N=N%(26**i)
print(ans)
# -*- coding: utf-8 -*-
n, k = map(int, input().split())
dest = list(map(int, input().split()))

index_map = {}

step = 0
next_index = 0
while 0 < k:
  k -= 1  
  next_index = dest[next_index] - 1

  if next_index in index_map:
    period = step - index_map[next_index]
    skip = k % period
    for j in range(0, skip):
      next_index = dest[next_index] - 1
    break

  index_map[next_index] = step
  step += 1

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

here = 1
log = [1]
loop = []
num_log = [0] * (n + 1)
num_log[1] = 1
for i in range(k):
    here = A[here]
    if num_log[here] == 1:
        pre_loop = log.index(here)
        k = k - pre_loop
        loop = log[pre_loop:]
        here = loop[k % len(loop)]
        break
    else:
        num_log[here] = 1
    log += [here]
print(here)
N, K = map(int, input().split())
A = list(map(int, input().split()))

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

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

if K <= rt.index(town):
    print(rt[K])
else:
    roop = (K-rt.index(town)) % (len(rt)-rt.index(town))
    print(rt[rt.index(town)+roop])
N, K = map(int, input().split())
A = [1] + list(map(int, input().split()))

p = 1
points = [p]
visited = set([p])

for _ in range(N):
    p = A[p]
    if p in visited:
        break
    points.append(p)
    visited.add(p)

idx = points.index(p)

if idx > K:
    print(points[K])
    exit()

cycle = points[idx:]

print(cycle[(K - idx) % len(cycle)])
N,K=map(int,input().split())
A=list(map(int,input().split()))
if K<=N:
    next_city=0
    for i in range(K):
        next_city=A[next_city]-1
else:
    next_city=0
    visited=[-1]*N
    visited[0]=0
    for num in range(1,N):
        next_city=A[next_city]-1
        if visited[next_city]!=-1:
            break
        visited[next_city]=num
    K=(K-num)%(num-visited[next_city])
    for i in range(K):
        next_city=A[next_city]-1
print(next_city+1)
nk = input().split()
n , k = int(nk[0]), int(nk[1])
a = [int(i) for i in input().split()]

t = 1
tl = [1]
ts = {1}


for i in range(len(a)):
    
    t = a[t-1]
    if t in ts:
        break
    else:
        tl.append(t)
        ts.add(t)

tl2 = tl[tl.index(t):len(tl)]
         
# print(tl)
# print(tl2)

ans = 0

if k < len(tl):
    ans = tl[k]
else:
    k = k - len(tl)
    j = k % len(tl2)
    ans = tl2[j]

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

B = [0] * N
C = []

C.append(0)
now = 0
B[0] = 1
for i in range(N):
    now = A[now] - 1
    if B[now] == 1:
        break
    else:
        C.append(now)
        B[now] = 1

mae = 0
for i in range(len(C)):
    if C[i] == now:
        mae = i
        break

ato = len(C) - 1

if K + 1 <= mae:
    print(C[K] + 1)
else:
    print(C[mae + (K - mae) % (ato - mae + 1)] + 1)
N, K = map(int, input().split())
*A, = map(int, input().split())
visited = [False] * N
towns = []
now = 0
length = 0
while not visited[now] and K > 0:
    visited[now] = True
    towns.append(now + 1)
    length += 1
    K -= 1
    now = A[now] - 1
if K == 0: print(now + 1)
else:
    idx = towns.index(now + 1)
    length -= idx
    print(towns[idx:][K % length])
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
d=[0]
w=set([0])
x=0
for i in range(n):
    if a[x]-1 not in w:
        d.append(a[x]-1)
        w.add(a[x]-1)
        x=a[x]-1
    else:
        f=a[x]-1
        break

#print(d)
if k<len(d):
    print(d[k]+1)
else:
    x=k-len(d)
    y=d.index(f)
    z=len(d)-y
    #print(d,f,x,y,z)
    #exit()
    print(d[y+x%z]+1)
n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]


curr = 0
arr = []
visited = [False]*n
while(not visited[curr]):
    visited[curr] = True
    arr.append(curr)
    curr = a[curr]-1

visited = [False]*n
cycleSize = 0
while(not visited[curr]):
    visited[curr] = True
    cycleSize+=1
    curr = a[curr]-1

pathSize = len(arr) - cycleSize

if(pathSize>=k):
    print(arr[k]+1)
else:
    print(arr[(k-pathSize)%cycleSize+pathSize]+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()))

M = 0
temp = K
while temp>0:
    M += 1
    temp>>=1

nexts = [[-1]*N for _ in range(M)]
for i in range(N):
    nexts[0][i] = A[i]-1

for t in range(1,M):
    for i in range(N):
        nexts[t][i] = nexts[t-1][nexts[t-1][i]]

Kb = [0]*M
temp = K
for i in range(M):
    Kb[i] = temp&1
    temp >>= 1

ans = 0
for i in reversed(range(M)):
    if Kb[i]:
        ans = nexts[i][ans]
print(ans+1)
import sys
from collections import deque

input = sys.stdin.readline
N, K = map(int, input().split())
A = list(map(int, input().split()))

visited = set()
q = deque()
q.append(1)

root = []
while q:
    pos = q.pop()
    if pos in visited:
        start = root.index(pos)
        s_len = len(root[:start])
        l_len = len(root[start:])
        break
    visited.add(pos)
    root.append(pos)

    q.append(A[pos-1])

if K <= s_len:
    print(root[K])
else:
    r = (K - s_len) % l_len
    print(root[start+r])
n,k=map(int,input().split())
A=list(map(int,input().split()))
s=[]
v=1
ord=[-1]*(n+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])
def D():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    next = a[0]
    path = [1]
    visited = [True] + [False]*(n-1)
    while not visited[next-1]:
        k -= 1
        if k==0:
            print(next)    
            break
        path.append(next)
        visited[next-1] = True 
        next = a[next-1]
    if k!=0:
        cyclic = path[path.index(next):]
        print(cyclic[k%(len(cyclic))-1])

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

pos = -1
seen = [False] * n
last = [-1] * n
hist = []
i = 0
cnt = 0

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

cycle = hist[last[pos]:]
preCircle = last[pos]

if (k <= preCircle):
    print(hist[k] + 1)
else:
    k -= preCircle
    ans = k % len(cycle)
    print(cycle[ans] + 1)
N,K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
doko = 0
for i in range(N):
    if A[doko] <= 0:
        saishou = min(A)
        loop = A[doko] - saishou + 1
        hajimarimade = A[doko]
        K += hajimarimade
        print(A.index(-1*(K % loop)+hajimarimade)+1)
        exit()
    if i == K:
        print(doko+1)
        exit()
    hoji = doko
    doko = A[doko] - 1
    A[hoji] = (-1)*i
N,K = map(int, input().split())
A = [*map(lambda x:int(x)-1, input().split())]
index = [-1]*N
path = []
pre = 0
while index[pre] < 0:
    index[pre] = len(path)
    path += [pre]
    pre = A[pre]
one = index[pre]
loops = len(path) - one
if K < one: print(path[K]+1)
else: print(path[(K-one) % loops + one]+1)
n,k = (int(x) for x in input().split())
town = [int(i) for i in input().split()]
warp  = [1]
checklist = [0]+[-1]*n
i = 0
j = 1
s = 0
while j<=n:
    warp.append(town[i])
    if checklist[town[i]-1]!=-1:
        s = checklist[town[i]-1]
        loop = warp[s:j]
        break
    checklist[town[i]-1] = j
    i = town[i]-1
    j += 1
if k>=s:
    m = len(loop)
    print(loop[(k-s)%m])
else:
    print(warp[k])
n,k=map(int,input().split())
a=list(map(int,input().split()))
pl=1
visit=[-1]*n
visit[0]=0
loop=0
cnt=0

while 1:
    cnt+=1
    pl=a[pl-1]
    
    
    if visit[pl-1]!=-1:
        loop=visit[pl-1]
        
        break
    visit[pl-1]=cnt
    if cnt==k:
        print(pl)
        exit()

loop_len=cnt-loop

k=(k-loop)%loop_len+loop
print(visit.index(k)+1)
import sys
sys.setrecursionlimit(10**6)

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

path = []
visited = {}


def dfs(v, k):
    if v in visited:
        return v

    visited[v] = k
    path.append(v)
    return dfs(A[v], k+1)


origin = dfs(0, 0)
i = visited[origin]
loop = path[i:]


if K <= i:
    print(path[K] + 1)
else:
    j = (K - i) % len(loop)
    print(loop[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())

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

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

nokori = k + 1 - i
if nokori != 1:
    loop = i - done[stay-1]
    for i in range(nokori%loop):
        stay = a[stay-1]

print(stay)
from collections import defaultdict

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

A = [int(x) - 1 for x in input().split()]

cur = 0
step = 0
d = defaultdict(int)
d[0] = step
li = []
while True:
    cur = A[cur]
    li.append(cur)
    if d[cur] != 0:
        break
    step += 1
    d[cur] = step

pre = li[:d[cur]]
loop = li[d[cur]:]

if K <= len(pre):
    print(pre[K - 1] + 1)
else:
    K -= len(pre) + 1
    print(loop[K % (len(loop))] + 1)
n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))
visited = [0]*(n+1)
pos = 1
steps = 0
while visited[pos] == 0:
	visited[pos] = steps
	steps += 1
	pos = a[pos]
#print(visited)
if k>=steps:
	k -= visited[pos]
	k %= (steps-visited[pos])
	#print(k)
	for i in range(1,n+1):
		if visited[i] == visited[pos]+k:
			print(i)
			break
else:
	for i in range(1,n+1):
		if visited[i] == k:
			print(i)
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 = map(int,input().split())
a_list = list(map(int,input().split()))
a = 0
t = 1
d = 0
hoge_list = []
hoge_set = set()
while a < k:
  hoge_list.append(t)
  hoge_set.add(t)
  t = a_list[t-1]
  a += 1
  if t in hoge_set:
    b = hoge_list.index(t)
    d += 1
    break
if d == 0:
  print(t)
else:
  e = (k-b)%(a-b)
  print(hoge_list[b+e])
N, K = map(int, input().split())
A = list(map(lambda x: int(x) - 1, input().split()))

R = []
done = set()
todo = [0]
done = set()
bp = None
while todo:
    p = todo.pop()
    R.append(p)
    done.add(p)
    np = A[p]
    if np in done:
        bp = np
    else:
        todo.append(np)
b = R.index(bp)
if K < b:
    ans = R[K] + 1
else:
    K -= b
    R = R[b:]
    ans = R[K%len(R)] + 1
print(ans)
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))

journey = [1]

for i in range(2*n + 100):
    nxt = a[journey[-1]]
    journey.append(nxt)
    
if k < n + 1:
    print(journey[k])
    exit()

loop_end = n

loop_start = n-1

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

period = loop_end - loop_start

k %= period
while k<= n:
    k += period

print(journey[k])
n,k=map(int,input().split())
a=list(map(int,input().split()))
visited={}
for i in range(1,n+1):
    visited[i]=-1
l=[1]
visited[1]=0
i=1
while True:
    if visited[a[l[i-1]-1]]==0:
        roop_start=l.index(a[l[i-1]-1])
        break
    visited[a[l[i-1]-1]]=0
    l.append(a[l[i-1]-1])
    i+=1
roop_dif=i-roop_start

if k<roop_start:
    print(l[k])
else:
    k-=roop_start
    k%=roop_dif
    print(l[roop_start+k])
def main():
  n, k = map(int, input().split())

  A = tuple(map(lambda a: int(a) - 1, input().split()))

  arrival = [None] * n
  arrival[0] = 0

  pos = 0
  for i in range(1, k + 1):
    next_pos = A[pos]

    if arrival[next_pos]:
      loop_len = i - arrival[next_pos]
      remaining = k - i
      kk = arrival[next_pos] + remaining % loop_len
      print(arrival.index(kk) + 1)
      return

    arrival[next_pos] = i
    pos = next_pos

  print(pos + 1)


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

A0=list(range(N))

def calc_zero(n):
    return n * 0

A0 = list(map(calc_zero, A0))

temp=0
tempv=[]

for i in range(K):
  if A0[temp]>=1:
    loop=i-A0[temp]
    t=(K-A0[temp])%loop
    temp=tempv[A0[temp]+t-1]
    break
  else:
    A0[temp]=i
    temp=A[temp]-1
    tempv.append(temp)

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

passed = [-1] * (n+1)
route = []
v = 1
while passed[v] == -1:
    passed[v] = len(route)
    route.append(v)
    v = a[v-1]

c = len(route) - passed[v]
l = passed[v]

if k < l:
    print(route[k])
else:
    k -= l
    k %= c
    print(route[l+k])
n,k=map(int,input().split())
a=[0]+list(map(int,input().split()))
already=[0]*(n+1)
already[1]=1
history=[1]
tmp=1
now=0
jump=1
while jump:
  now+=1
  tmp=a[tmp]
  history.append(tmp)
  if now==k:
    ans=tmp
    break
  elif already[tmp]==1:
    jump=0
    past=history.index(tmp)
    loop=now-past
    ans=history[past+(k-now)%loop]
    break
  else:
    already[tmp]=1

print(ans)
N, K = map(int, input().split())
A = list(map(int, input().split()))
passlist = [0]*N
point = 1
depth = 0
waylist = []
while passlist[point-1] == 0:
    waylist.append(point)
    passlist[point-1] = depth
    point = A[point-1]
    depth+=1
roop = waylist[waylist.index(point):len(waylist)]
firstreach = passlist[roop[0]-1]
if K < firstreach:
  print(passlist.index(K)+1)
else:
  print(roop[(K-firstreach)%len(roop)])
n,k,*a=map(int,open(0).read().split())
now=1
if k<=n:
  for i in range(k):
    now=a[now-1]
  print(now)
  exit()
# 絶対ループする
visited=[False]*n
visited[now-1]=-1
for i in range(4*10**5):
  now=a[now-1]
  if visited[now-1]==False:
    visited[now-1]=i
  else:
    c,d=visited[now-1],i
    break

k=(k-c)%(d-c)+c
while k<n:
  k+=(d-c)
now=1
for i in range(k):
  now=a[now-1]
print(now)
n,k=map(int,input().split())
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 = [int(i) for i in input().split()]
s = []
ord = [-1] * (n+1)
c, l = 1, 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]
print(s[k] if (k < l) else s[l + (k - l) % c])
n,k =map(int,input().split())
a=list(map(int,input().split()))
a = [1] + a
def next(i):
    return a[i]

z = [0] * (n+1)

place = 1
T = 0
interval = 0
if k < n:
    for i in range(k):
        place = next(place)
    print(place);exit()
place = 0
k += 1
for i in range(n):
    place = next(place)
    if z[place] != 0:
        interval = T - z[place]  + 1
        break
    z[1] = 1 
    T += 1
    z[place] = T

m = (k-z[place])%interval
for i in range(m):
    place = next(place)
print(place)
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)
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()))

p = [-1] * N
p[0] = 0
flag = 0
now = 1
i = 0

while flag == 0:
    i += 1
    now = A[now - 1]
    if p[now - 1] != -1:
        flag = 1
    else:
        p[now - 1] = i
    if i == K:
        flag = 2
if flag == 2:
    print(now)

else:
    r = i - p[now - 1]
    e = p[now - 1]

    count = K - e
    pi = count % r

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

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

lst = [0] * N
k = 1
cnt = 0
for j in range(10*N):
  lst[k-1] += 1
  k = A[k-1]
num = len(list(filter(lambda x:x>=1,lst)))
k = 1
first = []
loop = []
for j in range(num):
  if lst[k-1] == 1:
    first.append(k)
  elif lst[k-1] > 1:
    loop.append(k)
  k = A[k-1]
if K < len(first):
  print(first[K])
else:
  K -= len(first)
  print(loop[K%len(loop)])
N, K = map(int, input().split())
A = list(map(int, input().split()))

n = 1
order = [n]
order_set = set([n])

for i in range(1, K + 1):
    n = A[n - 1]
    if n in order_set:
        Li = order.index(n)
        Ri = len(order)
        idx = Li + (K - Li) % (Ri - Li)
        print(order[idx])
        exit()
    else:
        order.append(n)
        order_set.add(n)
        continue
print(n)
import sys
input = sys.stdin.readline

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

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


loop_cnt = l.index(loop_start)
loop = l[loop_cnt:]
print(loop[(k - loop_cnt) % len(loop) - 1])
N, K = map(int, input().split())
A = list(map(int, input().split()))
l = [0 for _ in range(N)]

now = 0
l[now] = 1
count = 0
loop = [0]

while count < K:
    count += 1
    now = A[now]-1
    loop.append(now)
    if l[now] == 1:
        loop = loop[loop.index(now):-1]
        break
    else:
        l[now] = 1

if count == K:
    print(now + 1)
else:
    ans = loop[(K-count) % len(loop)]
    print(ans + 1)