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())
pl=list(map(int,input().split()))
cl=[0]*2*n
order=[]
next=0
for i in range(k):
  if cl[next]==0:
    cl[next]=1
    next=pl[next]-1
  elif cl[next]==1:
    cl[next]=2
    order.append(next+1)
    next=pl[next]-1
  else:
    kk=len(order)
    hasuu=cl.count(1)
    newk=k-hasuu
    ans=newk%kk
    print(order[ans])
    break
else:
  print(next+1)

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

stop = False

used = set()
current = 1
old = -1
route = []
route_b4 = []
neverloop = True

for i in range(n):
    if old == current:
        stop == True
    if current in used:
        for i,city in enumerate(route):
            if city == current:
                route = route[i:]
                neverloop = False
            route_b4.append(city)

        break
    route.append(current)
    used.add(current)
    old = current
    current = a[current]
if neverloop == True:
    route_b4 = []
if stop==True:
    if len(route_b4) + len(route) >= k:
        if len(route_b4) > k:
            
            print(route_b4[k])
        else:
            print(route[k-len(route_b4)])
    else :
        print(route[len(route)-1])
else:
    if len(route_b4) + len(route) >= k:
        if len(route_b4) > k:
            print(route_b4[k])
        else:
            print(route[k-len(route_b4)])
    else:
        total = k + len(route_b4)
        print(route[(k-len(route_b4))%len(route)])

Solution (Python):
n,k = map(int,input().split())
a_list = list(map(int,input().split()))
roop_town = None
town_list = [1]
town_set = set([1])
for cur_town in town_list:
    if a_list[cur_town-1] in town_set:
        roop_town = a_list[cur_town-1]
        break
    town_list.append(a_list[cur_town-1])
    town_set.add(a_list[cur_town-1])
if roop_town == None:
    print(town_list[k])
else:
    roop_index = town_list.index(roop_town)
    roop_num = len(town_list) - roop_index
    if k<=roop_index:
        print(town_list[k])
    else:
        k_rooped_index = (k-roop_index)%roop_num
        print(town_list[roop_index+k_rooped_index])

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
route = [1]
for i in range(N):
  route.append(A[route[i]-1])
prepart = route.index(route[-1])
cycle = len(route) - prepart - 1
if K > N:
  K =(K-prepart)%cycle +prepart
print(route[K])

Solution (Python):
n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
town=[0]
set={0}
i=0
while True:
  idx=town[-1]
  if a[idx] in set:
    s=a[idx]
    i+=1
    break
  else:
    i+=1
    town.append(a[idx])
    set.add(a[idx])
if i!=n:
  idp=town.index(s)
  chain=len(town[:idp])
  cycle=len(town[idp:])
  if k<=chain:
    print(town[k]+1)
  else:
    p=(k-chain)%cycle + chain
    print(town[p]+1)
else:
  print(town[k%n]+1)

Solution (Python):
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    to = [[None]*n for _ in range(60)]
    to[0] = a[:]
    for i in range(1, 60):
        for j in range(n):
            to[i][j] = to[i-1][to[i-1][j]-1]
    p = 1
    for i in range(59, -1, -1):
        num = 1 << i
        if k >= num:
            k -= num
            p = to[i][p-1]
    print(p)

if __name__ == "__main__":
    main()

Solution (Python):
n,k=map(int,input().split())
pl=list(map(int,input().split()))
cl=[0]*2*n
res=[]
next=0
for x in range(k):
  if cl[next]==0:
    cl[next]=1
    next=pl[next]-1
  elif cl[next]==1:
    res.append(next+1)
    cl[next]+=1
    next=pl[next]-1
  else:
    lroop=len(res)
    k-=cl.count(1)
    num=k%lroop
    print(res[num])
    break
else:
  print(next+1)

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


def main():
    [n, k] = [int(e) for e in si().split()]
    trace = dict()
    for i in range(1, n+1):
        trace[i] = 0

    l = [int(e) for e in si().split()]
    i, cnt = 1, 0
    while k > 0:
        k -= 1
        cnt += 1
        i = l[i-1]
        if i == 1 or trace[i] != 0:
            cy = cnt-trace[i]
            k %= cy
            break
        trace[i] = cnt
    while k > 0:
        i = l[i-1]
        k -= 1
    print(i)


if __name__ == '__main__':
    main()

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

num = 1
li = [1]
flag = [True] * n
flag[0] = False

