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):
def main():
    N, K = (int(_) for _ in input().split())
    A = [int(_)-1 for _ in input().split()]
    visited = [-1] * N
    now = cc = 0
    while True:
        if visited[now] == -1:
            visited[now] = cc
            cc += 1
            now = A[now]
        else:
            T = cc - visited[now]
            break
    if K < visited[now]:
        for i in range(N):
            if visited[i] == K:
                print(i+1)
                break
    else:
        K -= visited[now]
        for i in range(N):
            if visited[i] == K%T + visited[now]:
                print(i+1)
                break
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys
n,k=map(int,input().split())
A=[int(i) for i in input().split()]
d=dict()
for i,a in enumerate(A):
    d[i+1] = a
p=1
li = set()
li.add(1)
for i in range(k):
    
    p = d[p]
    
    if p in li:
        cyc =  i + 1
        q = 1
        st=0
        while q != p:
            q = d[q]
            st+=1
        cyc -=st
        k = (k-st) % cyc + st
        r = 1
        
        for _ in range(k):
            
            r = d[r]
        print(r)
        sys.exit()
    li.add(p)
else:
    print(p)

Solution (Python):
N,K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N):
    A[i] = A[i] -1
G = 0
lis = [0]*N
lis[0] = 1
roop = []
cnt = 0
while True:
    G = A[G]
    cnt +=1
    lis[G] += 1
    if lis[G] == 2:
        roop.append(G)
    elif lis[G] == 3:
        break

roop_cn = len(roop)
diff = cnt - roop_cn*2
if K < roop_cn + diff:
    G = 0
    for i in range(K):
        G = A[G]
    print(G + 1)
    exit()

K_n = K - diff
amari = K_n % roop_cn
print(roop[amari]+1)

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

    now = 1
    n_move = 0
    graph = []
    visited = set()
    while True:
        graph.append(now)
        visited.add(now)
        now = A[now-1]
        n_move += 1
        if now in visited:
            break

    loop_start = now
    to_loop_start = 0
    for i in range(len(graph)):
        if graph[i] == loop_start:
            to_loop_start = i
    
    per_loop = n_move - to_loop_start
    
    if K <= to_loop_start:
        print(graph[K])
    else:
        print(graph[to_loop_start + (K-to_loop_start) % per_loop])

if __name__ == '__main__':
    solve()

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy


from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd

sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n,k = inpm()
    a = inpl()
    past = [0 for _ in range(n)]
    past[0] = 1
    root = [0]
    pre = 0
    while True:
        Next = a[pre]-1
        if past[Next]:
            break
        root.append(Next)
        pre = Next
        past[Next] = 1
    rev = []
    i = 0
    while root[len(root)-1-i] != Next:
        rev.append(root[len(root)-1-i])
        i += 1
    rev.reverse()
    rever = [Next]
    for i in range(len(rev)):
        rever.append(rev[i])
    n1 = len(root)
    n2 = len(rever)
    if n1-1 >= k:
        print(root[k]+1)
        return
    if rever[0] != 0:
        k -= n1-n2
    print(rever[(k)%n2]+1)
    
if __name__ == "__main__":
    main()

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

v = 0
li = [v]
set_ = {v}
while True:
    nxt_v = a[v]
    if nxt_v not in set_:
        li.append(nxt_v)
        set_.add(nxt_v)
        v = nxt_v
    else:
        start = nxt_v
        break

for i in range(len(li)):
    if li[i] == start:
        ratio = li[i:]
        syoko = li[0:i]

if k < len(syoko):
    print(syoko[k] + 1)

else:
    k -= len(syoko)
    print(ratio[k % len(ratio)] + 1)

Solution (Python):
if __name__ == '__main__':
    N, K = map(int, input().split())
    g = {}
    for i, a in enumerate(list(map(int, input().split()))):
        g[i + 1] = a

    rs = [1, g[1]]
    pos = g[1]
    used = [False for _ in range(N)]
    used[0] = True
    roop_i = None
    while 1:
        rs.append(g[pos])
        used[pos - 1] = True
        pos = g[pos]
        if used[pos - 1] is True:
            roop_i = pos
            break

    idx1 = rs.index(roop_i)

    d1 = idx1
    d2 = len(rs) - idx1 - 1

    if K <= d1:
        print(rs[K])
    else:
        print(rs[(K - d1) % d2 + d1])

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

