Problem:
 Score: 400 points    Problem Statement  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 1 4 5
3 2 4 1      Sample Output 1 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 .       Sample Input 2 6 727202214173249351
6 5 2 5 3 2      Sample Output 2 2

Solution (Python):
n,k = map(int, input().split())
a = list(map(int, input().split()))
a = [i-1 for i in a]
num = [0] * (n)
num[0] = 1
now = 0
looped = 0
for i in range(2,k+2):
  nex = a[now]
  if num[nex]:
    start = num[nex]
    cycle = i - num[nex]
    v = (k+1-i) % cycle + start
    ans = num.index(v) + 1
    looped = 1
    break
  else:
    num[nex] = i
    now = nex
if looped:
  print(ans)
else:
  print(now+1)

Solution (Python):
n,k = map(int, input().split())
l = list(map(int, input().split()))

keys = range(1,n+1)
values = [-1]*n
dic = dict(zip(keys,values))
dic[1] = 0
t = 1
i = 1

def inverse_lookup(d, x):
    for k,v in d.items():
        if x == v:
            return k

while i <= k:
    t = l[t-1]
    if dic[t] != -1:
        m  = dic[t]
        d = i-m
        r = (k-i+1)%d
        if r == 0:
            r = d
        ans = inverse_lookup(dic, (m+r-1))
        print(ans)
        break
    dic[t] = i
    i += 1
if i == k+1:
    print(t)

Solution (Python):
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
 
check=[-1 for i in range(N)]
  
temp=1
circle=[]
for i in range(K):
  temp=A[temp-1]
  if check[temp-1]==-1:
    check[temp-1]=i
  else:
    circle.append(i-check[temp-1])
    
    break
  

if len(circle)!=0:
  amari=(K-i)%circle[0]
  temp=1
  for j in range(i+amari):
    temp=A[temp-1] 

print(temp)

Solution (Python):
n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]
visit = {0}
tmp = 0
a_list = [0]
ans = True
for i in range(k):
    tmp = a[tmp]
    if tmp in visit:
        left = a_list.index(tmp)
        ans = False
        print(a_list[left + (k - left) % (i + 1 - left)] + 1)
        break
    else:
        visit.add(tmp)
        a_list.append(tmp)
if ans:
    print(tmp + 1)

Solution (Python):
def detectCycle(a):
    start, length = 0, 1
    p1, p2 = a[1], a[a[1]]
    while p1 != p2:
        p1, p2 = a[p1], a[a[p2]]
    
    p2 = 1
    while p1 != p2:
        p1, p2 = a[p1], a[p2]
        start += 1
    
    p2 = a[p1]
    while p1 != p2:
        p2 = a[p2]
        length += 1
    
    return start, length

n, k = (int(x) for x in input().split())
a = [-1] + [int(x) for x in input().split()]
start, length = detectCycle(a)

loc = 1
for i in range(min(start,k)):
    loc = a[loc]
k = max(k-start,0)

k %= length
for i in range(k):
    loc = a[loc]
print(loc)

Solution (Python):
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])

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

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

memo = [0] * n

index = 0
memo_index = 0
cnt_k = 1
while 1:
    tmp_move_to = a[index]
    if not memo[tmp_move_to]:
        memo[tmp_move_to] = 1
        index = tmp_move_to
        if cnt_k == k:
            print(tmp_move_to + 1)
            exit()
        cnt_k += 1
    else:
        break
k -= cnt_k

kaburi = tmp_move_to
circle = [kaburi]

index = kaburi
while 1:
    if kaburi == a[index]:
        break
    else:
        circle.append(a[index])
        index = a[index]

print(circle[k % len(circle)] + 1)

Solution (Python):
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)

Solution (Python):
from collections import defaultdict

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

s = 1
w = [1]
y = defaultdict(lambda: 0)
y[1] = 1

