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()))
t = [1]
x = [0]*n
x[0] = 1
y = 1
z = 0
d = 0
flag = False
for i in range(k):
    t.append(a[t[i]-1])
    if x[a[t[i]-1]-1] != 0:
        y = x[a[t[i]-1]-1]
        z = i+2
        b = [a[t[i]-1]]
        break
    else:
        x[a[t[i]-1]-1] += i+2
        z = i+2
        b = [a[t[i]-1]]
    if i == k - 1:
        flag = True

if flag:
    print(a[t[k-1]-1])
    exit()

d = z-y


if d != 0:
    r = (k - y) % d
else:
    r = k - y

for i in range(r+1):
    b.append(a[b[i]-1])

print(b[r+1])

Solution (Python):
n, m = map(int, input().split())
lis = list(map(lambda z: int(z) - 1, map(int, input().split())))

s = {0}
res = [0]

while lis[res[-1]] not in s:
    res.append(lis[res[-1]])
    s.add(res[-1])

x, y = res.index(lis[res[-1]]), len(res)

if m < y:
    print(res[m] + 1)
else:
    mm = (m - x) % (y - x)
    print(res[mm + x] + 1)

Solution (Python):
from copy import copy
n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]
ans = 0
while k:
    if k & 1:
        ans = a[ans]
    c = [a[a[i]] for i in range(n)]
    a = copy(c)
    k >>= 1
print(ans + 1)

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,numpy,string,decimal
import sys
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def SS(): return map(str,sys.stdin.readline().rstrip().split())
def II(): return map(int,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 = II()
A = LI()
flag = [False]*N
past = []
past.append(0)
now = 0
cnt = 1
for k in range(K):
  now = A[now]-1
  if flag[now]:
    past = past[past.index(now)::]
    print(past[(K - cnt) % len(past)]+1)
    exit()
  flag[now] = True
  cnt += 1
  past.append(now)
print(now+1)

Solution (Python):

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


s = []
ord = [-1] * (n+1)

v = 1
c = 1
l = 0
while ord[v] == -1:
    ord[v] = len(s)
    s.append(v)
    v = a[v-1]
c = len(s) - ord[v]
l = ord[v]

if l > k:
    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()))
pl=1
visit=[-1]*n
visit[0]=0
loop=0
cnt=0

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

loop_len=cnt-loop
q=True
k=(k-loop)%loop_len+loop
print(visit.index(k)+1)

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

Visit = [None] * N
now = 0
n = 0
while Visit[now] is None:
    Visit[now] = n
    now = As[now]-1
    n += 1
    if n == K:
        print(now+1)
        exit()

for _ in range((K-Visit[now]) % (n-Visit[now])):
    now = As[now]-1
print(now+1)

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

s = []
ord = [-1]*(n+1)
c = 1
l = 0


v = 1
while (ord[v] == -1):
    ord[v] = len(s)
    s.append(v)
    v = a[v-1]
    
c = len(s) - ord[v]
l = ord[v]


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

town = [0 for i in range(N+1)]

def next(A,now):
    return(A[now-1])

now = 1
i = 0
while True:
    if town[now] != 0:
        roop_start = town[now]
        roop_end = i
        break
    town[now] = i+1
    now = next(A,now)
    i += 1

roop = roop_end - roop_start + 1
teleport = min(roop_start + (K - roop_start)%roop,K)

now = 1
for i in range(teleport):
    now = next(A,now)

print(now)

Solution (Python):


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

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

tele = [0]
s = 0
B = {0:0}
for i in range(N):
    if A[tele[-1]] in B and len(B) != 1:
        s = B[A[tele[-1]]]
        break
    B[A[tele[-1]]] = i+1
    tele.append(A[tele[-1]])

if K <= s:
    print(tele[K]+1)
else:
    K -= s
    amari = K%(len(tele[s:len(tele)]))
    tele = tele[s:len(tele)]*2
    print(tele[amari]+1)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