rep_check = 0
now = 1
place = dict()
place2 = []

if now == a[1]:
    print(1)
    exit()

while now not in place:
    place[now] = 1
    place2.append(now)
    now = a[now]
    rep_check += 1
    if rep_check == k:
        print(now)
        exit()

rep_start = 0
for i in enumerate(place):
    if i[1] == now:
        rep_start = i[0]
        break

k -= rep_start
rep_cnt = len(place) - rep_start

place2 = place2[rep_start:]
k %= rep_cnt
print(place2[k])

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

history = []
visited = set()
cur = 0
for i in range(k):
    if cur in visited:
        x = history.index(cur)
        y = len(history) - x
        print(history[x+(k-x)%y] + 1)
        exit()

    visited.add(cur)
    history.append(cur)
    cur = a[cur]

print(a[history[k-1]]+1)

Solution (Python):
n, k = map(int,input().split())
A = list(map(int,input().split()))
A = [a-1 for a in A]

path = []
passed = set([])
p = 0
while 1:
  if k ==0:
    print(p+1)
    exit()
  if p not in passed:
    path.append(p)
    passed.add(p)
    p = A[p]
    k -= 1
  else:
    break
path = path[path.index(p):]
term = len(path)
k %= term
print(path[k]+1)

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


def answer():
    path = [1]
    visited = set([1])
    now = 1
    t = 0

    while True:
        if now == a[now - 1]:
            return now

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

        if t == k:
            return now

        if now in visited:
            ti = path.index(now)
            tc = t - ti
            cycle = path[ti:]
            break
        else:
            path.append(now)
            visited.add(now)

    return cycle[(k - t) % tc]


print(answer())

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


check = {}
check[1] = 0


cnt = [1]


i = 0


now = 1
while True:
    cnt.append(a[now])
    i += 1
    now = cnt[-1]
    if now in check.keys():
        ans = (k-i) % (i - check[now])
        print(cnt[check[now]+ans])
        break
    else:
        check[now] = i
    if i == k:
        print(now)
        break

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
dic = {1:1}
his=[1]
p=1
s=0
g=0
step=1
for i in range(k):
    t=a[p-1]
    if (t in dic and dic[t]==1):
        s=t
        g=a[t-1]
        break
    else:
        dic[t]=1
    step+=1
    his.append(t)
    p=t
x=his.index(t)
o=step-x
lp=his[x:]
k-=x


print(lp[k%o])

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

if N > K:
    ans = 0
    while K > 0:
        ans = A[ans] -1
        K -= 1
    print(ans+1)
else:
    count = 0
    a = 0
    
    is_visited = [0 for _ in range(N)]
    is_visited[0] = 1
    while True:
        a = A[a]-1
        count += 1
        if is_visited[a] > 0:
            before_roop = is_visited[a]-1
            cycle = count - before_roop
            break
        is_visited[a] = count+1
        
        
        
    
    
    mod = (K - before_roop)%cycle
    
    print(is_visited.index(is_visited[a] +mod) +1)

Solution (Python):
import sys
from typing import Callable, List, NoReturn, Tuple


def main() -> NoReturn:
    scan: Callable[[], str] = sys.stdin.readline

    n, k = map(int, scan().rstrip().split())
    a: Tuple[int] = tuple(int(a_i) - 1 for a_i in scan().rstrip().split())

    flag: List[bool] = [False] * n
    path: List[int] = []

    current_city: int = 0
    prev_city: int = -1

    while not flag[current_city]:
        flag[current_city] = True
        path.append(current_city)
        prev_city = current_city
        current_city = a[current_city]

    start_loop_index: int = path.index(current_city)

    if k < len(path):
        print(path[k] + 1)
    else:
        index_in_loop: int = (k - start_loop_index) % (len(path) - start_loop_index)
        print((path[start_loop_index:])[index_in_loop] + 1)


if __name__ == '__main__':
    main()

Solution (Python):

import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))


N, K = lr()
A = [0] + lr()
if A[1] == 1:
    print(1); exit()