flag = 0
zen = 0
for i in range(k):

	if(y[a[s-1]] == 1):

		flag = 1
		if(a[s-1] == 1):
			break
		else:
			for j in range(len(w)):

				if(w[j] == a[s-1]):
					zen = j
					w = w[j:]
					
					break
		break
	s = a[s-1]
	y[s] = 1
	w.append(s)



if(flag==0):
	print(s)
else:
	print(w[((k - zen) % (len(w)))])

Solution (Python):
n,k = map(int, input().split())
tele = [0] + list(map(int, input().split()))
move = [-1]*(n+1)
move[1] = 0
now = 1
cnt = 0

while True:
    cnt += 1
    now = tele[now] 

    if move[now] != -1:
        cycle = cnt - move[now]
        break
    else:
        move[now] = cnt

notCycle = move[now]

if notCycle > k:
    now = 1
    for i in range(k):
        now = tele[now]

else:
    loop = (k-notCycle)%cycle
    for i in range(loop):
        now = tele[now]
print(now)

Solution (Python):
n,k = map(int,input().split())
A = [int(i) for i in input().split()]

Visit = [False for i in range(n+1)]

loop_st,loop_end,loop_len = 0,0,0
st = 1
releki = []
loop_list = []
for i in range(10000000000000):
    if Visit[st] == True:
        loop_key = st
        loop_st = releki.index(loop_key)
        loop_end = len(releki)-loop_st
        loop_list = releki[loop_st:]
        loop_len = len(loop_list)
        break
    releki.append(st)
    Visit[st] = True
    st = A[st-1]
if k <loop_st:
    print(releki[k])
    exit()
k -= loop_st

print(loop_list[k%loop_len])

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

count = 0
loc = 0
locs = {0: 0}
ini = 0

while True:
    count += 1
    loc = a[loc] - 1
    
    if count == k:
        break
    if loc in locs:
        ini = locs.get(loc)
        break
    locs[loc] = count

if count != k:
    rest = int((k - ini) % (count - ini))
    if rest <= 0:
        print(loc + 1)
    else:
        d = 0
        while True:
            d += 1
            loc = a[loc] - 1
            if d == rest:
                print(loc + 1)
                break
else:
    print(loc + 1)

Solution (Python):
b0=input().split()
towns=int(b0[0])
k=int(b0[1])
 
al=[int(i)-1 for i in input().split()]
 
rt=[-1 for x in range(towns)]
rt[0]=0
now=0
for x in range(1,300000000):
  now=al[now]
  if rt[now]!=-1:
    stp=x
    break
  else:
    rt[now]=x

ll=stp-rt[now]
k2=k-rt[now]
 
if k<rt[now]:
  print(rt.index(k)+1)
else:
  print(rt.index(k2%ll+rt[now])+1)

Solution (Python):
N,K=map(int,input().split())
*A,=map(int,input().split())

point=[0]*N
count_X=0
count_Y=0
i=0
while point[i]!=3:
    if point[i]<2:
        count_X+=1
    point[i]+=1
    i=A[i]-1
    if point[i]>1:
        count_Y+=1
count_Y-=1

if K>=count_X:
    Z=count_X+(K-count_X)%count_Y
else:
    Z=K

i=0
ans=1
while i<Z:
    ans=A[ans-1]
    i+=1

print(ans)

Solution (Python):
N,K=(int(x) for x in input().split())
A=list(map(int, input().split()))
if K<=N:
    s=1
    for i in range(K):
        s=A[s-1]
    print(s)
else:
    s=1
    S={1}
    iloop=1
    while A[s-1] not in S:
        S.add(A[s-1])
        s=A[s-1]
        iloop=iloop+1
    p=s
    loop=1
    while A[s-1] != p:
        loop=loop+1
        s=A[s-1]
    g=(K-iloop)%loop+iloop
    s=1
    for i in range(g):
        s=A[s-1]
    print(s)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))