for i in range(k):
    num = a[num]
    if flag[num-1]:
        li.append(num)
        flag[num-1] = False
    else:
        break

d = li.index(num)
ans = (k-d)%(len(li)-d)+d

print(li[ans])

Solution (Python):
N, K = map(int, input().split())
Nlist = {}
Alist = {}
visit = {}
count = 1
for a in input().split():
    a = int(a)
    Alist[count] = a
    count = count+1

count = 1
visit[0] = 1
Nlist[1]=0
while True:
    if Alist[visit[count-1]] in Nlist:
        memo = Alist[visit[count-1]]
        break
    visit[count] = Alist[visit[count-1]]
    Nlist[visit[count]] = count
    count = count+1

if K < count:
    print(visit[K])
else:

    loop = count-Nlist[memo]
    K = K-(count-loop)
    K = K % loop
    K = K+Nlist[memo]
    print(visit[K])

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

dist = [-1 for i in range(n)]
dist[0] = 0

now_town = 0
freq = 0
for i in range(k):
    next_town = a[now_town] - 1
    if dist[next_town] != -1:
        freq = (i+1) - dist[next_town]
        const = dist[next_town]
        break
    dist[next_town] = i + 1
    now_town = next_town

if freq == 0:  
    print(next_town+1)
else:  
    freq_ = (k - const) % freq
    if freq_ == 0:
        print(next_town+1)
    else:
        now_town = next_town
        for i in range(freq_):
            now_town = a[now_town] - 1
        print(now_town+1)

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

dist = [-1 for i in range(n)]
dist[0] = 0

now_town = 0
flg = 0  
for i in range(k):
    
    next_town = a[now_town] - 1
    if dist[next_town] != -1:
        flg = 1
        diff = (i+1) - dist[next_town]
        const = dist[next_town]
        break
    dist[next_town] = i + 1
    now_town = next_town





if flg == 0:
    print(next_town+1)
elif flg == 1:
    c = (k - const) % diff
    
    
    if c == 0:
        print(next_town+1)
    else:
        now_town = next_town
        
        for i in range(c):
            next_town = a[now_town] - 1
            now_town = next_town
            
        print(now_town+1)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
l1=set([a[0]])
num1=a[0]
num2=1
l2=[]
def al(num):
    l1.add(num)
def n2(num5):
    return num5 in l1
for i in range(n):
    num1=a[num1-1]
    al(num1)
    if num2==len(l1):
        break
    else:
        num2=len(l1)
l1=list(l1)
num=l1.index(num1)
l2=l1[num:]
l1=l1[:num]
num1=len(l1)
num2=len(l2)
if k<=num1:
    print(l1[k-1])
else:
    num3=(k-num1)%num2
    print(l2[num3-1])

Solution (Python):
import sys
from collections import deque, defaultdict
N, K = map(int, input().split())
A = [int(i) - 1 for i in input().split()]

s = set()
s.add(0)
cur = 0
num = 0
d = defaultdict(int)
d[0] = 0
ls = [0]
for i in range(K):
    if A[cur] not in s:
        cur = A[cur]
        num += 1
        d[cur] = i+1
        ls.append(cur)
        s.add(cur)
    else:
        cur = A[cur]
        num += 1
        edges_heiro = num - d[cur]
        nokori = K - num
        print(ls[d[cur] + nokori%edges_heiro] + 1)
        sys.exit()
print(cur + 1)

Solution (Python):
import sys
import time
st = time.perf_counter()
def inpl():
    return list(map(int, input().split()))


N, K = map(int, input().split())
A = list(map(lambda a: int(a)-1, input().split()))
T = [A]

for i in range(59):
    prev = T[-1]
    T.append([prev[a] for a in prev])

now = 0
for k,v in enumerate(T):
    if ((1<<k)&K)>0:
        now = v[now]
print(now+1)



ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

Solution (Python):
N, K = map(int, input().split())
A = list(map(lambda a: int(a) - 1, input().split()))
 
T = [A]
for _ in range(59):
    prev = T[-1]
    T.append([prev[a] for a in prev])
 
now = 0
for d, P in enumerate(T):
    if ((1 << d) & K) > 0:
        now = P[now]
print(now + 1)

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

alr=[False]*n
alr[0]=True
now_in=0

if k<=2*(10**5)+1:
    for i in range(k):
        now_in=a[now_in]-1
    print(now_in+1)
    sys.exit()