pl=1
visit=[-1]*n
visit[0]=0
loop=0
q=True
v=True
cnt=0

while q:
    cnt+=1
    pl=a[pl-1]
    if visit[pl-1]!=-1:
        loop=visit[pl-1]
        break
    visit[pl-1]=cnt
    if cnt==k:
        print(pl)
        v=not v
        break
    if cnt==10**7:
        q=not q
if v:
    loop_len=cnt-loop
    q=True
    k=(k-loop)%loop_len+loop
    print(visit.index(k)+1)

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


next = 1
List = [-1]*N
 
for i in range(K):
    if List[next-1] == -1:
        List[next-1] = i
        next = A[next-1]
 
    else:
        loop = i - List[next-1]
        afterloop = (K-(i-loop))%loop
        print(List.index((i-loop) + afterloop)+1)
        exit()
 
print(next)

Solution (Python):
X = input().split()
N = int(X[0])
K = int(X[1])

H = input().split()
A = [int(z) for z in H]
B = []
s = []

for z in range(N):
	B.append(0)
	s.append(0)

C = 1
B[0] +=1
s[0] = 0

for i in range(K):
	C = A[C-1] 
	B[C-1] += 1
	if(B[C-1] == 2):
		break
	s[C-1] = i+1

r = i+1 - s[C-1]

if not r == 0:
	
	t = (K - s[C-1])
	u = t % r
	for g in range(u):
		C = A[C-1] 

print(C)

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

vis = set()
vis.add(1)
cnt = 0
cnt2town = {}
town2cnt = {}
cnt2town[0] = 1
town2cnt[1] = 0

cnt = 1
prev = 1
while cnt <= K:
    a = A[prev - 1]

    if a in vis:
        c = town2cnt[a]

        rem = K - cnt
        num_loop = cnt - c

        r = rem % num_loop
        print(cnt2town[c + r])

        exit()

    cnt2town[cnt] = a
    town2cnt[a] = cnt
    vis.add(a)

    prev = a
    cnt += 1


print(a)

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))
flag=0
now=0
i=0
count=0
c=[0]*N
r=[0]
c[0]=1
while flag==0:
  now=A[now]-1
  count+=1
  if count==K:
    flag=1
  elif c[now]==1:
    flag=2
  c[now]=1
  r.append(now)
if flag==1:
  print(now+1)
else:
  o=[i for i, x in enumerate(r) if x == now]
  print(r[(K-o[0])%(o[1]-o[0])+o[0]]+1)

Solution (Python):


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

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

tele = [0]

z = 0
s = 0

B = {0:0}
for i in range(N):
    if A[tele[-1]] in B and len(B) != 1:
        s = B[A[tele[-1]]]
        break
    B[A[tele[-1]]] = i+1
    tele.append(A[tele[-1]])
    z = tele[-1]

if K <= s:
    print(tele[K]+1)
else:
    K -= s
    amari = K%(len(tele[s:len(tele)]))
    tele = tele[s:len(tele)]*2
    print(tele[amari]+1)

Solution (Python):
n,k=list(map(int,input().split()))
A=[i for i in range(1,n+1)]
B=list(map(int,input().split())) 
road=dict(zip(A,B))







j=1
C=[-1 for i in range(n)]
dp=dict(zip(A,C))
dp[1]=0
num=0
while dp[j]!=k-1:
    j=road[j]
    num=num+1
    if dp[j]== -1:
        dp[j]=num
    else:
        t=num-dp[j]
        pre=dp[j]
        itti=j
        break
    if num==k-1:
        print(road[j])
        exit()

mod=(k-pre)%t

for i in range(mod):
    itti=road[itti]
print(itti)

Solution (Python):
N, K, *A = map(int, open(0).read().split())

A = [0] + A
visited = [-1] * (N + 1)

pre = 0
cycle = 0

