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()))
for i in range(N):
    A[i] -= 1

reached = [0] * N

next = A[0]
cnt = 1
while not reached[next]:
    reached[next] = cnt
    next = A[next]
    cnt += 1

cnt2 = 1
start = next

next = A[start]
while  start != next:
    cnt2 += 1
    next = A[next]

if K < cnt:
    
    for i in range(N):
        if reached[i] == K:
            print(i+1)
elif K == cnt:
    
    print(start+1)
elif K > cnt:
    
    
    
    
    tmp = K - cnt
    r = tmp % cnt2
    next = A[start]
    for i in range(r-1):
        next = A[next]
    if r == 0:
        print(start+1)
    else:
        print(next+1)

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

roopStart = -1
roopCount = 0
firstMove = 0
now = 0
trace = [0] * n

for i in range(2 * n):
    if trace[now] == 1 and roopStart == -1:
        roopStart = now

    if trace[now] == 2:
        break

    if roopStart != -1:
        roopCount += 1
    else:
        firstMove += 1

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

firstMove -= roopCount

now = 0
if k < firstMove:
    for i in range(k):
        now = a[now] - 1
    print(now + 1)
else:
    for i in range(firstMove):
        now = a[now] - 1

    k -= firstMove
    k %= roopCount
    for i in range(k):
        now = a[now] - 1
    print(now + 1)

Solution (Python):
N, K = map(lambda x: int(x), input().split(" "))
As = list(map(lambda x: int(x), input().split(" ")))

visited = set()
visited_list = []
index = 0
visited.add(index)
visited_list.append(0)
count = 0
flag = False
for i in range(K):
    index = As[index] - 1
    count += 1
    if index in visited:
        flag = True
        break
    visited.add(index)
    visited_list.append(index)

temp_index = visited_list.index(index)
K -= count
count = count - temp_index

if flag:
    K %= count
    for i in range(K):
        index = As[index] - 1

print(index+1)

Solution (Python):




def main():
    
    N, K = list(map(int, input().split()))
    A = [x-1 for x in list(map(int, input().split()))]

    len_cycle = 0
    start = 0
    already_visit = set([0])

    loop_node = -1
    if K > N:
        while True:
            start = A[start]
            len_cycle += 1
            if start in already_visit:
                loop_node = start
                break
            else:
                already_visit.add(start)

        dist_loop_node = 0
        if loop_node != 0:
            start = 0
            while True:
                start = A[start]
                dist_loop_node += 1
                if start == loop_node:
                    break
            len_cycle -= dist_loop_node

        len_move = (K - dist_loop_node) % len_cycle
        start = loop_node
        for _ in range(len_move):
            start = A[start]
        print(start+1)
    else:
        start = 0
        for _ in range(K):
            start = A[start]
        print(start+1)
    pass

if __name__ == '__main__':
    main()

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

if not end:
    j = pos.index(cur)
    print(pos[(k-j-1)%(len(pos)-j) + j])

Solution (Python):
N, K = list(map(int, input().split()))
K -= 1
A = list(map(lambda x: int(x) - 1, input().split()))
seen = [None]*N
seen_set = set()
now = 0
for i in range(N):
    seen[i] = now
    seen_set.add(now)
    to = A[now]
    now = to
    if now in seen_set:
        seen[i+1] = now
        loop_s = seen.index(now)
        loop_end = i + 1
        size = loop_end - loop_s
        break
if K <= loop_s:
    print(seen[K+1] + 1)
else:
    K -= loop_s
    print(seen[loop_s + 1 + K % size] + 1)

Solution (Python):
n,k=map(int,input().split())
A=list(map(int,input().split()))
C=[0]+[-1]*(n-1)
p=1
c=0
s=-1
e=-1
for i in range(n):
    p=A[p-1]
    c+=1
    if C[p-1]==-1:
        C[p-1]=c
    else:
        s=C[p-1]
        e=c
        break
P=1
if s!=-1 and e!=-1:
    if k<=s:
        while k>0:
            P=A[P-1]
            k-=1
        print(P)
    else:
        l=(k-s)%(e-s)
        k=s+l+1
        while k>1:
            P=A[P-1]
            k-=1
        print(P)