for i in range(n+1):
    now_in=a[now_in]-1
    if alr[now_in] is False:
        alr[now_in]=True
    else:
        start=now_in
        break

count_r=0
now_in=start
for i in range(2*(10**5)+1):
    now_in=a[now_in]-1
    count_r+=1
    if now_in==start:
        break

now_in=0
count=0
while now_in!=start:
    now_in=a[now_in]-1
    count+=1

c=k-count
plus=c%count_r
for i in range(plus):
    now_in=a[now_in]-1

print(now_in+1)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
was = []
tf = [False] * N
place = 1
cnt = 0

while tf[place-1] == False:
  if cnt < K:
    tf[place-1] = True
    was.append(place)
    place = A[place-1]
    cnt += 1
  else:
    print(place)
    break;
else:
  loop = cnt - was.index(place)
  num = (K-cnt) % loop + was.index(place)
  print(was[num])

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

town = 0
cnt = 0

visited = [False] * n
visited[0] = -1

start_cnt = 0
for i in range(n*2):
    town = A[town]
    if visited[town] != False:
        start_cnt = i + 1
        loop = i - visited[town]
        break
    else:
        visited[town] = i

town = 0

if k >= start_cnt:
    k = (k-start_cnt)%loop+start_cnt

for i in range(k):
    town = A[town]

print(town+1)

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

town = 0
cnt = 0























visited = [False] * n
visited[0] = 0

for i in range(n):
    town = A[town]
    if visited[town]:
        good_town = town
        break
    visited[town] = i+1

town = good_town

for i in range(n):
    town = A[town]
    if town == good_town:
        loop = i + 1

if k >= visited[good_town]:
    k = ((k-visited[good_town])%loop)+visited[good_town]


town = 0
for i in range(k):
    town = A[town]
print(town+1)

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

visited = [-1]*(n+1)
pos = 1
visited[pos] = 0
loop = 0
root=[1]
for i in range(1,n+1):
  pos = a[pos-1]
  if visited[pos] == -1:
    visited[pos] = i
    root.append(pos)
  else:
    loop = i - visited[pos]
    mae = visited[pos]
    break
if k < mae:
  print(root[k])
else:
  index=(k-mae)%loop
  print(root[index+mae])

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

N,K = map(int,readline().split())
A = [0] + list(map(int,readline().split()))

pos = 1

while K:
  if K & 1:
    pos = A[pos]
  A = [A[A[i]] for i in range(N + 1)]
  K >>= 1
  
print(pos)

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

t = [[-1] * N for _ in range(61)]
t[0] = [A[i] - 1 for i in range(N)]
for k in range(1, 61):
    for i in range(N):
        t[k][i] = t[k - 1][t[k - 1][i]]

u = 0
for k in range(61):
    if K >> k & 1:
        u = t[k][u]

print(u + 1)

Solution (Python):

n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = [1]
c = 1
for i in range(n):
    ans.append(a[ans[i] - 1])
b = ans.index(ans[n])
if k < b:
    for p in range(k):
        c = a[c - 1]
else:
    for p in range((k-b) % (n-b)+b):
        c = a[c - 1]
print(c)

Solution (Python):
n, k = map(int, input().split())
A = list(map(lambda x: x-1, map(int, input().split())))
isVisit = [False]*n
visits = []
visit = 0
for i in range(n):
    if isVisit[visit]:
        break
    else:
        isVisit[visit] = True
        visits.append(visit)
        visit = A[visit]

if len(visits)>k:
    print(visits[k]+1)
else:
    k -= visits.index(visit)
    visits = visits[visits.index(visit):]
    print(visits[k%len(visits)]+1)

Solution (Python):
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
tmp = [0 for _ in range(N)]


spot = 1
road = [1]
tmp[0] = 1
for _ in range(K):
    n_s = A[spot-1]
    if not tmp[n_s-1] == 1:
        road.append(n_s)
        tmp[n_s-1] = 1
        spot = n_s
    else:
        spot = n_s
        break

till_loop = road.index(spot)

loop = road[till_loop:]

cycle_len = len(road) - road.index(spot)

answer = loop[(K-till_loop) % cycle_len]

print(answer)