s = [-1] * N
i = 0
cur = 0
m = 0
while True:
    if s[cur] != -1 and m != 1:
        m = i - s[cur]
        K = i + (K - i) % m
    else:
        s[cur] = i
    if i == K:
        print(cur+1)
        exit()
    i += 1
    cur = A[cur] - 1

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))

index = 1
route = [index]
node = {index}

for i in range(1, K + 1):
    index = A[index - 1]
    if index in node:
        pre = route.index(index)
        loop = len(route)
        route_idx = (K - pre) % (loop - pre) + pre
        print(route[route_idx])
        break
    else:
        route.append(index)
        node.add(index)
else:
    print(index)

Solution (Python):
N, K = map(int,input().split())
A = [0] + list(map(int,input().split()))
t = 1
u = 1
ans = 1
L = [0] * (N + 1)
loop = [0]
l1 = 0
l2 = 0
count = 0

while True:
  t = A[t]
  if L[t] == 1:
    l2 += 1
    L[t] += 1
    loop.append(t)
  elif L[t] == 0:
    L[t] += 1
    l1 += 1
    l2 += 1
  elif L[t] == 2:
    break
l = l2 - l1
loop[0] = loop[-1]

if K <= l1:
  while count < K:
    u = A[u]
    count += 1
  print(u)
else:
  print(loop[(K - l1) % l])

Solution (Python):
import collections


def main():
    N, K = list(map(int, input().split(' ')))
    A  = list(map(int, input().split(' ')))
    
    where_li = []
    pre = 0
    for i in range(N):
        where_li.append(A[pre])
        pre = A[pre]-1
    
    len_w = len(where_li)
    a = where_li.index(A[pre])
    
    mod_s = len_w-a 
    
    if K<=len_w:
        print(where_li[K-1])
    else:
        nokori = K - len_w
        if nokori % mod_s==0:
            d = mod_s
        else:
            d = nokori % mod_s
        print(where_li[d-1+a])

main()

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
visited = [0]*N 
cnt = 0 
pos = 0 
flag = False
while K > 0:
    cnt += 1
    K -= 1
    if visited[pos] == 0:
        visited[pos] = cnt
    elif not flag:
        K %= cnt-visited[pos]
        flag = True
    pos = A[pos]-1
print(pos+1)

Solution (Python):
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])

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))

c = [0]*N
cc = [0]
c[0] = 1
i = 0
while True:
  c[A[i]-1] += 1  
  cc.append(A[i]-1)
  if c[A[i]-1] > 1:
    memo = A[i]-1
    break
  i = A[i] - 1  
    
used = [i for i in range(N) if c[i] > 0]

pos = [i for i in range(len(cc)) if cc[i] == memo]

res = pos[1] - pos[0]
loop = [i for i in cc[pos[0]:pos[1]]]

if K < N:
  ans = cc[K]+1
else:
  i = (K - (pos[0])) % res
  ans = loop[i]+1
  
print(ans)

Solution (Python):
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])

Solution (Python):
N , K = map(int,input().split())
 
A = list(map(int,input().split()))
 
reach = [-1 for i in range(N)]
reach[0] = 0 
now = 0

dic ={}
dic[0] = 0
for i in range(N):
  to = A[now]-1
  if reach[to] != -1:
    ini = i+1
    
    loop = i+1 - reach[to]
    break
  if i+1 not in dic:
    dic[i+1] = to
  reach[to] = i+1
  
  now = to
 
 

if K < ini:
  print(dic[K]+1)
else:
  t = (ini-loop) + (K-ini)%loop
  print(dic[t]+1)

Solution (Python):
from collections import deque
n,k=map(int, input().split())
a=list(map(int, input().split()))
arr=[-1]*n
arr[0]=0
que=deque([a[0]])
i=1
loop=0

while loop==0:
    x=que.popleft()
    if arr[x-1]==-1:
        arr[x-1]=i
        i+=1
        que.append(a[x-1])
    else:
        loop=i-arr[x-1]
        break