else:
    if k<=n:
        while k>0:
            P=A[P-1]
            k-=1
        print(P)
    else:
        while n>0:
            P=A[P-1]

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

Solution (Python):
import numpy as np
import math

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


visited = {}
next=1
start=-1
loop_num=0
while(K>0):
	
	if next==start:
		break
	if not next in visited.keys():
		visited[next] = 1
	elif start==-1:
		start=next
		loop_num=0
	next = A[next-1]
	K-=1
	loop_num+=1


if K==0:
	print(next)
else:
	K%=loop_num
	next=start
	for i in range(K):
		next = A[next-1]
	print(next)

Solution (Python):
import sys

sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

def main():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    lenA = len(A)

    tmp = []
    path = []
    setpath = set([])
    cnt = 0
    i = 1
    for _ in range(1, N+10):
        setpath.add(i)
        path.append(i)
        if len(setpath) != len(path):
            path.pop()
            itmp = path.index(i)
            break
        i = A[i-1]

    lenpath = len(path)
    if K >= lenpath:
        jtmp = (K-itmp)%(lenpath-itmp)
        ans = path[itmp+jtmp]
    else:
        ans = path[K]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
f = [0 for _ in range(n)]
f[0] = 1

v = [1]
s = 0
idx = 0
loop = 0
last = 1
for i in range(k):
    vi = a[last - 1]
    if not f[vi - 1]:
        f[vi - 1] = 1
        last = vi
        s += 1
        v.append(vi)
    else:
        s += 1
        idx = v.index(vi)
        loop = s - idx
        break
else:
    print(vi)
    exit()
remain = k - s
print(v[idx:][remain % loop])

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

A_list = list(map(int, input().split()))
A_list = [A - 1 for A in A_list]

path = [0]
town = 0
k = K
j = 0

while k > 0:
  town = A_list[town]
   
  if k % 10000 == 0:
    if town in path:
      
      
      first_visit = path.index(town)
      loop_size = j - first_visit + 1
      
      
      k = (k-1) % loop_size 
      
      town = path[first_visit + k]
      break
    
  path.append(town)
    
  k = k - 1
  j = j + 1

print(town+1)

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

Solution (Python):
import sys
def input():
    return sys.stdin.readline().rstrip('\n')


def calc(N, K, A):
    n = 1
    k = 0
    d = {n: k}
    while True:
        k += 1
        n = A[n - 1]
        if k == K or n == A[n - 1]:
            return n
        if not n in d:
            d[n] = k
        else:
            x = (K - d[n]) % (k - d[n]) + 1
            for i in d:
                if d[i] == d[n] - 1 + x:
                    return i
            return -1

    

(N, K) = tuple([int(s) for s in input().split()])
A = [int(s) for s in input().split()]
print(calc(N, K, A))

Solution (Python):
import sys
input = sys.stdin.readline



class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


def main():
    N, K = [int(i) for i in input().split()]
    teleporters = [int(i) for i in input().split()]

    
    visited = set()
    visited.add(1)
    pos = 1
    loop = [1]
    while True:
        next = teleporters[pos-1]
        if next not in visited:
            pos = next
            visited.add(next)
            loop.append(next)
        else:
            loop_start = loop.index(next)
            break

    init = loop[:loop_start]
    loop = loop[loop_start:]
    if K < len(init):
        return init[K]
    else:
        K = K - len(init)
        return loop[K % len(loop)]


if __name__ == "__main__":
    print(main())

Solution (Python):
def main():
    N, K = map(int, input().split())
    A = [int(a) for a in input().split()]
    visited = [False for i in range(N)]

    ind = 0
    cnt_toloop = 0
    for i in range(N):
        if visited[ind] == False:
            visited[ind] = True
            ind = A[ind] - 1
            cnt_toloop += 1
        else:
            break

    start = ind
    ind = start
    cnt = 0
    for i in range(N):
        if A[ind] - 1 == start:
            cnt += 1
            break
        else:
            ind = A[ind] - 1
            cnt += 1

    step = 0
    ind = 0
    
    if K >= cnt_toloop:
        K -= cnt_toloop
        K = K % cnt

        step = cnt_toloop + K

        for i in range(step):
                ind = A[ind] - 1
        print(ind + 1)
   
    else:
        step = K
        for i in range(step):
            ind = A[ind] - 1
        print(ind + 1)
    