cur = 1
for i in range(N):
    visited[cur] = i

    cur = A[cur]
    if visited[cur] >= 0:
        pre = visited[cur]
        cycle = i + 1 - visited[cur]
        break

cur = 1
for _ in range(min(K, pre + ((K - pre) % cycle))):
    cur = A[cur]

print(cur)

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

m = {}
for i, a in enumerate(A):
    m[i+1] = a

visited = []
cycle = set()
current_city = 1
visited.append(current_city)
cycle.add(current_city)
cycle_start_index = -1
while 1:
    current_city = m[current_city]
    if current_city in cycle:
        cycle_start_index = visited.index(current_city)
        break
    else:
        visited.append(current_city)
        cycle.add(current_city)

cycle_before = visited[:cycle_start_index]
cycle_after = visited[cycle_start_index:]
if len(cycle_before) - 1 < K:
    print(cycle_after[(K - len(cycle_before)) % len(cycle_after)])
else:
    print(cycle_before[K])

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

  root = [1]

  record = [-1]*N
  break_flag = 0
  for n in range(K):
    new = int(A_list[ int(root[n])-1 ])
    if record[new-1] <0:
      record[new-1]=1
      root.append(new)
    else:
      ind = root.index(new)
      break
  else:
    print(new)
    break_flag = 1
    
  if break_flag == 0:  
	  pre = len(root[:ind])
	  roop = len(root)-pre

	  print(root[ind:][(K-pre)%roop])
   
if __name__ == '__main__':
  main()

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

s = 1
seen = [False for _ in range(n+1)]
d = []
while not seen[s]:
    seen[s] = True
    d.append(s)
    s = a[s-1]

noncyi = d.index(s)
cyi = len(d) - noncyi
if k < noncyi:
    print(d[k])
else:
    k -= noncyi
    print(d[noncyi + k%cyi])

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = []
ord = [-1 for i in range(n+1)]
v = 1
c = 1
l = 0
while ord[v] == -1:
    ord[v] = len(s)
    s.append(v)
    v = a[v-1]
c = len(s) - ord[v]
l = ord[v]
if k < l:
    print(s[k])
else:
    k -= l
    k %= c
    print(s[l+k])

Solution (Python):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a = [a[i] - 1 for i in range(n)]
    next_i = 0
    seen = [-1] * n
    count = 0
    while True:
        if seen[next_i] != -1:
            start = seen[next_i]
            l = count - seen[next_i]
            break
        seen[next_i] = count
        count += 1
        next_i = a[next_i]
    if k > start:
        k = ((k - start) % l) + start
    for i in range(n):
        if seen[i] == k:
            print(i + 1)
            break


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

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

visited=[-1]*(n+1)
visited[1]=0

lstart=0
t=0

now=1
while lstart==0:
    if visited[al[now]]<0:
        visited[al[now]]=visited[now]+1
        now=al[now]
        
    else:
        t=visited[now]-visited[al[now]]+1
        lstart=al[now]

if visited[lstart]>=k:
    print(visited.index(k))
            
else:
    print(visited.index(visited[lstart]+(k-visited[lstart])%t))

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    n,k=map(int,readline().split())
    a=list(map(lambda x:int(x)-1,readline().split()))

    d=[]
    x=0
    while pow(2,x)<k:
        li=[0]*n
        if x==0:
            for i in range(n):
                li[i]=a[i]
            d.append(li)
        else:
            for i in range(n):
                j=d[x-1][i]
                li[i]=d[x-1][j]
            d.append(li)
        x+=1

    l=bin(k)[2:]
    x=len(l)
    ans=0
    for i in range(x):
        j=x-i-1
        if l[i]=="1":
            ans=d[j][ans]

    print(ans+1)

if __name__=="__main__":
    main()

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = []
ord = [-1 for i in range(n+1)]
v = 1
while ord[v] == -1:
    ord[v] = len(s)
    s.append(v)
    v = a[v-1]