b_loop=i-loop

if k<i:
    print(arr.index(k)+1)
else:
    print(arr.index(b_loop+(k-b_loop)%loop)+1)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):

n,s=map(int,input().split())
a=1
lists=list(map(int,input().split()))
visited=[False for i in range(n+1)]
data=[]
now=a
flag=True
while flag:
    now=lists[now-1]
    if visited[now]==False:
        visited[now]=True
        data.append(now)
    else:
        data.append(now)
        flag=False



looping=0
for i in range(len(data)):
    if data[i]==data[-1]:
        mod=i
        index=i
        looping=data[i:-1]
        loop=len(data)-1-index
        break

U=(s-mod)%loop
if s<=mod:
    print(data[s-1])
elif s>mod:
    print(looping[U-1])

Solution (Python):




def main(n, k, a):
    ck=[-1]*n
    tak=0
    c=0
    ck[0]=c
    for i in range(n):
        c+=1
        tak=a[tak]-1
        if ck[tak]!=-1:
            loop=c-ck[tak]
            lps=ck[tak]
            break
        ck[tak]=c

    tak=0
    for i in range(min(k, (k-lps)%(loop)+lps)):
        tak=a[tak]-1

    print(tak+1)


if __name__ == '__main__':
    n,k=map(int, input().split())
    a=list(map(int, input().split()))
    main(n, k, a)

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

c = 0
loc = 0
locs = {0: 0}
ini = 0

while True:
    c += 1
    loc = a[loc] - 1
    
    if c == k:
        print(loc + 1)
        break
    if loc in locs:
        ini = locs.get(loc)
        break
    locs[loc] = c

if c != k:
    rest = int((k - ini) % (c - ini))
    if rest <= 0:
        print(loc + 1)
    else:
        d = 0
        while True:
            d += 1
            loc = a[loc] - 1
            if d == rest:
                print(loc + 1)
                break

Solution (Python):
def resolve():
    N, K = map(int, input().split())
    A = tuple(map(int, input().split()))
    path = [1]
    for i in range(2*N):
        cur = A[path[-1]-1]
        path.append(cur)

    if K < N:
        print(path[K])
        exit()

    end = N
    si = N-1
    while path[si] != path[end]:
        si -= 1
    dist = end - si
    K = K % dist
    while K < N:
        K += dist

    print(path[K])

if __name__ == '__main__':
    resolve()

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))

from collections import defaultdict, deque
from sys import exit
import math
import copy
from bisect import bisect_left

import sys


n, k = getList()

nums = [0] + getList()
cnt = 1
if k == 1:
    print(1)
    exit()
cur = 1
visited = [-1 for i in range(n + 1)]
visited[1] = 1
rooped = False
while True:
    
    cur = nums[cur]
    cnt += 1

    
    if visited[cur] != -1:
        
        roop = cnt - visited[cur]
        
        k -= ((k-cnt) // roop) * roop
        rooped = True
    if not rooped:
        
        visited[cur] = cnt

    if cnt == k:
        print(nums[cur])
        break

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))

zumi_hash = {}
zumi = [-1]*(2*(10**5))
ima = 1
for i in range(K):
    
    if ima in zumi_hash:
        zumi[i] = ima
        break
    
    if ima == A[ima-1]:
        print(ima)
        exit(0)
    zumi_hash[ima] = True
    zumi[i] = ima
    ima = A[ima-1]
else:
    print(ima)
    exit(0)

loop_e = zumi.index(-1)
loop_s = zumi.index(zumi[loop_e-1])
zan_loop = K-loop_s
loop = zumi[loop_s:loop_e-1]
amari = zan_loop % (len(loop))
print(loop[amari])