if __name__ == "__main__":
    main()

Solution (Python):
N, K = map(int, input().split())
S = list(num-1 for num in map(int, input().split()))
loc = 0

arrived = [0]*N
ar = set()
cnt = 0
while not loc in ar:
    arrived[cnt] = loc
    ar.add(loc)
    loc = S[loc]
    cnt += 1

if K < cnt:
    print(arrived[K]+1)
else:
    for i in range(cnt):
        if arrived[i] == loc:
            K -= i
            roop = cnt-i
            K %= roop
            print(arrived[i+K]+1)
            break

Solution (Python):
import sys
import itertools
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
N, K = map(int, readline().split())
A = list(map(lambda x: x - 1, map(int, readline().split())))
visited = [False for i in range(N)]

cur = 0
cycle = []
start = 0
while True:
    if visited[cur]:
        start = cur + 1
        break

    visited[cur] = True
    cycle.append(cur + 1)
    cur = A[cur]


cycle_start_idx = 0
for i in range(len(cycle)):
    if cycle[i] == start:
        cycle_start_idx = i
        break

if K <= cycle_start_idx:
    print(cycle[K])
    exit()


cycle_len = len(cycle) - cycle_start_idx
idx = (K - cycle_start_idx) % cycle_len
print(cycle[idx + cycle_start_idx])

Solution (Python):
def main():
    n, k = [int(e) for e in input().split()]
    A = [int(e) for e in input().split()]

    R = [1]
    RS = set(R)
    cnt = 0
    while True:
        nx = A[R[-1] - 1]
        if nx in RS:
            st = R.index(nx)
            R = R[st:]
            break

        R.append(nx)
        RS.add(nx)

        cnt += 1
        if cnt == k:
            print(nx)
            exit()

    m = (k - cnt) % len(R)
    print(R[m - 1])


if __name__ == '__main__':
    main()

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

cycle = [A[1]]
i = A[1]
visited = set(cycle)
while A[i] not in visited:
    cycle.append(A[i])
    visited.add(A[i])
    i = A[i]
interest_ind = cycle.index(A[i])
second_part = cycle[interest_ind:]
p = len(cycle) - interest_ind
k -= 1
if k < interest_ind:
    print(cycle[k])
else:
    k -= interest_ind
    k %= p
    print(cycle[interest_ind + k])

Solution (Python):


from pprint import pprint
import sys
sys.setrecursionlimit(10 ** 6)

n_towns, k_times = map(int, input().split())
teleport = list(map(int, input().split()))

seq = []
flags = [False] * n_towns
visited = [0] * n_towns

flags[0] = True
seq.append(0)
visited[0] += 1

town_prev = 0

while True:
    town_next = teleport[town_prev] - 1
    visited[town_next] += 1
    if flags[town_next]:
        seq.append(town_next)
        break
    seq.append(town_next)
    flags[town_next] = True
    town_prev = town_next

loop_len = 0
for town, count in enumerate(visited):
    if count == 2:
        loop_idx = seq.index(town)




if k_times < loop_idx:
    print(seq[k_times] + 1)
else:
    arr = seq[loop_idx:len(seq) - 1]
    r = (k_times - loop_idx) % len(arr)
    print(arr[r] + 1)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.insert(0,0)
loop=[]
keys = [i for i in range(1,n+1)]
values=[0 for i in range(n)]
dd=dict(zip(keys, values))
start=10
nex=1
pos=a
while start!=nex:
  start=1
  if dd[nex]==1:
    pos=loop.index(nex)
    loop1=loop[pos:]
    nex=1
  else:
    loop.append(nex)
    dd[nex]=1
    nex=a[nex]