lap = 1
cur = A[1]
nex = [1, A[1]]
been = set([A[1]])
while cur != 1:
    if lap == K:
        print(cur); exit()
    cur = A[cur]
    lap += 1
    nex.append(cur)
    if cur in been:
        lap2 = 1
        cur2 = A[cur]
        while cur2 != cur:
            if lap + lap2 == K:
                print(cur2)
            cur2 = A[cur2]
            lap2 += 1
        K -= lap
        K %= lap2
        for _ in range(K):
            cur2 = A[cur2]
        print(cur2); exit()
    been.add(cur)


K %= lap
answer = nex[K]
print(answer)

Solution (Python):
def solve(n,k,A):
    if A[0]==0:
        return 0
    visited=set()
    visited.add(0)
    C=[0]
    p=A[0]
    while p not in visited:
        visited.add(p)
        C.append(p)
        p=A[p]
    l=len(C)
    s=C.index(p)
    if k<=s:
        return C[k]
    cycle=l-s
    k-=s
    r=k%cycle
    return C[s+r]

def main():
    n,k=map(int,input().split())
    A=tuple(map(lambda x: int(x)-1,input().split()))
    ans=solve(n,k,A)+1
    print(ans)
    
if __name__=='__main__':
    main()

Solution (Python):




def main():
    n, k = map(int, input().split())
    a_list = [0] + list(map(int, input().split()))

    prevs = {}
    c = 0
    pos = 1
    while c < k and pos not in prevs:
        prevs[pos] = c
        pos = a_list[pos]
        c += 1

    if c >= k:
        print(pos)
    else:
        loop_count = c - prevs[pos]
        c_base = prevs[pos]
        kmod = (k - c_base) % loop_count
        prev_poss = list(prevs.keys())[c_base:]
        print(prev_poss[kmod])


if __name__ == '__main__':
    main()

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

visited = [False] * N
loop = [0]
cur_town = 0
step = 0
while True:
    if visited[cur_town]:
        loop = loop[loop.index(cur_town):-1]
        step -= len(loop)
        print(loop[(K - step) % len(loop)] + 1)
        exit()
    else:
        visited[cur_town] = True
        cur_town = A[cur_town] - 1
        loop.append(cur_town)
        step += 1
        if step == K:
            print(cur_town + 1)
            exit()

Solution (Python):
from collections import defaultdict

n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
for i in range(n):
    a[i] -= 1

loop = [0]
town = 0
visited = defaultdict(lambda: None)
visited[town] = 0

period = 0
for i in range(1, k+1):
    town = a[town]
    if visited[town] != None:
        offset = visited[town]
        period = i - offset 
        break
    else:
        loop.append(town)
        visited[town] = i

if period == 0:
    print(town+1)
else:
    print(loop[offset+((k-offset)%period)]+1)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [0]+A
now = 1

if K < N:
    for i in range(K):
        now = A[now]
    print(now)
else:
    visited = [0 for i in range(N+1)]
    for i in range(1, N+2):
        if visited[now] == 0:
            visited[now] = i
            now = A[now]
        else:
            T = i-visited[now]
            tmp = visited[now] + (K-i+1)%T
            print(visited.index(tmp))
            break

Solution (Python):
def binarySearch (arr, l, r, x): 
  
    
    if r >= l: 
  
        mid = l + (r - l) // 2
  
        
        if arr[mid] == x: 
            return mid 
          
        
        
        elif arr[mid] > x: 
            return binarySearch(arr, l, mid-1, x) 
  
        
        
        else: 
            return binarySearch(arr, mid + 1, r, x) 
  
    else: 
        
        return -1
n,k=map(int,input().split( ))
s=input().split( )
p=[]
g=0
while binarySearch(p,0,len(p)-1,int(g))==-1:
    p.append(int(g))
    g=int(s[int(g)])-1
x=p.index(g)
y=len(p)
if k>=y:
    k+=1
    u=p[x:y]
    k-=x
    v=(k)%(y-x)
    print(u[v-1]+1)
else:
    print(p[k]+1)

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
flag = True
went = {1}
go = [1]
i = 0
while flag:
  if a[i] not in went:
    go.append(a[i])
    went.add(a[i])
    i = a[i]-1
  else:
    flag = False
loop = go[go.index(a[i]):]
if len(go) > k:
  print(go[k])