Solution (Python):
def main():
    n, k = list(map(int, input().split()))
    a_list = list(map(int, input().split()))
    
    roop_count = []
    for i in range(n):
        roop_count.append(0)
    
    town = 1
    town_candidate = []
    roop_count[0] = 1
    roop_1 = 0
    roop_2 = 0
    count = 0
    for i in range(k):
        town = a_list[town-1]
        roop_count[town-1] += 1
        if roop_count[town-1] == 1:
            roop_1 += 1
        elif roop_count[town-1] == 2:
            roop_2 += 1
            town_candidate.append(town)
        elif roop_count[town-1] == 3:
            break
        count = i
    
    
    if count == k-1:
        print(town)
    else:
        town = (k-roop_1) % roop_2 -1
        print(town_candidate[town])

if __name__ == '__main__':
    main()

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

curTown=1
k=0
while k<K:
    lastVisited[curTown-1]=k
    k+=1
    curTown=A[curTown-1]
    if lastVisited[curTown-1]>=0:
        p=k-lastVisited[curTown-1]
        k+=((K-k)//p)*p
        lastVisited=[-1]*N
        
print(curTown)

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

goto = [-1] + list(map(int, input().split()))


l = [[_, 0] for _ in range(2*n)] 

cnt = [0 for _ in range(n+1)] 

tmp = 1
for i in range(1, 2*n):
    l[i][1] = tmp
    cnt[l[i][1]] += 1
    tmp = goto[tmp]
    if cnt[l[i][1]] == 2:
        x = l[i]
        break



for j in range(n):
    if l[j][1] == x[1]:
        y = l[j]
        break


if y[0] > k:
    print(l[k+1][1])
else:
    z = (k-y[0]+1) % (x[0]-y[0]) + y[0]
    print(l[z][1])

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

d = [0] * n
d[0] = 1
t = a[0]
cnt = 0
r = [1]

while d[t-1] == 0:
    cnt += 1
    d[t-1] = cnt + 1
    if k == cnt:
        print(t)
        exit()
    r.append(t)
    t = a[t-1]

rp = r[d[t-1]-1:]
b = (k-cnt) % len(rp)
print(rp[b-1])

Solution (Python):
n, k = map(int, input().split())
a = list(map(lambda x:int(x)-1, input().split()))
reached = [0 for i in range(n)]
trace = [0]
here = 0
reached[0] = 1
while reached[a[here]] != 1:
    trace.append(a[here])
    here = a[here]
    reached[here] = 1
here = a[here]
loop_start = trace.index(here)
l = len(trace)
loop = l - loop_start
if k < loop_start:
    print(trace[k]+1)
else:
    k -= loop_start
    print(trace[k % loop + loop_start]+1)

Solution (Python):
n, k = list(map(int, input().split()))
trans = list(map(int, input().split()))
trans = [-1] + trans
dest = 1
seen = [False] * (n+1)
start = 0
output = 0
t = []
for _ in range(k):
  dest = trans[dest]
  if seen[dest]:
    for i, x in enumerate(t):
      if x == dest:
        start = i
    break
  seen[dest] = True
  t.append(dest)
if len(t) < k:
  s = t[start:]
  output = s[(k - len(t[:start]) - 1) % len(s)]
else:
  output = t[k-1]
print(output)

Solution (Python):

"""
Created on Sat Jun 20 10:11:22 2020

@author: NEC-PCuser
"""

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

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

tele_set = set()
tele_set.add(1)
junkai_town = -1 
junkai_first = -1
town = 1
for i in range(N):
    if A[town - 1] not in tele_set:
        tele_set.add(A[town - 1])
    else:
        junkai_town = A[town - 1]
        break
    town = A[town - 1] 

town = 1
start_flg = False
junkaimae_list = []
junkai_list = []
for i in range(N):
    if town == junkai_town and start_flg == False:
        junkai_first = i
        start_flg = True
    elif town == junkai_town:
        break
    if start_flg == True:
        junkai_list.append(town)
    else:
        junkaimae_list.append(town)
    town = A[town - 1]


if K < len(junkaimae_list):
    print(junkaimae_list[K])
else:
    kaisu = K - len(junkaimae_list)
    print(junkai_list[kaisu % len(junkai_list)])

Solution (Python):
import sys
import time
st = time.perf_counter()


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

cnt = [-1] * N
cnt[0] = 0
now = 0
for i in range(1, 1001001001):
    now = A[now]
    if cnt[now] == -1:
        cnt[now] = i
    else:
        circle = i - cnt[now]
        t = cnt[now]
        break
if K - t > 0:
    amari = (K - t) % circle
    for _ in range(amari):
        now = A[now]
else:
    now = 0
    for i in range(K):
        now = A[now]
print(now + 1)


ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

Solution (Python):
import sys
N,k=map(int,input().split())
blist=[0]+list(map(int,input().split()))

checked_list=[-1]*(N+1)
pos=1
t=0
while True:
  if t==k:
    print(pos)
    sys.exit(0)
  elif checked_list[pos]!=-1:
    break
  
  checked_list[pos]=t
  pos=blist[pos]  
  t+=1
  

cycle=t-checked_list[pos]

pos_in_cycle=(k-checked_list[pos])%cycle

k2=checked_list[pos]+pos_in_cycle


print(checked_list.index(k2))

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

    num = 1
    l = []
    s = [1]*n

    while s[num-1]:
        l.append(num)
        s[num-1] = 0
        num = a[num-1]

    if l.index(num) <= k:
        l2 = l[l.index(num):]
        kk = (k-len(set(l)-set(l2)))%len(l2)
        print(l2[kk])
    else:
        print(l[k])

main()

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

p = 0

if n>=k:
    for i in range(k):
        p = a[p]
    print(p+1)

else:
    lst = [-1]*n
    lst[0] = 0
    
    for i in range(1,n+1):
        p = a[p]
        if lst[p] == -1:
            lst[p] = i
        else:
            loop = i - lst[p]
            k -= lst[p]
            extra = k % loop
            break

    for i in range(extra):
        p = a[p]
    print(p+1)

Solution (Python):

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

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


arrived={i:False for i in range(1,n+1)}

city=[]
before=1
while True:
    nxt=al[before-1]
    if arrived[nxt]:
        break
    arrived[nxt]=True
    city.append(nxt)
    before=nxt
ind=city.index(nxt)
loop=city[ind:]
if k<=ind:
    ans=city[k-1]
else:
    k-=ind
    ans=loop[k%len(loop)-1]
print(ans)

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

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

used = [False] * N
loop = [0]  
now = 0  
while not used[A[now]]:
    used[A[now]] = True
    loop.append(A[now])
    now = A[now]

ind = loop.index(A[now])

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

    print(loop[ind+K]+1)

Solution (Python):

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

Solution (Python):
def main():
    n, k = map(int, input().split()) 
    al = list(map(int, input().split())) 
    city_l = [1]
    city_s = set([1])
    loop_city_l = []
    curr_city = 1
    cnt = 0
    for i in range(1,k+1):
        next_c = al[curr_city-1]
        if next_c in city_s:
            cnt = i
            first_city = city_l.index(next_c)
            loop_city_l = city_l[first_city:]
            break
        else:
            city_s.add(next_c)
            city_l.append(next_c)
        curr_city = next_c
    else:
        print(curr_city)
        return

    
    remained = k-cnt
    last_pos = remained%len(loop_city_l)
    print(loop_city_l[last_pos])

if __name__ == "__main__":
    main()

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

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

gtyo = [0] *n
gtyo[0] = 1

gone = 0
t = 1

while t < n:
    gone = lis[gone] -1
    t += 1
    if gtyo[gone]:
        loop = t -gtyo[gone]
        t = n
    else:
        gtyo[gone] = t
        
if k -(gtyo[gone]-1)>= 0:
    res = (k- (gtyo[gone]-1)) % loop
else:
    res = k- (gtyo[gone]-1)
    
tmp = [i - gtyo[gone] for i in gtyo]

print(tmp.index(res)+1)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
dist = [-1] * n
dist[0] = 0
now = 0
while True:
    next = a[now] - 1
    if dist[next] == -1:
        dist[next] = dist[now] + 1
    else:
        hajimari = next
        loopnum = dist[now] + 1 - dist[hajimari]
        town = 0
        if k <= dist[hajimari]:
            for _ in range(k):
                town = a[town] - 1
        else:
            town = hajimari
            for _ in range((k - dist[hajimari]) % loopnum):
                town = a[town] - 1
        print(town + 1)
        exit()
    now = next

Solution (Python):
from collections import deque
n, k = map(int, input().split())
a = list(map(int, input().split()))
g = [i for i in range(n)]
for i in range(n):
    g[i] = a[i]-1
dist =[-1]*n
dist[0] = 0
q = deque([0])
hajimari = -1
loopnum = -1
while q:
    v = q.popleft()
    u = g[v]
    if dist[u] == -1:
        dist[u] = dist[v]+1
        q.append(u)
    else:
        hajimari = u
        loopnum = dist[v] + 1 - dist[u]
        break
    if hajimari != -1:
        break
if k <= dist[hajimari]:
    now = 0
    for _ in range(k):
        now = g[now]
    print(now+1)
else:
    now = hajimari
    for _ in range((k-dist[hajimari])%loopnum):
        now = g[now]
    print(now+1)

Solution (Python):
n, k = map(int, input().split())
li = list(map(int, input().split()))
 
check = [0] * (n)
res = []
next = 0
 
for i in range(k):
    if check[next] == 0:
        check[next] = 1
        next = li[next]-1
    elif check[next] == 1:
        check[next] += 1
        res.append(next+1)
        next = li[next]-1
    else:
        cnt = len(res)
        k -= check.count(1)
        amari = k%cnt
        print(res[amari])
        break
else:
    print(next+1)

Solution (Python):
n, k = map(int, input().split())
li = list(map(int, input().split()))
 
check = [0] * (n+10)
res = []
next = 0
 
for i in range(k):
    if check[next] == 0:
        check[next] = 1
        next = li[next]-1
    elif check[next] == 1:
        check[next] += 1
        res.append(next+1)
        next = li[next]-1
    else:
        cnt = len(res)
        k -= check.count(1)
        amari = k%cnt
        print(res[amari])
        break
else:
    print(next+1)

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

num = 1
li = [1]
flag = [True] * n
flag[0] = False

for i in range(k):
    num = a[num]
    if flag[num-1]:
        li.append(num)
        flag[num-1] = False
    else:
        break

d = li.index(num)
ans = (k-d)%(len(li)-d)+d

print(li[ans])

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

num = 1
li = [1]
flag = [True] * n
flag[0] = False

for i in range(k):
    num = a[num-1]
    if flag[num-1]:
        li.append(num)
        flag[num-1] = False
    else:
        break

d = li.index(num)
ans = (k-d)%(len(li)-d)+d

print(li[ans])

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

Root = []
Visited = [0 for _ in range(N+1)]

now = 1
after = A[now]
while Visited[now] == 0:
    Root.append(now)
    Visited[now] = 1
    now = after
    after = A[now]
ind = Root.index(now)
Init = Root[:ind]
Loop = Root[ind:]

if K <= len(Init)-1:
    print(Init[K])
else:
    print(Loop[(K-len(Init))%len(Loop)])

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))
kari=set()
hako=set()
count=1
cnt=1
zahyo=0
ans=0
kari.add(zahyo)
if K<=10**6:
    for i in range(K):
        ans=A[ans]-1
    print(ans+1)
    exit()
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in kari:
        kari.add(zahyo)
        count+=1
    else:
        break