if pos==a:
  length=len(loop)
  print(loop[k%length])
else:
  length=len(loop1)
  if k>=len(loop):
    k=k-pos
    print(loop1[k%length])
  else:
    print(loop[k])

Solution (Python):
from collections import deque
n, k = list(map(int, input().split()))
a = [0, ] + list(map(int, input().split()))
stack = deque([1, ])
visited = [0] * (n + 1)
hist = []
open = []
loop = []
while stack:
    now = stack.popleft()
    if visited[now] == 1:
        b = hist.index(now)
        open = hist[:b]
        loop = hist[b:]
        break
    next = a[now]
    visited[now] = 1
    hist.append(now)
    stack.append(next)
if k < len(open):
    print(open[k])
else:
    print(loop[(k - len(open)) % len(loop)])

Solution (Python):
from collections import defaultdict

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

    town = defaultdict(int)
    cn = defaultdict(int)
    
    visited = set()
    visited.add(0)
    cnt = 0
    tmp = A[0]-1
    town[cnt] = 0
    ii = 0
    
    while True:
        if(tmp not in visited):
            visited.add(tmp)
            cnt += 1
            cn[tmp] = cnt
            town[cnt] = tmp
            tmp = A[tmp]-1
        else:
            ii = cn[tmp]
            break
    
    if(K >= len(town)):
        if(ii != 0):
            K -= ii
            ans = town[(K%(len(town)-ii))+ii]+1
        else:    
            ans = town[K%(len(town))]+1
        print(ans)
    else:
        ans = town[K]+1
        print(ans)
main()

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




visited = {}  
visited[0] = 0 
next_node = A[0]-1
count=1
while True:
    if next_node in visited:
        loop_node = next_node
        break
    visited[next_node]=count
    count+=1
    next_node = A[next_node] - 1

loop_width = count-visited[loop_node]
first_loop = visited[loop_node]

if K<=first_loop:
    now_node = 0
    for _ in range(K):
        now_node = A[now_node] -1
    print(now_node + 1)
    exit()
rest = (K-first_loop)%loop_width
now_node=loop_node
for _ in range(rest):
    now_node = A[now_node] - 1
print(now_node + 1)

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

Solution (Python):




def main():
    N, K = tuple(int(c) for c in input().split(' '))
    A = [None] + list(int(c) for c in input().split(' '))
    
    memo = {1: 0}
    machi = 1
    k = 0
    while k < K:
        machi = A[machi]
        k += 1
        if not machi in memo:
            memo[machi] = k
        else:
            cycle = k - memo[machi]
            res = (K-k) % cycle
            k = K - res
            

    print(machi)

if __name__ == "__main__":
    main()

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

if k >= i:
  print(r[i+(k-i)%(len(r)-i)]+1)
else:
  print(r[k]+1)

Solution (Python):

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

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

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


if len(trace) > K:
    print(trace[K])
else:
    ind = K - len(trace)
    ind = ind % len(loop)
    print(loop[ind])

Solution (Python):
from __future__ import print_function

from functools import reduce
from operator import mul
from collections import Counter
from collections import deque
from itertools import accumulate
from queue import Queue
from queue import PriorityQueue as pq
from heapq import heapreplace
from heapq import heapify
from heapq import heappushpop
from heapq import heappop
from heapq import heappush
import heapq
import time
import random
import bisect
import itertools
import collections
from fractions import Fraction
import fractions
import string
import math
import operator
import functools
import copy
import array
import re
import sys
sys.setrecursionlimit(500000)


input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


















def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def main():
    
    n, k = map(int, input().strip().split())  
    a = list(map(lambda x: int(x) - 1, input().strip().split()))  

    
    eprint("a", end=':\n')
    eprint(a)
    
    path = []
    visitedlist = [False for _ in range(n)]
    pos = 0
    cnt=0   

    while True:
        if cnt==k:
            print(pos+1)
            return
        if visitedlist[pos]:
            start = pos
            break
        cnt+=1
        path.append(pos)
        visitedlist[pos] = True
        pos = a[pos]

    eprint('path ', end=':\n')
    eprint(path)

    for i in range(len(path)):
        if path[i] == start:
            cycle = path[i:]

    eprint('cycle ',end=':\n')
    eprint(cycle)

    m=len(cycle)
    cnt1=k-cnt
    cnt2=cnt1%m
    print(cycle[cnt2]+1)