else:
  k -= len(go)-len(loop)
  ans = k % len(loop)
  print(loop[ans])

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n,k=MI()
lis=LI()
i=0
x=0
plot=[0]
while i<n:
    x=lis[x]-1
    i+=1
    plot.append(x)

y=plot[-1]
for i in range(n):
    j=n-i-1
    if y==plot[j]:
        t=i+1
        break

for i in range(n):
    if plot[i]==plot[i+t]:
        am=i
        break

if k<=am:
    print(plot[k]+1)
else:
    print(plot[am+(k-am)%t]+1)

Solution (Python):



import sys
sys.setrecursionlimit(10**6)
import math
import itertools
from collections import defaultdict
from collections import Counter
import numpy as np

import heapq






def rec(arr,c,now,cnt,stop=False ,s_val=0):
    if stop and s_val==cnt:
        return now,cnt
    if c[now]:
        return now,cnt
    c[now]=True
    return rec(arr,c,a[now],cnt+1,stop=stop,s_val=s_val)
    

n,k=list(map(int,input().split()))
a=[0]+list(map(int,input().split()))





c=[False for _ in range(n+1)]
start,s_to_rep_num=rec(a,c,1,0)
c=[False for _ in range(n+1)]
end,rep_n=rec(a,c,start,0)
val=k
s=1

if s_to_rep_num<k:
    s=start
    val=(k-s_to_rep_num)%rep_n
c=[False for _ in range(n+1)]
ans,cnt=rec(a,c,s,0,stop=True,s_val=val)
print(ans)

Solution (Python):
def mp():return map(int,input().split())
def lmp():return list(map(int,input().split()))

n,k = mp()
a = lmp()
a.insert(0,0)
here = 1
cnt = 0
hajimete = [0]*(n+1)
hajimete[0] = -1
used = set()
used.add(1)
flag = False
for i in range(k):
    cnt += 1
    here = a[here]
    if here not in used:
        used.add(here)
        hajimete[here] = cnt
    else:
        flag = True
        break
if not flag:
    print(here)
else:
    loop = cnt - hajimete[here]
    town = hajimete[here] + ((k-hajimete[here]) % loop)
    print(hajimete.index(town))

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

ar = [-1 for i in range(n+1)]
ar[1] == 0
ni = 1
t = 0
while k > t:
    t += 1
    if t == k:
        print(a[ni-1])
        exit()
    elif ar[a[ni-1]] > -1:
        break
    else:
        ar[a[ni-1]] = t
        ni = a[ni-1]

mod = t-ar[a[ni-1]]
idx = ar[a[ni-1]]+((k-ar[a[ni-1]])%mod)
print(ar.index(idx))

Solution (Python):
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))
path = {i:A[i]-1 for i in range(N)}
flags = [-1] * N
before = 0
flags[0] = 0
for i in range(N):
  after = path[before]
  if flags[after] > -1:
    loop = i - flags[after] + 1
    first = flags[after]
    break
  before = after
  flags[after] = i+1

if first < K:
  print(flags.index(first + (K-first) % loop) + 1)
else:
  print(flags.index(K) + 1)

Solution (Python):
from collections import deque

N, K = map(int, input().split())
A = list(map(int, input().split()))
Q = deque()
Q.append(0)

Seen = [-1] * N
Seen[0] = 0
loop = 0
point = 0
while True:
    now = Q.popleft()
    nex = A[now]
    nex -= 1
    if Seen[nex] == -1:
        Seen[nex] = Seen[now] + 1
        Q.append(nex)
    elif Seen[nex] != -1:
        first = Seen[nex]
        loop = Seen[now] - Seen[nex] + 1
        point = nex
        break

if first > K:
    ind = Seen.index(K)
    print(ind+1)
    exit()

p = (K - first) % loop
Q.clear()
Q.append(point)
ans = point
while p > 0:
    no = Q.popleft()
    ans = A[no]
    ans -= 1
    Q.append(ans)
    p -= 1
print(ans + 1)

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