hako.add(zahyo)
for i in range(N):
    zahyo=A[zahyo]-1
    if zahyo not in hako:
        hako.add(zahyo)
        cnt+=1
    else:
        break
count=len(kari)
cnt=len(hako)
kaisu=K-count

if kaisu>N:
    kaisu=kaisu%cnt
for i in range(kaisu):
    zahyo=A[zahyo]-1


print(zahyo+1)

Solution (Python):
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
alist = list(map(lambda x:int(x) - 1,input().split()))
count = [0]*n
count[0] += 1
now = 0
order = [0]
for i in range(k):
    now = alist[now]
    count[now] += 1
    if count[now] == 2:
        start = order.index(now)
        loop = len(order[start:])
        syoki = len(order[:start])
        break
    order.append(now)
else:
    print(now+1)
    exit()
k -= syoki
k %= loop
print(order[k+syoki] + 1)

Solution (Python):
n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]
if k <= n:
  p = 0
  for i in range(k):
    p = a[p]
  print(p + 1)
  exit()
p = 0
t = [-1]*n
t[0] = 0
for i in range(1, n):
  p = a[p]
  if t[p] != -1:
    break
  t[p] = i
d = i - t[p]
k -= i
k %= d
for i in range(k):
  p = a[p]
print(p + 1)