if __name__ == '__main__':
    main()

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

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

next=0
hoge=[]
zisyo={}

for i in range(n):
    next=A[next]
    if next in zisyo:
        t=next
        break
    else:
        zisyo[next]=1
        hoge.append(next)
hoge_1=[]
hoge_2=[]
for i in range(len(hoge)):
    if hoge[i]==t:
        hoge_1=hoge[:i]
        hoge_2=hoge[i:]

ans=0
if k<=len(hoge_1):
    for i in range(k):
        ans=A[ans]
else:
    k=k-len(hoge_1)-1
    k=k%len(hoge_2)
    ans=hoge_2[0]
    for i in range(k):
        ans=A[ans]


print(ans+1)

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

pos = 0
memo = [[False, 0] for _ in range(N)] 
memo[0] = [True, 0]
distance = 0
l_pos = 0
tel_cnt = 0
while True:
    next = A[pos]
    tel_cnt += 1
    if memo[next][0]: 
        r_pos, l_pos = memo[pos][1], memo[next][1]
        distance = r_pos - l_pos
        break
    memo[next][1] = tel_cnt
    memo[next][0] = True
    
    pos = next

def find_idx_by_telcnt(tel_cnt):
    for i in range(N):
        m = memo[i]
        if m[1] == tel_cnt:
            return i

if K <= l_pos:
    print(find_idx_by_telcnt(K) + 1)
else:
    diff = K - l_pos
    rem = diff % (distance + 1)
    ans_pos = l_pos + rem
    print(find_idx_by_telcnt(ans_pos) + 1)

Solution (Python):



N, K = map(int, input().split())
As = list(map(int, input().split()))
Ms = dict()

cnt = 0
pos = 0
Ms[pos] = cnt

found = False

while cnt < K:
    pos = As[pos]-1
    cnt += 1
    if (not found) and (pos in Ms.keys()):
        found = True
        K = (K - Ms[pos]) % (cnt - Ms[pos]) + cnt
    Ms[pos] = cnt

print(pos+1)

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

l = [0]*n
s = set()
x = 0

for i in range(n):
    machi = a[x]
    if machi in s:
        hajime = l.index(machi)
        repeat = l[hajime:i]
        break
    x = machi-1
    l[i] = machi
    s.add(machi)
else:
    amari = k%n
    print(l[amari-1])
    exit()

if k < hajime:
    print(l[k-1])
else:
    amari = (k-hajime)%len(repeat)
    print(repeat[amari-1])

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

Solution (Python):
import bisect
n, k = map(int, input().split())
a = list(map(int, input().split()))
seen = []
seen_sort = [1]
start = 1

while True:
    tmp = a[start - 1]
    seen.append(start)
    index = bisect.bisect_left(seen_sort, tmp)
    if index >= len(seen_sort) or seen_sort[index] != tmp:
        seen_sort.insert(index, tmp)
    else:
        repeat_index = seen.index(tmp)
        repeat_len = len(seen) - repeat_index
        break
    start = tmp

if k <= repeat_index:
    print(seen[k])
else:
    print(seen[repeat_index + ((k - repeat_index) % repeat_len)])

Solution (Python):
N , K = map(int,input().split())
B = {}
C = {}
D = {0}
A = list(map(int,input().split()))
place = 0
num = 0
C[0] = 0
while True:
    place = int(A[place]) - 1
    num += 1   
    if place in D:
        Y = int(num) - int(C[place])
        break
    elif num == K:
        break
    else:
        B[num] = place
        C[place] = num
        D.add(place)
try:
    X = (K - num) % Y
    print(B[C[place] + X] + 1)
except:
    print(place+1)