c = len(s)-ord[v]
l = ord[v]
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()))
c=2
t=0
path=[]
s=set()
cycle=[]
while(c<=k):
    if a[t] in s:
        
        cycle=path[path.index(a[t]):]
        k-=c-1
        break
    path.append(a[t])
    s.add(a[t])
    t=a[t]-1
    c+=1
if cycle==[]:
    print(a[t])
else:
    print(cycle[k%len(cycle)])

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

DB = [[0] * (n + 1) for _ in range(max_k + 1)]

for j in range(1, n + 1):
    DB[0][j] = A[j]
for i in range(max_k):
    for j in range(1, n + 1):
        num = DB[i][j]
        DB[i + 1][j] += DB[i][num]
bit = bin(k)
num = 1
for i in range(max_k):
    if bit[-(i + 1)] == "1":
        num = DB[i][num]
print(num)

Solution (Python):


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

    c = 1
    while k:
        if k & 1:
            c = a[c]
        na = [0] * (n+1)
        for i in range(n+1):
            na[i] = a[a[i]]
        a = na
        k >>= 1

    print(c)

if __name__ == '__main__':
    resolve()

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

dic={}

i=0
dic[1]=i
next_town=1

while True:
    next_town=A[next_town-1]
    K -=1
    i +=1
    if K==0:
        print(next_town)
        break
    elif next_town in dic:
        lp_st=dic[next_town]
        lp_len=i-lp_st

        L=K%lp_len

        if L==0:
            print(next_town)
            break
        else:
            K=L

    else:
        dic[next_town]=i

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

time=0
color=["white" for _ in range(n+1)]
D=[-1 for _ in range(n+1)]
C,s=[],A[0]
point=0
while 1:
    if color[s]=="white":
        color[s]="gray"
        C.append(s)
        D[s]=time
        time +=1
        s=A[s-1]
    else:
        point=D[s]
        break

loop=C[point:]
if k>len(C):
    k -=point
    print(loop[k%len(loop)-1])
else:print(C[k-1])

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

here = 0
visited = set()
route = []

while here not in visited:
    visited.add(here)
    route.append(here)
    here = A[here]

loopStart = route.index(here)

if K <= loopStart:
    ans = route[K]
else:
    lenCycle = len(route) - loopStart
    ans = route[loopStart + (K - loopStart) % lenCycle]

print(ans + 1)

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

cache={}
log=[]
A = list(map(lambda x:int(x)-1, input().split(' ')))

now=0

while(True):
  log.append(now)
  cache[now]=1
  now=A[now]
  if(cache.get(now)):break

loop_start=log.index(now)
loop_end=len(log)
loop=loop_end-loop_start

if K<len(log):
  print(log[K]+1)
else:
  print(log[(K-loop_start)%loop+loop_start]+1)

Solution (Python):
import numpy as np 
from sys import stdin, setrecursionlimit
 

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

t = 1
tpass = [1]
k = 0
j = 1
temp3 = [0]*N
for i in range(N):
    temp = A[t-1] 
    if i == K-1:
        print(temp)
        exit()
    if temp3[temp-1] != 0 :
        idx = tpass.index(temp)
        l = len(tpass)
        j = -1
        break
    else:
        
        t = temp
        temp3[t-1] = 1
        tpass.append(temp)
        k += 1

try:
    a = K - k
    loop = l - idx
    b = a % loop 
    temp2 = tpass[idx:]
    ans = temp2[b-1]
    print(ans)
except:
    pass

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
d2 = []
hist=[-1]*(n+1)
hist[0] = 0
cur = 1
d2.append(1)
st_idx = 0
rp_lgt = 1
for i in range(n):
  cur = a[cur-1]
  if hist[cur-1] >= 0 :
     d2.append(cur)
     st_idx = hist[cur-1]
     rp_lgt = i - st_idx + 1 
     break
  else:
    d2.append(cur)
    hist[cur-1] = i + 1