visited = [-1] * n
cnt = 0
n = 0
while True:
    visited[n] = cnt
    nx = As[n] - 1
    cnt += 1
    
    if visited[nx] != -1:
        first = visited[nx]
        if k <= first:
            for i,v in enumerate(visited):
                if v == k:
                    print(i+1)
                    exit()    
        a = (k-first)%(cnt-first)
        loop = [v-first for v in visited]
        for i,x in enumerate(loop): 
            if x == a:
                print(i+1)
                exit()
        break
    n = nx

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = [0 for _ in range(n)]
p = 1
i = 0
while True:
    p = a[p - 1]
    if b[p - 1] > 0 or p == 1:
        break
    else:
        i += 1
        b[p - 1] = i
bp = b[p - 1]
if k <= bp:
    print(b.index(k) + 1)
else:
    l = bp + (k - bp) % (i + 1 - bp)
    print(b.index(l) + 1)

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

history = {}
cycle = []
pos = 1
isEnd = False
for i in range(2*N):
    if pos in history:
        for j in range(len(cycle)):
            if cycle[j] == pos:
                head = cycle[:j]
                cycle = cycle[j:]
                isEnd = True
                break

    if isEnd:
        break

    history[pos] = 1
    cycle.append(pos)
    pos = As[pos-1]




if K < len(head):
    print(head[K])
else:
    mod = (K-len(head)) % len(cycle)
    print(cycle[mod])

Solution (Python):
import sys

sys.setrecursionlimit(10**9)

def dfs(num,lis,count,target):
    visited[num]=count
    n_city=A[num]
    if visited[n_city]!=-1:
        lis.append(num)
        if target<=count:
              print(lis[target]+1)
        else:
            rep=len(lis)-visited[n_city]
            target-=visited[n_city]
            amari=target%rep
            temp=lis[visited[n_city]:]
            print(temp[amari]+1)
        exit()
    else:
        lis.append(num)
        dfs(n_city,lis,count+1,target)
    

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

visited=[-1]*N

dfs(0,[],0,K)

Solution (Python):
def d_teleporter():
    N, K = [int(i) for i in input().split()]
    A = [int(i) - 1 for i in input().split()]  

    from collections import defaultdict
    visit = [0]
    visit_dict = defaultdict(list)
    visit_dict[0] = [0]
    current = 0
    is_found_first = False
    for i in range(N):
        current = A[current]
        if len(visit_dict[current]) == 1 and not is_found_first:
            count_to_loop = visit_dict[current][0]
            loop_length = i - visit_dict[current][0] + 1
            is_found_first = True
        visit.append(current)
        visit_dict[current].append(i + 1)

    if K <= N:
        return visit[K] + 1
    net_moving_time = ((K - count_to_loop) % loop_length) + count_to_loop
    
    current = 0
    for _ in range(net_moving_time):
        current = A[current]
    return current + 1

print(d_teleporter())

Solution (Python):
import numpy as np
n,m=list(map(int,input().split()))
a=np.array(list(map(int,input().split())))
b=np.zeros(n)

mod = 2*n

tmp = 0
for i in range(mod):
  if b[tmp] == 2:
    break
  b[tmp] += 1
  tmp = a[tmp] -1
  
s = len(a[b==1])
t = len(a[b==2])

if m > s:
  m = (m-s)%t + s
tmp = 0
for i in range(m):
  tmp = a[tmp] -1
  
print(tmp+1)

Solution (Python):
def main():
    import sys
    sys.setrecursionlimit(200000)
    input = sys.stdin.readline

    
    
    

    N,K = map(int,input().split())
    A = list(map(int,input().split()))
    
    path = {}
    
    info = []
    def dfs(n,count):
        if n in path:
            
            
            info.append(path[n])
            info.append(count - path[n] +1)
        else:
            count += 1
            path[n] = count
            nx = A[n-1]
            dfs(nx,count)
    dfs(1,-1)
    x,y = info
    if K <= x:
        for k,v in path.items():
            if v == K:
                print(k)
    else:
        K -= x
        K %= y
        for k,v in path.items():
            if v-x == K:
                print(k)


main()

Solution (Python):
n ,k =map(int,input().split())
a = list(map(int,input().split()))
p = {i for i in range(n)}
now = 1
ok = [0 for i in range(n)]
cou = 1
ok[0] = 1
while True:
    cou += 1
    nex = a[now-1]
    if ok[nex-1] == 0:
        ok[nex-1] = cou
    elif ok[nex-1] != 0:
        break
    now = nex
    if cou == k+1:
        print(nex)
        exit()