Solution (Python):
n, k = map(int , input().split())
A = list(map(int, input().split()))
now = 1
point = [0 for _ in range(n)]
count = 0
step = 0
flag = True
flag2 = True
for i in range(k):
  point[now-1] += 1
  if(point[now-1] == 2):
    if(flag):
      step = 0
      init = i
      flag = False
  if(point[now-1] == 3):
    flag2 = False
    break
      
  now = A[now-1]
  step += 1;
  
  
if(flag2):
  print(now)
else:
  a = k-(init-1)
  k = a%step
  k += (init-1)
  now = 1
  for i in range(k):
    now = A[now-1]
  print(now)

Solution (Python):
from collections import deque
n,k = map(int, input().split())
a=[-1]
a.extend(list(map(int, input().split())))
route=[-1]*(n+1)
q=deque([1])
cnt=0
roop=0
while q:
  p=q.pop()
  route[p]=cnt
  if route[a[p]] == -1:
    q.append(a[p])
    cnt+=1
  else:
    roop=[route[a[p]],route[p]]
    break

if k <= min(roop):
  for i,r in enumerate(route[1:],start=1):
    if r == k:
      print(i)
else:
  t=(k-min(roop))%(max(roop)-min(roop)+1)
  for i,r in enumerate(route[1:],start=1):
    if r == t+min(roop):
      print(i)

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

d = []
color = [0] * n
nxt = 1
for i in range(m):
    if color[nxt-1] == 3:
        loop = len(d)
        rest = m - i
        q = rest % loop
        print(d[q])
        exit()
    elif color[nxt-1] == 2:
        d.append(nxt)
        color[nxt-1] += 1
        nxt = a[nxt-1]
    else:
        color[nxt-1] += 1
        nxt = a[nxt-1]
print(nxt)

Solution (Python):
n,k=map(int,input().split())
loop_=[1]
l_set=set()
lst=list(map(int,input().split()))

tar=None
for i in range(k):
    num=lst[loop_[-1]-1]
    if num in l_set:
        tar=num
        break
    else:
        loop_.append(num)
        l_set.add(num)

if tar==None:
    print(loop_[k])
    exit()    
loop__=loop_[loop_.index(tar):]

num=k+1-len(loop_)
try:
  print(loop__[num%len(loop__)-1])
except:
  print(1)

Solution (Python):
n,k = map(int, input().split())
A = list(map(int, input().split()))
A = {i+1:A[i] for i in range(len(A))}

if k <= n:
    current = 1
    for i in range(k):
        current = A[current]
    print(current)
else:
    current = 1
    count = 0
    pos = []
    visited = {i+1:-1 for i in range(len(A))}
    for i in range(k):
        
        if visited[current] >= 0:
            circle = pos[visited[current]:]
            ans = k - visited[current]
            ans = ans % len(circle)
            
            print(circle[ans])



            break
        
        pos.append(current)
        visited[current] = count
        count += 1
        current = A[current]

Solution (Python):
n,k = map(int,input().split())
a_ls = list(map(int, input().split()))
a_ls = [0] + a_ls

visited_ls = [0] * (n+1)
tour_ls = []
now = 1
times = 1
while True:
    
    tour_ls.append(now)
    visited_ls[now] += 1

    nex = a_ls[now]
    if visited_ls[nex]:
        first_ind = tour_ls.index(nex)
        before_loop = first_ind
        loop_ls = tour_ls[first_ind:times]
        break
    
    now = nex
    times += 1
if k - before_loop >= 0:
    ind = (k - before_loop) % len(loop_ls)
    print(loop_ls[ind])
else:
    print(tour_ls[k])

Solution (Python):
def main():
    n, k = map(int, input().split())
    lst = list(map(int, input().split()))
    ma = [l-1 for l in lst]
    
    visited = [False for _ in range(n)]
    history = []
    now = 0
    while True:
        if visited[now]:
            history.append(now)
            break
        
        visited[now] = True
        history.append(now)
        
        now = ma[now]
    if k <= len(history)-1:
        print(history[k] + 1)
        return
    
    last = history[-1]
    for i in range(n):
        if history[i] == last:
            roopstart = i
            break
    syokilst = history[:roopstart]
    rooplst = history[roopstart:-1]
    syokilen = len(syokilst)
    rooplen = len(rooplst)
    resi = k - syokilen
    mod = resi%(rooplen)
    print(rooplst[mod] + 1)