wk = k - st_idx
md = wk % rp_lgt
if k < st_idx:
  ans = d2[k]
else:
  ans = d2[md+st_idx]
print(ans)

Solution (Python):

import sys

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

    count = 1
    now = 0
    visited = [1]
    set_v = set()
    set_v.add(1)
    l = -1
    while count < N:
        x = A[now]
        if x in set_v:
            l = visited.index(x)
            break
        else:
            count += 1
            visited.append(x)
            set_v.add(x)
            now = x-1
    r = count
    
    
    if K < l:
        print(visited[K])
    else:
        print(visited[l + (K-l) % (r-l)])

if __name__ == '__main__':
    main()

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
pl=1
visit=[-1]*n
visit[0]=0
loop=0
q=True
v=True
cnt=0

while q:
    cnt+=1
    pl=a[pl-1]
    if visit[pl-1]!=-1:
        loop=visit[pl-1]
        break
    visit[pl-1]=cnt
    if cnt==k:
        print(pl)
        v=not v
        break
    if cnt==10**7:
        q=not q
if v:
    loop_len=cnt-loop
    q=True
    k=(k-loop)%loop_len
    cnt=0
    if k==0:
        print(pl)
    else:
        while q:
            cnt+=1
            pl=a[pl-1]
            if cnt==k:
                print(pl)
                break
            if cnt==10**7:
                q=not q

Solution (Python):

import sys
import collections



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

    cur = 1
    c = {}
    route = []
    roop = None
    while (True):
        if cur in c:
            roop = cur
            break
        route.append(cur)
        c[cur] = 1
        cur = A[cur - 1]
    idx = route.index(roop)
    if K <= idx:
        print(route[K])
        return
    
    print(route[(K - idx) % (len(route) - idx) + idx])
    return


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
n,k=map(int, input().split())

A = deque(map(int, input().split()))
li = []
li2 = [0]*n
li.append(1)

next = 0

t=0
rep = False

for i in range(k):
    if i == k:
        print(next+1)
        exit()

    if li2[next] == 1:
        t = li.index(next+1)
        rep = True
        li.pop()
        break

    li.append(A[next])
    li2[next] = 1
    next = A[next]-1


tmp = (k-t)%len(li[t:])
print(li[tmp+t])

Solution (Python):
import sys
input = sys.stdin.readline
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    visited = {i:0 for i in range(N)}
    visited[0] = 1
    count = 0
    pre_count = 0
    cicle_idx = 0
    is_cicle = False
    idx = 0
    ans = -1
    while not is_cicle:
        count += 1
        if count == K:
            ans = A[idx]
        idx = A[idx]-1
        if visited[idx] == 1:
            visited[idx] = 2
            is_cicle = True
            cicle_idx = idx
            break
        visited[idx] = 1
    if ans == -1:
        cicle = []
        will_push = False
        while True:
            idx = A[idx]-1
            if idx == cicle_idx:will_push = True
            if len(cicle) > 0 and visited[idx] == 2:break
            if will_push:
                cicle.append(idx)
        pre_count = count - len(cicle)
        ans_idx = (K - pre_count) % len(cicle)
        print(cicle[ans_idx]+1)
    else:
        print(ans)
if __name__ == "__main__":
    main()

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

now = 1
check = [-1]*(n+1)

for i in range(k):
  if check[now] == -1:
    check[now] = i
    now = a[now]
  else:
    loop = i - check[now]
    afterloop = (k-(i-loop))%loop
    print(check.index((i-loop) + afterloop))
    exit()
print(now)

Solution (Python):
n,k = map(int,input().split())
a_list = list(map(int,input().split()))
a = 0
t = 1
d = 0
hoge_list = []
visit = [False for _ in range(n+1)]
while a < k:
  hoge_list.append(t)
  t = a_list[t-1]
  a += 1
  if visit[t]:
    b = hoge_list.index(t)
    d += 1
    break
  visit[t]=True