lost = k - (ok[nex-1]-1)
lokz = cou - ok[nex-1]

p = lost%lokz
ans = ok.index(ok[nex-1]+p)+1
print(ans)

Solution (Python):
n, k = map(int, input().split())
ns = list(map(int, input().split()))
l = [1]
seen = set()
cur = 1
flag = True
while flag:
    i = ns[cur-1]
    cur = i
    if i in seen:
        ri = l.index(i)
        roop = l[ri:]
        flag = False
    else:
        l.append(i)
        seen.add(i)
if k <= len(l)-1:
    print(l[k])
else:
    print(roop[(k-len(l))%len(roop)])

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

D = [[] for _ in range(N+1)]

def judge(idx, now):
    cnt = 1
    n = A[now]
    while n != now:
        n = A[n]
        cnt += 1
    t = K - idx - 1
    t %= cnt
    return t

now = 1
i = 0
j = True
while i < K:
    now = A[now]
    D[now].append(i)
    if j and len(D[now]) > 2:
        i = K - judge(i, now) - 1
        j = False
    i += 1
   
print(now)

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
from bisect import bisect_left, bisect_right, insort
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def MS(): return map(str,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())



n,k = MI()
a = LI()
ans = [1] * n
memo = [-1] * n
memo[0] = 0
s = 0
for i in range(1,n):
    if memo[a[ans[i-1]-1]-1] == -1:
        ans[i] = a[ans[i-1]-1]
        s += 1
        memo[ans[i] - 1] = s

    else:
        t = memo[a[ans[i-1]-1]-1]
        break
if k <= t:
    print(ans[k])
elif t == 0:
    print(ans[(k - 1) % (s-t +1) +1])
else:
    print(ans[(k-(t-1)-1) % (s-t+1) + t])

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

jumps = 0
point = 1
arrived = [False] * (N + 1)
history = [0] * (N * 2)
arrived[1] = True
history[0] = 1
warped = False