Solution (Python):
n,k= list(map(int, input().split()))
li= list(map(int, input().split()))
d = [0 for i in range(n)]
c=[1]
nex=1
first=0
flag=0
for i in range(k):
    nex=li[nex-1]
    if d[nex-1]:
        first=c.index(nex)
        flag=1
        break
    d[nex-1]=1
    c.append(nex)
if flag:
    k-=first
    k%=len(c)-first
    print(c[first+k])
else:
    print(c[-1])

Solution (Python):

N, K = map(int, input().split())
A = list(map(int, input().split()))
dct = {i+1:a for i,a in enumerate(A)}
visited = [0] * N
visited[0] = 1
if (K <= 2 * 10**5):
    nex = 1
    for _ in range(K):
        nex = dct[nex]
    print(nex)
else:
    cnt = 0
    nex = 1
    for _ in range(N):
        cnt += 1
        nex = dct[nex]
        visited[nex-1] += 1
        if visited[nex-1] > 1:
            break
    cnt1 = 0
    nex1 = 1
    for _ in range(N):
        cnt1 += 1
        nex1 = dct[nex1]
        if nex1 == nex:
            break
    if (cnt - cnt1) == 0:
        print(1)
    else:
        l = (K - cnt1) % (cnt - cnt1)
        for i in range(l):
            nex = dct[nex]
        print(nex)

Solution (Python):
n, k = map(int, input().split())
b = list(map(int, input().split()))
a = [0]
a.extend(b)

count = 1
idou = [0]*(n+1)
idousaki = a[1]
while idou[idousaki] == 0:
  idou[idousaki] = count
  idousaki = a[idousaki]
  count += 1

if k >= idou[idousaki]:
  amari = (k - idou[idousaki]) % (count -idou[idousaki])
  print(idou.index((idou[idousaki]+amari)))
else:
  print(idou.index(k))

Solution (Python):
n,k = map(int,input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
current = 0
visited = {0:0} 
route = [0]
for i in range(1, k+1):
    n = a[current]
    if n in visited.keys():
        index = visited[n]
        remain = k - i
        circle = route[index:i]
        finish = remain % len(circle)
        current = circle[finish]
        break
    else:
        visited[n] = i
        route.append(n)

    current = n

print(current+1)

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

already = [0]*n
already[0] = 0
now = 0
count = 1
while True:
  count += 1
  now = a[now]-1
  if already[now]:
    k1 = already[now]
    k2 = count-already[now]
    break
  else:
    already[now] = count

if k > k2+k1:
  k -= k2+k1
  k %= k2
  ans = 1
  for i in range(k1):
    ans = a[ans-1]
  for i in range(k):
    ans = a[ans-1]
else:
  ans = 1
  for i in range(k):
    ans = a[ans-1]
print(ans)

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))
lity=[[0,0] for i in range(N)]
if N<K:
  hata=True
  i=0
  a=0
  j=0
  while hata:
    if lity[i][0]==1:
      a=j-lity[i][1]
      break
    lity[i][0]+=1
    lity[i][1]=j
    i=A[i]-1
    j+=1
  K-=j
  p=K%a
  while p>=1:
    i=A[i]-1
    p-=1
  print(i+1)
else:
  i=0
  for j in range(K):
    i=A[i]-1
  print(i+1)

Solution (Python):
N, K = map(int,input().split())
A = list(map(int,input().split()))

count = 0
i = 1
position = 1
position_list = [1]
position_history = [0] * N
start_loop = 0
def last():
  k = (K - start_loop) % (count - start_loop)
  print(position_list[start_loop+k])

while i > 0:
  position = A[position-1]
  position_history[position-1] += 1
  count += 1
  if count == K:
    print(position)
    break
  elif position_history[position-1] > 1:
    start_loop = position_list.index(position)
    position_list.append(position)
    last()
    break
  else:
    position_list.append(position)

Solution (Python):
n, k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]
assert len(a) == n
current = 0
visited = [-1] * n
visited[0] = 0
for i in range(1, k):
  current = a[current]
  if visited[current] == -1:
    visited[current] = i
  else:
    pre = visited[current]
    count = (k - pre) % (i - pre) + pre
    print(visited.index(count)+1)
    break