Solution (Python):
import sys 
from collections import deque
sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def Main():
    n,k = IL()
    a = list(IL())
    chk = [0]*n
    save = []
    current = 0
    for step in range(1,k+1):
        if chk[current]<2:
            if chk[current]==1:
                save.append(current+1)
            chk[current] += 1
            current = a[current]-1
        else:
            break
    else:
        print(current+1)
        exit()
    x = k-step+1
    x %= len(save)
    print(save[x])
    return

if __name__=='__main__':
    Main()

Solution (Python):
import sys

readline = sys.stdin.buffer.readline


def solver(N, K, A):
    i = 0
    i_list = [0]
    reached = [False for _ in range(N)]

    while True:
        next_i = A[i]
        if reached[next_i]:
            break

        reached[next_i] = True
        i_list.append(next_i)
        i = next_i

    m = i_list.index(next_i)

    if m >= K:
        return i_list[K] + 1

    le = len(i_list) - m
    K -= m
    ind = K % le

    return i_list[m + ind] + 1


def run():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    A = [a - 1 for a in A]

    ans = solver(N, K, A)
    print(ans)


run()

Solution (Python):
import sys

readline = sys.stdin.buffer.readline


def solver(N, K, A):
    i = 0
    i_list = [0]
    reached = [False for _ in range(N)]

    while True:
        next_i = A[i]

        if reached[next_i]:
            break

        reached[next_i] = True

        i_list.append(next_i)
        i = next_i

    m = i_list.index(next_i)

    if m >= K:
        return i_list[K] + 1

    le = len(i_list) - m
    K -= m
    ind = K % le

    return i_list[m + ind] + 1