if d == 0:
  print(t)
else:
  e = (k-b)%(a-b)
  print(hoge_list[b+e])

Solution (Python):


import sys
read = sys.stdin.read
readline = sys.stdin.readline 

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

def pow2(a,n):
    if n==0: return list(range(n))
    if n==1: return a
    if n%2:
        return mul(a,pow2(a,n-1))
    else:
        x = pow2(a,n//2)
        return mul(x,x)

def mul(a,b):
    return [a[bi] for bi in b]

x = pow2(a,k)
print(x[0]+1)

Solution (Python):
n,k = map(int,input().split())
a_list = list(map(int,input().split()))
a = 0
t = 1
d = 0
hoge_list = []
hoge_set = set()
while a < k:
  hoge_list.append(t)
  hoge_set.add(t)
  t = a_list[t-1]
  a += 1
  if t in hoge_set:
    b = hoge_list.index(t)
    d += 1
    break
if d == 0:
  print(t)
else:
  e = (k-b)%(a-b)
  print(hoge_list[b+e])

Solution (Python):
def main():
    import sys
    sys.setrecursionlimit(10 ** 7)

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

    rs = [-1] * N

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

        if rest == 0:
            return cur + 1  

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

    print(dfs())


if __name__ == '__main__':
    main()

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
l = [1]
for i in range(n):  
    l.append(a[l[i]-1])
    
p = l.index(l[-1])
if k<=n:
  print(l[k])
  exit()
  
x = (k-n+1)%(n-p)
if x==0:
  print(l[n-1])
else:
  print(l[x+p-1])

Solution (Python):
import numpy as np 
from sys import stdin, setrecursionlimit
 
setrecursionlimit(10 ** 9)

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

t = 1
tpass = [1]
k = 0
j = 1
temp3 = [0]*N
for i in range(N):
    temp = A[t-1] 
    if i == K-1:
        print(temp)
        exit()
    if temp3[temp-1] != 0 :
        idx = tpass.index(temp)
        l = len(tpass)
        j = -1
        break
    else:
        
        t = temp
        temp3[t-1] = 1
        tpass.append(temp)
        k += 1

try:
    a = K - k
    loop = l - idx
    b = a % loop 
    temp2 = tpass[idx:]
    ans = temp2[b-1]
    print(ans)
except:
    pass

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

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

step = 1
start = 0
while(True):
    start += 1
    a1[step] += 1
    step = a[step]
    if a1[step] == 1:
        break
c = step
gap = 0
while(True):
    gap += 1
    step = a[step]
    if step == c:
        break
result = 1
if k > start:
    k = (k-start) % gap
    result = c
for _ in range(k):
    result = a[result]

print(result)

Solution (Python):
a,b=map(int,input().split())
c=[0]+list(map(int,input().split()))

d=set()
e=[]
mir=1
while 1:
  if mir in d:
    g=e.index(mir)
    f=e[g:]
    e=e[:g]
    break
  else:
    d.add(mir)
    e.append(mir)
    mir=c[mir]
if b<len(e):
  print(e[b])
else:
  b=b-len(e)
  print(f[b%(len(f))])

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

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

L = []
L.append(1)
f = 1
memo = [-1] * N
memo[0] = 0
cnt = 1

while 1:
    L.append(A[f-1])
    f = A[f-1]
    if memo[f-1] != -1:
        loop = cnt - memo[f-1]
        break
    memo[f-1] = cnt
    cnt += 1



NL = L[-(loop+1):-1]


if K <= (len(L) - 1) - loop:
    print(L[K])
else:
    K -= (len(L) - 1) - loop
    a = K % len(NL)
    print(NL[a])

Solution (Python):
N,K = map(int,input().split())
list1 = list(map(int,input().split()))
visited_town = [[] for i in range(N+1)] 
visited_time = [None for i in range(2*N)] 

visited_town[1].append(0) 
visited_time[0] = 1

cycle_start = -1
cycle_end = -1

for i in range(1,2*N):
  next_town = list1[visited_time[i-1]-1]
  visited_town[next_town].append(i)
  visited_time[i] = next_town
  if len(visited_town[next_town]) == 2:
    cycle_start = visited_town[next_town][0]
    cycle_end   = visited_town[next_town][1]
    break

if K < cycle_start:
  ans = visited_time[K]
  print(ans)
else :   
  syuuki = cycle_end-cycle_start
  cycle = visited_time[cycle_start:cycle_end]
  ans = cycle[(K-cycle_start) % syuuki]
  print(ans)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
def get_cycle_length():
  pointer1 = 1
  pointer2 = a[1-1]
  while pointer1 != pointer2:
    pointer2 = a[pointer2-1]
    pointer2 = a[pointer2-1]
    pointer1 = a[pointer1-1]
  cycle_length = 1
  pointer2 = a[pointer2-1]
  while pointer1 != pointer2:
    pointer2 = a[pointer2-1]
    cycle_length += 1
  return cycle_length
def get_start_length(cycle_length):
  pointer1 = 1
  pointer2 = 1
  for _ in range(cycle_length):
    pointer2 = a[pointer2-1]
  start_length = 0
  while pointer1 != pointer2:
    start_length += 1
    pointer1 = a[pointer1-1]
    pointer2 = a[pointer2-1]
  return start_length
if k>n:
  cycle_length = get_cycle_length()
  start_length = get_start_length(cycle_length)
  remainder_steps = k - start_length
  modded_remainder = remainder_steps % cycle_length
  k = start_length + modded_remainder
town = 1
for _ in range(k):
  town = a[town-1]
print(town)

Solution (Python):
import os

import sys
import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7



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

A = np.array(A, dtype=int) - 1

dp = np.zeros((K.bit_length(), N), dtype=int)
dp[0] = A
for h in range(len(dp) - 1):
    dp[h + 1] = dp[h, dp[h]]

ans = 0
for i in reversed(range(K.bit_length())):
    if K >> i & 1:
        ans = dp[i, ans]
print(ans + 1)

Solution (Python):
import sys
def inpl(): return list(map(int, sys.stdin.readline().split()))
N, K = map(int, sys.stdin.readline().split())
A = inpl()
dic = {}
for i in range(N):
    dic[i+1] = A[i]
i = 1
visit = []
visit_set = set()
while i not in visit_set:
    visit.append(i)
    visit_set.add(i)
    i = dic[i]
find = visit.index(i)
if K < find:
    print(visit[K])
else:
    root = visit[find:]
    length = len(root)
    K = (K-find) % length
    print(root[K])

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

flag_list = [float('INF') for n in range(N+1)]

myseries = []

myflag = 0
mypos = 1
mycnt = 0 
while myflag == 0:
    if flag_list[mypos] == float('INF'):
        flag_list[mypos] = mycnt
        
        myseries.append(mypos)
        mypos = mylist[mypos]
        mycnt += 1
    else:
        myflag = 1
        mystart = flag_list[mypos]
        mylen = mycnt-mystart




if K < mystart:
    print(myseries[K])
else:
    print(myseries[(K-mystart)%mylen + mystart])

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

time=0
color=["white" for _ in range(n+1)]
D=[-1 for _ in range(n+1)]
C,s=[],A[0]
point=0
while 1:
    if color[s]=="white":
        color[s]="gray"
        C.append(s)
        D[s]=time
        time +=1
        s=A[s-1]
    else:
        point=D[s]
        break

loop=C[point:]
if k>len(C):
    k -=point+1
    print(loop[k%len(loop)])
else:print(C[k-1])

Solution (Python):
if __name__=="__main__":
    N, K = map(int, input().split(' '))
    _town_list = input().split(' ')
    town_list = [int(n) for n in _town_list]

    next_town = town_list[0]
    visited_town = set()
    visited_town.add(1)
    visited_town.add(next_town)
    lst_visited_town = [1]
    lst_visited_town.append(next_town)
    counter = 1

    while True:
        counter += 1
        next_town = town_list[next_town-1]
        if counter == K:
            print(next_town)
            break
        if next_town in visited_town:
            repeated_town_idx = lst_visited_town.index(next_town)
            loop_num = counter - repeated_town_idx
            print(lst_visited_town[(K-repeated_town_idx)%loop_num + repeated_town_idx])
            break
        lst_visited_town.append(next_town)
        visited_town.add(next_town)

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

loc = 1
loclog=[1]
flag = 0
hist = [0]*N

for i in range(K):
    hist[loc-1] = 1
    loc = A[loc-1]
    if hist[loc-1] != 0:
        roopstart = loclog.index(loc)
        roopend = len(loclog)
        flag = 1
        break
    loclog.append(loc)

if flag == 0:
    ans = loc
else:
    roopidx = (K+1-roopstart)%(roopend-roopstart)
    if roopidx == 0:
        ans = loclog[roopend-1]
    else:
        ans = loclog[roopstart+roopidx-1]
print(ans)

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

G = {}
for i in range(1, n + 1):
    G.update({i: a[i - 1]})

seen = [False] * (n + 1)
path = [1]

i = 1
while True:
    seen[i] = True
    i = G[i]
    
    if seen[i]:
        break
    else:
        path.append(i)

m = path.index(i)

if m > k:
    print(path[k])
else:
    k -= m
    path = path[m:]
    k %= len(path)
    print(path[k])

Solution (Python):
D = 60

N, K = map(int, input().split())
to = [[None] * N for _ in range(D)]
to[0] = list(map(lambda x: int(x)-1, input().split()))

for i in range(D-1):
    for j in range(N):
        to[i+1][j] = to[i][to[i][j]]

v = 0
for i in range(D, -1, -1):
    hops = 1 << i
    if K >= hops:
        K -= hops
        v = to[i][v]

print(v+1)

Solution (Python):
n, k = map(int, input().strip().split())
L = list(map(int, input().strip().split()))
L = [v - 1 for v in L]
 
seen = set()
intersec = 0
route=[]
while intersec not in seen:
    seen.add(intersec)
    route.append(intersec)
    intersec = L[intersec]

intersecInd = route.index(intersec)

if k<=intersecInd:
    ans=route[k]
else:
    loopSize = len(route) - intersecInd
    ans = route[(k - intersecInd) % (loopSize) + intersecInd]
 
print(ans + 1)

Solution (Python):
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    A = [int(s) for s in input().split()]

    v = 1
    s = []
    order = [-1] * (N + 1)
    while order[v] == -1:
        order[v] = len(s)
        s.append(v)
        v = A[v - 1]
    l = order[v]
    c = len(s) - order[v]

    if K < l:
        print(s[K])
    else:
        print(s[l + (K - l) % c])

    return


if __name__ == '__main__':
    main()

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

flag=[0]*N
patt=[]
aaaa=0
i=0
while True:
    patt.append(i)
    if flag[i]==1:
        
        break
    flag[i]=1
    i=A[i]-1
 
 
srt = patt.index(patt[-1])

end = len(patt)-2
ptrn = patt[srt:end+1]
 
if K<srt:
    print(patt[K]+1)
else:
    B = (K-srt) % len(ptrn)
    
 
    print(ptrn[B]+1)

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

journey = [1]

for i in range(2 * n + 100):
    next = a[journey[-1]]
    journey.append(next)

if k < n+1:
    print(journey[k])
    exit()

loop_end = n

loop_start = n - 1

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

period = loop_end - loop_start
k %= period

while k < n:
    k += period

print(journey[k])