else:
  print(a[current]+1)

Solution (Python):

def main():
    n,k = map(int,input().split())
    dests =[0 for _ in range(n)]
    telepos = list(map(int,input().split()))
    for i,t in enumerate(telepos):
        dests[i] = t-1
    print(solve(dests,k))




def solve(dests,k,here = 0):
    here = 0
    telepo_times = 0
    cycle_start = -1
    cycle_end = -1
    cycle_length = -1
    visited = dict()
    visited2 = []


    
    while telepo_times < k:
        
        if here in visited:
            cycle_start = visited[here]
            
            cycle_end = telepo_times - 1
            
            cycle_length = cycle_end - cycle_start + 1
            
            break
        

        visited[here] = telepo_times
        visited2.append(here)
        telepo_times += 1
        here = dests[here]
    if cycle_length == -1:
        return here + 1

    cycle_index = (k-cycle_start) % cycle_length
    return visited2[cycle_start + cycle_index] + 1




    
    
    
    







if __name__ == '__main__':
    main()

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

l = [1]
ind = 0
val = 0
for i in range(n):
    val = a[ind]
    l.append(val)
    ind = val - 1

if n >=k:
    answer1 = l[k]

cnt = 1
repeat =[]
repeat.append(l.pop())
z = 0

for j in range(n - 1, -1, -1):
    z = l.pop()
    if z == repeat[0]:
        break
    else:
        cnt += 1
        repeat.append(z)

repeat = repeat[::-1]

jouyo = (k - n - 1) % cnt

if n >= k:
    print(answer1)
else:
    print(repeat[jouyo])

Solution (Python):
import sys
N, K = map(int, input().split())
A = list(map(int, input().split()))

now = 1
temp_l = [0]*N
temp_count = [-1]*N
temp_count[0] = 0
for i in range(N):
    pre = now
    now = A[pre-1]
    if temp_l[now-1] == 1:
        end = now
        temp = i+1
        break
    else:
        temp_l[now-1] = 1
        temp_count[now-1] = i+1

s_index = temp_count[end-1]
repeat = temp - s_index
if K <= s_index:
    print(temp_count.index(K)+1)
else:
    K -= s_index
    K = K % repeat
    print(temp_count.index(K+s_index)+1)

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
next = a[0]
tmp_list = [a[0]]
roop_list = []
tmp_set = set([a[0]])
while(True):
    next = a[next-1]
    if next in tmp_set:
        hajimari = tmp_list.index(next)
        roop_list = tmp_list[hajimari:]
        break
    tmp_set.add(next)
    tmp_list.append(next)
if k <= hajimari:
    print(tmp_list[k-1])
else:
    print(roop_list[((k-hajimari)%len(roop_list))-1])

Solution (Python):
n,k=map(int,input().split())
A=[0]+list(map(int,input().split()))
from collections import defaultdict

reached=defaultdict(int)
reached[1]=1
i=1
for j in range(1,n+1):
    if reached[A[i]]==0:
        reached[A[i]]=j+1
    else:
        t1=reached[A[i]]
        t2=j+1
        break
    i=A[i]
reached=dict(reached)
if k>t1-1:
    ama=(k-(t1-1))%(t2-t1)
    ans=[s for s,h in reached.items() if h==t1+ama]
else:
    ans=[s for s,h in reached.items() if h==k+1]
print(ans[0])

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
a.insert(0, 0)

if a[1] == 1:
    print(1)