main()

Solution (Python):
n , k = map(int,input().split())
S = list(map(int,input().split()))
l = [1]
for i in range(2*n):
    l.append(S[l[i]-1])

L = len(l)
def f(s, e):
    a = s
    b = s

    while True:
        a += 2
        b += 1
        if a >= e:
            return None
        if l[a] == l[b]:
            break

    a = 0
    while True:
        a += 1
        b += 1
        if b >= e:
            return None
        if l[a] == l[b]:
            break

    return a, b

c, d = f(0, L)
loop = l[c:d]

m = d - c 
A = c + 1
B = d + 1
if k <=  n:
    print(l[k])
else:
   K = (k - c) % m
   print(loop[K])

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

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

visited = [0] * N

order = []

cur = 1
cnt = 0

while True:
    if visited[cur-1] == 0:
        visited[cur-1] += 1
        if cnt == K:
            print(cur)
            break

    elif visited[cur-1] == 1: 
        
        order_from_cur = order[order.index(cur):]
        r = (K-cnt) % len(order_from_cur)

        final = order_from_cur[r]
        print(final)
        break
    order.append(cur)
    cur = A[cur - 1]
    cnt += 1

Solution (Python):
import numpy as np

n, k = list(map(lambda x: int(x), input().split(' ')))
teles = list(map(lambda x: int(x)-1, input().split(' ')))

beforeloop = 0
loop = []

done = False

ni, nj = 0, 0
pi, pj = 0, 0
while True:
  pi = teles[pi]
  pj = teles[teles[pj]]
  ni += 1
  nj += 2
  
  
  if ni == k:
    print(pi+1)
    done = True
    break
  if pi == pj:
    beforeloop = ni
    break
    
if not done:
  current = pi
  loop.append(current)
  while True:
    current = teles[current]
    if current == pi:
      break
    loop.append(current)
  print(loop[(k - beforeloop) % len(loop)]+1)

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

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
town = [0] * n
move = []
next1 = a[0] - 1
flag = 0
for i in range(k):
    town[next1] += 1
    move += [next1]
    if town[next1] > 1:
        flag = 1
        break
    if i == k-1:
        break
    next1 = a[next1] - 1
if flag == 0:
    print(next1+1)
else:
    first = move.index(move[-1])
    roop = move[first:-1]
    print(roop[(k - first)%len(roop)-1]+1)

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

loop_count = 0
loop = [0] * N
i = 1
town = A[0] - 1
loop[0] = i
if N > K:
    for _ in range(K - 1):
        town = A[town] - 1
    print(town + 1)
else:
    while True:
        if loop[town] == 0:
            i += 1
            loop[town] = i
            town = A[town] - 1
        else:
            break
    loop_size = i - loop[town] + 1
    
    if loop_size != 0:
        K = (K - i) % loop_size
    
    for _ in range(K):
        town = A[town] - 1
    
    print(town + 1)

Solution (Python):
from copy import copy, deepcopy
from collections import Counter
from math import sqrt, floor, factorial
from itertools import permutations, combinations, combinations_with_replacement
from operator import mul
from functools import reduce
import bisect

MOD = 10**9 + 7
INF = float('inf')

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

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

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

mem = [0]*N
mem2 = [0]*N

idx = 0
count1 = 0
while True:
    mem[idx] += 1
    if mem[idx] == 2:
        break
    count1 += 1
    idx = A[idx]


idx2 = idx
count2 = 0

count2 += 1
idx2 = A[idx2]

while True:
    if idx2 == idx:
        break
    count2 += 1
    idx2 = A[idx2]

tmp = count1 - count2

if tmp >= K:
    ans = 0
    for _ in range(K):
        ans = A[ans]
else:
    e = (K - tmp) % count2 + tmp
    ans = 0
    for _ in range(e):
        ans = A[ans]

print(ans+1)