def run():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    A = [a - 1 for a in A]

    ans = solver(N, K, A)
    print(ans)


run()

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

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

reached = []

check = [0]*(N+1)
now = 1


for _ in range(N):
    check[now]+=1
    if check[now] == 1 :
        reached.append(now)
        now = A[now-1]

    if check[now] > 1:
        break



for i in range(len(reached)):
    if reached[i]== now:
        cnt = i
        break


roop = len(reached)- cnt
init = len(reached) 

if K < init:
    print(reached[K])
else:
    print(reached[(K-init)%roop+cnt])

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
l = [0 for _ in range(N)]

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

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

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

Solution (Python):


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

cnt = 0
temp = []

d = [0]*(2*(10**5) + 1)

now = 1
while True:
  d[now] += 1
  if d[now] > 1:
    break

  temp.append(now)
  now = A[now-1]


for t in temp[::-1]:
  cnt += 1
  if t == now:
    break

temp2 = temp[-cnt::]

if len(temp) <= K:
  kk = (K-(len(temp)-cnt))%cnt
  print(temp2[kk])
else:
  print(temp[K])

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

    num = 1
    l = []
    s = [1]*n

    while s[num-1]:
        l.append(num)
        s[num-1] = 0
        num = a[num-1]

    if l.index(num) <= k:
        l2 = l[l.index(num):]
        kk = (k-len(set(l)-set(l2)))%len(l2)
        print(l2[kk])
    else:
        print(l[k])

main()

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

isSearched = [False]*n
isSearched[0] = True
ans = []
def f():
    global k
    now = 0
    nex = A[0]
    ans.append(now)
    for i in range(k):
        now = nex
        ans.append(now)
        isSearched[now] = True
        nex = A[nex]
        if(isSearched[nex]):
            break

    if((len(ans)-1)==k):
        print(ans[-1]+1)
        return
    for i in range(len(ans)):
        if(ans[i]==nex):
            savei = i
    k -= savei
    mod = len(ans) - savei
    print(ans[savei+k%mod]+1)

f()

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
visit=[0]*n
visit[0]=1
now=0
c=0
for i in range(k):
  now_=now
  now=a[now]-1
  if visit[now]==0:
    visit[now]=visit[now_]+1
  else:
    c=1
    break
if c==1:
  t=(k+2-visit[now])%(visit[now_]-visit[now]+1)
  if t==0:
    print(now_+1)
  else:
    i=now
    for _ in range(t-1):
      i=a[i]-1
    print(i+1) 
else:
  u=now+1
  print(u)

Solution (Python):
import sys
n, k = [int(i) for i in sys.stdin.readline().split()]
a_s = [int(i)-1 for i in sys.stdin.readline().split()]
cnt = 0
ind = 0
ls = [ind]
last_pos = {0:0}
_set = {ind}
while cnt < k:
    ind = a_s[ind]
    if ind in _set:
        break
    _set.add(ind)
    ls.append(ind)
    cnt += 1
if cnt == k:
    print(ind+1)
else:
    start = ls.index(ind)
    end = cnt+1
    print(ls[start:][(k - cnt) % (end - start)-1]+1)