else:
    reached = {i: 0 for i in range(1, n+1)}
    now = 1
    count = 0
    while reached[now] == 0:
        reached[now] = count
        now = a[now]
        count += 1
    roop = count - reached[now]
    if k < reached[now]:
        index = k
    else:
        index = (k - reached[now]) % roop + reached[now]
    
    
    for key, value in reached.items():
        if value == index:
            print(key)
            break

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, k, *a = map(int, read().split())
dict = [-1] * (n + 1)
now = a[0]
dict[0] = 0
for i in range(1, n * 2):
    if i == k:
        print(now)
        exit()
    now = a[now - 1]
    if dict[now] != -1:
        k -= (i + 1)
        k %= (i - dict[now])
        break
    else:
        dict[now] = i
for i in range(k):
    now = a[now - 1]
print(now)

Solution (Python):
def main():
  N,K=map(int,input().split())
  A=list(map(int,input().split()))
  n=[0]*N
  
  d=[1]
  for _ in range(K):
    T=A[d[-1]-1]
    if n[T-1]==1:
      break
    else:
      n[T-1]=1
    d.append(T)
  F=d.index(T)
  LOOP_TOWN=d[F:]
  q=(K-F)%len(LOOP_TOWN)
  print(LOOP_TOWN[q])

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
import sys

def main():
    input = sys.stdin.readline
    n,k = map(int,input().split())

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

    

    checked_dict = {1:0}
    que = deque([a[0]])

    counter = 0


    while True:
        counter += 1
        x = que.popleft()
        if counter == k:
            print(x)
            exit()
        if x in checked_dict:
            startflg = checked_dict[x]
            loop_length = counter - startflg

            inverse_dict = {v:k for k,v in checked_dict.items()}

            loop_list = [inverse_dict[i] for i in range(startflg,startflg+loop_length)]
            amari = (k-startflg) % loop_length
            print(loop_list[amari])
            exit()
        else:
            que.append(a[x-1])
            checked_dict[x] = counter

if __name__ == '__main__':
    main()

Solution (Python):
n,k = map(int,input().split())

a  = [0]*n

a = [int(s) for s in input().split()]

nxt = a[0]

seen = [1]
load = set()
load.add(1)
res = 0
flag = 0
for i in range(k-1):
    
    
    if not(nxt in load):
        seen.append(nxt)
        load.add(nxt)
        nxt = a[nxt-1]
    else:
        root = i - seen.index(nxt) + 1
        
        
        zansa = (k-1-i)%root
        res = seen[seen.index(nxt)+zansa]
        flag = 1
        break


if(flag == 1):
    print(res)
else:
    print(nxt)

Solution (Python):
n, k = map(int, input().split())
A = list(map(int, input().split()))
tmp = 1
route = [tmp]
edge = {tmp}

for i in range(1, k + 1):
    tmp = A[tmp - 1]
    if tmp in edge:
        left = route.index(tmp)
        right = len(route)
        idx = left + (k - left) % (right - left)
        print(route[idx])
        break
    else:
        route.append(tmp)
        edge.add(tmp)
        continue
else:
    print(tmp)

Solution (Python):
n, k = map(int, input().split())
A = list(map(int, input().split()))
tmp = 1
route = [tmp]
edge = {tmp}

for i in range(1, k + 1):
    tmp = A[tmp - 1]
    if tmp in edge:
        left = route.index(tmp)
        right = len(route)
        idx = left + (k - left) % (right - left)
        print(route[idx])
        break
    else:
        route.append(tmp)
        edge.add(tmp)
        continue
else:
    print(tmp)

Solution (Python):
def main():
  N,K=map(int,input().split())
  A=list(map(int,input().split()))
  n=[0]*N
  
  d=[1]
  for _ in range(K):
    T=A[d[-1]-1]
    if n[T-1]==1:
      break
    else:
      n[T-1]=1
    d.append(T)
  F=d.index(T)
  LOOP_TOWN=d[F:]
  q=(K-F)%len(LOOP_TOWN)
  print(LOOP_TOWN[q])

if __name__ == "__main__":
    main()

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
i=0
s=set()
s.add(1)
order=[1]
while 1:
    if a[i] in s:
        order.append(a[i])
        break
    else:
        order.append(a[i])
        s.add(a[i])
    i=a[i]-1