while jumps < K:
    next_point = A[point]
    if not warped and arrived[next_point]:
        last_jump = 0
        while history[last_jump] != next_point:
            last_jump += 1
        loop_num = jumps - last_jump + 1
        jumps += ((K - jumps) // loop_num - 1) * loop_num
        warped = True

    arrived[point] = True
    if jumps < N * 2:
        history[jumps] = point
    point = next_point
    jumps += 1

print(point)

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

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

    data = []
    visited = set()
    data.append(1)
    visited.add(1)
    ai = 1
    while True:
        ai = A[ai - 1]
        if ai in visited:
            break
        data.append(ai)
        visited.add(ai)
    d_index = data.index(ai)
    if K > d_index:
        rep_data = data[d_index:]
        rep_len = len(rep_data)
        K -= d_index
        print(rep_data[K - K // rep_len * rep_len])
    else:
        print(data[K])

if __name__ == '__main__':
    main()

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
a=[0]+a
vis={}
cnt=0
cur=1
total=0
while True:
    if cur in vis:
        break
    vis[cur]=True
    cur=a[cur]
    total += 1
vis = {}
cycle=0
while True:
    if cur in vis:
        break
    vis[cur]=True
    cur=a[cur]
    cycle += 1
if k>total:
    k -= (total-cycle)
    k=k%cycle
    while k>0:
        cur=a[cur]
        k -= 1
    print(cur
    )
else:
    cur=1
    while k>0:
        cur=a[cur]
        k -= 1
    print(cur)

Solution (Python):
import sys
input = sys.stdin.buffer.readline
N, K = map(int, input().split())
li = list(map(int, input().split()))
li_a = []
for i in li:
    i -=1
    li_a.append(i)
k = li_a[0]
dic = {}
for i in range(N):
    dic[i] = [False, 0]
dic[0] = [True, 0]
loop = 0
for i in range(1, N+1):
    if dic[k][0] == True:
        loop = i - dic[k][1]
        start = dic[k][1]
        start_k = k
        break
    dic[k] = [True, i]
    k = li_a[k]
    
if K-start <= 0:
    ans = 0
    for i in range(K):
        ans = li_a[ans]
    print(ans+1)
    sys.exit()

amari = (K-start)%loop
if amari == 0:
    print(start_k+1)
else:
    ans = start_k
    for i in range(amari):
        ans = li_a[ans]
    print(ans+1)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, K = map(int, readline().split())
    A = [0] + list(map(int, readline().split()))
    visit = [0]*(N+1)
    nx = 1
    count = 0
    while True:
        now = nx
        nx = A[now]
        count += 1
        if visit[nx] == 0:
            if count == K:
                print(nx)
                break
            else:
                visit[nx] = count
                continue
        else:
            move = (K - visit[nx]) % (count - visit[nx])
            city = visit.index(visit[nx] + move)
            print(city)
            break






if __name__ == '__main__':
    main()

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

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

is_first_visit = [True for i in range(N)]

now_city = 1
visited_history = []
num_to_visit = K

is_finished = False

while is_first_visit[now_city-1]:
    is_first_visit[now_city-1] = False
    visited_history.append(now_city)
    now_city = A[now_city-1]
    num_to_visit -= 1
    
    if num_to_visit == 0:
        is_finished = True
        break

if is_finished:
    print(now_city)
else:
    loop_route = visited_history[visited_history.index(now_city):]
    loop_length = len(loop_route)

    print(loop_route[loop_route.index(now_city) + (num_to_visit % loop_length)])

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

visited = [None] * N
visited[0] = 0
period = None
k = K
cur = 0
while 0 < k:
    next_town = A[cur]
    k -= 1
    if visited[next_town] is None or period is not None:
        visited[next_town] = visited[cur] + 1
    else:
        period = visited[cur] + 1 - visited[next_town]
        k -= k // period * period
    cur = next_town
print(cur + 1)

Solution (Python):
n,k=map(int,input().split())
a=tuple(map(int,input().split()))
b=[-1 for i in range(n)]
b[0]+=1
h=0
for i in range(0,k):
    h=a[h]-1
    
    if b[h]==-1:
        b[h]=i+1
    
    else:
        t=i+1-b[h]
        for j in range((k-i-1)%t):
            h=a[h]-1
        print(h+1)
        break
else:
    print(h+1)

Solution (Python):
n,k=list(map(int,input().split()))
As=list(map(int,input().split()))
root=[1]
count=[0]*n
count[0]+=1

cnt=0
for _ in range(k):
    dis=As[root[-1]-1]-1
    if count[dis]!=0:
        cnt+=1
        break
    else:
        root.append(dis+1)
        count[dis]+=1
if cnt==1:
    first=root.index(As[root[-1]-1])
    print(root[(k-first)%(len(root)-first)+first])
else:
    print(root[-1])

Solution (Python):
inputs = input().split(' ')
town_cnt = int(inputs[0])
trans_cnt = int(inputs[1])

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

king = 0
loop_dic = {}
loop_diff = 0
loop_cyc = 0
is_loop = False
for i in range(trans_cnt):
    if loop_dic.get(king) is None:
        loop_dic[king] = i
        king = towns[king]
    else:
        loop_diff = loop_dic.get(king)
        loop_cyc = i - loop_dic.get(king)
        is_loop = True
        break

if is_loop:
    trans_cnt = ((trans_cnt - loop_diff) % loop_cyc) + loop_diff
    king = 0
    for i in range(trans_cnt):
        king = towns[king]

print(king + 1)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
towns=[-1]*n
towns[0]=0
current=1
for i in range(1,n+2):
  current=a[current-1]
  if i==k+1:
    loop=k+2
    thing=0
    place=1
    break
  elif towns[current-1]==-1:
    towns[current-1]=i
  else:
    loop=i-towns[current-1]
    thing=towns[current-1]
    place=current
    break
k-=thing
b=k%loop
current=place
for i in range(b):
  current=a[current-1]
  if towns[current-1]==-1:
    towns[current-1]=i
print(current)

Solution (Python):
N, K = map(int,input().split())
A = list(map(int,input().split()))
A = [0] + A
L = []
S = set()
i = 1
cnt = 0
while (not i in S) and cnt != K:
    S.add(i)
    L.append(i)
    cnt += 1
    i = A[i]

if cnt == K:
    print(i)
else:
    j = L.index(i)
    ans = L[(j + (K - j) % (cnt - j)) % cnt]
    print(ans)