for i in range(len(order)):
    if order[-1]==order[i]:
        idx=i
        break

cnt=len(order)-(idx+1)
if len(order)>k:
    print(order[k])
else:
    k-=len(order)
    order=order[idx+1:]
    
    
    k%=cnt
    
    print(order[k])

Solution (Python):
N,K = [int(i) for i in input().split(' ')]
A = [int(i) for i in input().split(' ')]
 
where_li = []
pre = 0
for i in range(N):
	where_li.append(A[pre])
	pre = A[pre]-1

len_w = len(where_li)
a = where_li.index(A[pre])
mod_s = len_w-a
if K<=len_w:
	print(where_li[K-1])
else:
	if (K-len_w)%mod_s==0:
		d = mod_s
	else:
		d = (K-len_w)%mod_s
	print(where_li[d-1+a])

Solution (Python):
n, k = map(int, input().split())
L= list(map(int, input().split()))
S=[]
S.append(1)
Q=set()
Q.add(1)
p=0
cnt=0
for i in range(n):
	if L[p] in Q:
		cnt+=1
		S.append(L[p])
		break
	else:
		cnt+=1
		S.append(L[p])
		Q.add(L[p])
		p=L[p]-1

cnt2=0
for i in range(n):
	if S[i]!=L[p]:
		cnt2+=1
	else:
		S2=S[i:]
		break
loop=len(S)-cnt2-1


if cnt>=k:
	print(S[k])
else:
	print(S2[(k-cnt)%loop])

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,K=MI()
    A=LI_()
    dist = [inf]*N

    now = 0
    start = 0
    for i in range(N+2):
        if(dist[now] != inf):
            cycle = i - dist[now]
            if(K>=dist[now]):
                K-=dist[now] 
                start = now
                K%=cycle
            break
        dist[now] = i
        now = A[now]
    for i in range(K):
        start = A[start]
    print(start+1)

if __name__ == '__main__':
    main()

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
pos = 0
visit = [0] * N
move = []
roop = []
while visit[pos]!=2:
    if visit[pos]==0:
        move.append(pos)
    else:
        roop.append(pos)
    visit[pos] += 1
    pos = A[pos] - 1
if len(move)>K:
    print(move[K]+1)
else:
    print(roop[(K-(len(move)-len(roop)))%len(roop)]+1)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int,input().split()))
P = 0
Q = [0] + [1 for _ in range(N-1)]
R = [-1] + [0 for _ in range(N-1)]
L = 0
for i in range(K):
    P = A[P]-1
    if Q[P] == 0:
        L = i - R[P]
        S = (K - R[P] - 1) % L
        G = 0
        for j in range(S + R[P] + 1):
            G = A[G]-1
        print(G+1)
        exit()
    if Q[P] == 1:
        R[P] = i
        Q[P] = 0
print(P+1)

Solution (Python):
n, k = map(int,input().split())
town = list(map(int,input().split()))
cnt = 1
step = 1
now = town[0]
via = [1]
s = {1}
while True:
  now = town[via[-1]-1]
  if not now in s:
    via.append(now)
    s.add(now)
  else:
    start = via.index(now)
    roop = len(via) - start
    break
 
 
if len(via) == 1:
    print(1)
    exit()
 
elif len(via) > k:
    print(via[k])
    exit()
 
else:
    roop_remainder = (k + 1 - start) % roop
    if roop_remainder == 0:
        print(via[-1])
    else:
        print(via[start + roop_remainder -1])

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

f = 1
first = []
for _ in range(n):
    num = A[f-1]
    first.append(num)
    f = num

for i in range(n):
    if num == first[i]:
        roopcount = n-i-1 
        break

if k <= n:
    print(first[k-1])
else:
    s = (k-(n-roopcount))%roopcount
    print(first[i+s])