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,*A=map(int, open(0).read().split())
x=1
C=[None]*N
C[0]=0
c=0
while c<K:
    x=A[x-1]
    c+=1
    if C[x-1] is None:
        C[x-1]=c
    else:
        a=C[x-1]
        b=c-a
        K=(K-a)%b+a+b
        while c<K:
            x=A[x-1]
            c+=1
        break
print(x)

Solution (Python):
N, K = map(int, input().split(" "))
towns = list(map(lambda x: int(x)-1, input().split(" ")))
assert len(towns) == N
































L = None
visited = set([])
current = 0
while 1:
  if K == 0:
    break
  current = towns[current]
  K -= 1
  if current in visited:
    L = 0
    tcurrent = current
    while 1:
      tcurrent = towns[tcurrent]
      L += 1
      if tcurrent == current:
        break
    break
  visited.add(current)

if L is not None:
  K = K % L
  
  for _ in range(K):
    current = towns[current]
print(current + 1)

Solution (Python):
N, K = map(int, input().split(" "))
towns = list(map(lambda x: int(x)-1, input().split(" ")))
assert len(towns) == N



score = {0: 0}
current = 0
move = 0
while 1:
  next_town = towns[current]
  move += 1
  
  if score.get(next_town) is not None:
    
    loop_move = move - score.get(next_town)
    loop_town = next_town
    until_loop = score.get(next_town)
    
    
    
    break
  score[next_town] = move
  current = next_town

if K < until_loop:
  result = 0
  for k in range(K):
    result = towns[result]
else:
  result = loop_town
  for k in range((K - until_loop) % loop_move):
    result = towns[result]
print(result + 1)

Solution (Python):
N,K=map(int,input().split())
List = list(map(int, input().split()))
trace = [0]*N
pos = 1
period = 0
seppen = 0
res = 0
if K <= N:
  for i in range(K):
    if i == 0:
      pos = List[0]
    else:
      pos = List[pos-1]
  res = pos
      
else:
  for i in range(N):
    if i == 0:
      trace[i] = 1
      pos = List[0]
    else:
      if not trace[pos-1] == 0:
        period = i+1 - trace[pos-1]
        seppen = i
        break
      else:
        trace[pos-1] = i+1
        pos = List[pos-1]
  K_2 = K - seppen
  med = K_2 % period
  pos = List[trace.index(max(trace))]
  if med == 0:
    res = pos
  else:
    for i in range (med):
      pos = List[pos-1]
    res = pos

print(res)

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

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[n])
 
if k <= n:
    print(l[k])
else:
    x = (k - n + 1) % (n - p)
    if x == 0:
        print(l[n - 1])
    else:
        print(l[x + p - 1])

Solution (Python):
import sys

N, K = map(int, input().split())
A = list(map(int, sys.stdin.readline().rsplit()))
B = [-1] * N

if K <= N:
    now = 0
    for i in range(K):
        now = A[now] - 1
    print(now + 1)
    exit()

now = 0
for i in range(N):
    if B[now] == -1:
        B[now] = i
        now = A[now] - 1
    else:
        tmp = (K - B[now]) % (i - B[now])
        for i in range(tmp):
            now = A[now] - 1
        print(now + 1)
        exit()

Solution (Python):
N,K=map(int,input().split())
A=[int(a)-1 for a in input().split()]
city=0
if K<=N:
    for k in range(K):
        city=A[city]
    print(city+1)
else:
    visited=[-1]*N
    for k in range(K):
        if visited[city]!=-1:
            roop_start=visited[city]
            cycle_num=k-roop_start
            break
        visited[city]=k
        city=A[city]
    K=(K-roop_start)%cycle_num+roop_start
    city=0
    for k in range(K):
        city=A[city]
    print(city+1)

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

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

    now = 1
    l = []
    pre = [-1] * N
    turn = 0
    while True:
        if pre[now-1] != -1:
            break
        pre[now-1] = turn
        l.append(now)
        now = A[now-1]
        turn += 1

    if K < len(l):
        print(l[K])
    else:
        d = turn - pre[A[l[-1]-1]-1]
        f = len(l) - d
        t = (K - f) % d
        print(l[f+t])

if __name__ == "__main__":
    main()

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

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

now = 1
B = [0] * N
cycle = -1
bC = -1

if K < N:
    for _ in range(K):
        now = A[now - 1]
    print(now)
    exit()
else:
    for i in range(N+1):
        now = A[now - 1]
        if B[now - 1] == 0:
            B[now - 1] = i
        else:
            cycle = i - B[now - 1]
            bC = B[now - 1] + 1
            break
    last = (K - bC)%cycle
    for _ in range(last):
        now = A[now - 1]
    print(now)
    exit()

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

cycle=[]
for i in range(0,N):
    cycle.append(-1)
    A[i]-=1

count=0
cycle[0]=count
town=0
mod=0
while True:
    count+=1
    K-=1
    town=A[town]
    if K==0:
        mod=1
        break
    if cycle[town]==-1:
        cycle[town]=count
    else:
        mod=count-cycle[town]
        break

K%=mod
while K>0:
    town=A[town]
    K-=1

print(town+1)

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

move=[]
for i in range(0,N):
    move.append([])
    A[i]-=1
    move[i].append(A[i])


for j in range(1,64):
    for i in range(0,N):
        move[i].append(move[move[i][j-1]][j-1])
town=0
for i in range(0,64):
    if K%2==1:
        town=move[town][i]
    K//=2

print(town+1)

Solution (Python):
import math
def fact(n):
    ans = 1
    for i in range(2, n+1):
        ans*= i
    return ans
def comb(n, c):
    return fact(n)//(fact(n-c)*c)
n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt=  0
i = 1
cycle = ['1']
set = {'1'}
for c in range(n):
    i = a[i-1]
    if(i in set):
        break
    else:
        cycle.append(i)
        set.add(i)

ind = cycle.index(i)
k-= ind
rcycle = cycle[ind:]
if(k <= 0):
    print(cycle[k+ind])
else:
    now = (k+1)%len(rcycle)
    print(rcycle[now-1])

Solution (Python):
import math
def fact(n):
    ans = 1
    for i in range(2, n+1):
        ans*= i
    return ans
def comb(n, c):
    return fact(n)//(fact(n-c)*c)
'''
n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt=  0
i = 1
cycle = ['1']

for c in range(n):
    i = a[i-1]
    if(i in cycle):
        break
    else:
        cycle.append(i)

ind = cycle.index(i)
k-= ind
rcycle = cycle[ind:]
if(k <= 0):
    print(cycle[k+ind])
else:
    now = (k+1)%len(rcycle)
    print(rcycle[now-1])
'''
n,k = map(int,input().split())
a = list(map(int,input().split()))
already = ["1"]
set = {"1"}
now = 1
 
for i in range(n):
    now = str(a[int(now)-1])
    if now in set:
        break
    else:
        already.append(now)
        set.add(now)
 
 
ind = already.index(now)
k -= ind
cycle = already[ind:]
 
if k <= 0:
    print(already[k+ind])
else:
    now = (k+1) % len(cycle)
    print(cycle[now-1])

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
already = ["1"]
set = {"1"}
now = 1

for i in range(n):
    now = str(a[int(now)-1])
    if now in set:
        break
    else:
        already.append(now)
        set.add(now)


ind = already.index(now)
k -= ind
cycle = already[ind:]

if k <= 0:
    print(already[k+ind])
else:
    now = (k+1) % len(cycle)
    print(cycle[now-1])

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

V = [-1] * n
V[0] = 0

nv = A[0]
i = 1
while True:
    if V[nv] != -1:
        first_time = V[nv]
        cycle = i - first_time
        break
    V[nv] = i
    i += 1
    nv = A[nv]

if k <= first_time:
    for idx, v in enumerate(V):
        if k == v:
            ans = idx + 1
            print(ans)
else:
    k -= first_time
    for idx, v in enumerate(V):
        if first_time + (k % cycle) == v:
            ans = idx + 1
            print(ans)

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

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

    now=1
    flg = 0
    path = [1]
    pathcheck ={1}
    for i in range(min(k,n)):
        now = a[now-1]
        if now in pathcheck:
            flg = -1
            path.append(now)
            break
        path.append(now)
        pathcheck.add(now)


    if flg == 0:
        print(now)
        exit()

    x1 = path.index(now)
    x2 = len(path)-1
    x3 = (k-x1)%(x2-x1)
    print(path[x1+x3])

if __name__ == '__main__':
    main()

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

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

    now=1
    flg = 0
    path = [1]
    pathcheck ={1}
    for i in range(min(k,n)):
        now = a[now-1]
        if now in pathcheck:
            flg = -1
            path.append(now)
            break
        path.append(now)
        pathcheck.add(now)


    if flg == 0:
        print(now)
        exit()

    x1 = path.index(now)
    x2 = len(path)-1
    x3 = (k-x1)%(x2-x1)
    print(path[x1+x3])

if __name__ == '__main__':
    main()

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

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

    now=1
    flg = 0
    path = [1]
    pathcheck ={1}
    while True:
        now = a[now-1]
        flg = flg + 1
        if now in pathcheck:
            flg = -1
            path.append(now)
            break
        path.append(now)
        pathcheck.add(now)
        if flg == k:
            break


    if flg == k:
        print(now)
        exit()

    x1 = path.index(now)
    x2 = len(path)-1
    x3 = (k-x1)%(x2-x1)
    print(path[x1+x3])

if __name__ == '__main__':
    main()

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

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

did = set([1])
route = [1]

now = 1
for i in range(k):
    now = d[now]
    route.append(now)
    if now in did:
        break
    did.add(now)

if k < 10 ** 6:
    now = 1
    for i in range(k):
        now = d[now]

    print(now)

else:
    for i in range(len(route)):
        if route[i] == route[-1]:
            roop = route[i:-1]
            print(roop[(k - i) % len(roop)])
            exit()

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

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

    now=1
    flg = 0
    path = [1]
    pathcheck ={1}
    for i in range(k):
        now = a[now-1]
        if now in pathcheck:
            flg = -1
            path.append(now)
            break
        path.append(now)
        pathcheck.add(now)


    if flg == 0:
        print(now)
        exit()

    x1 = path.index(now)
    x2 = len(path)-1
    x3 = (k-x1)%(x2-x1)
    print(path[x1+x3])

if __name__ == '__main__':
    main()

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

now=1
flg = 0
path = [1]
pathcheck ={1}
for i in range(k):
    now = a[now-1]
    if now in pathcheck:
        flg = -1
        path.append(now)
        break
    path.append(now)
    pathcheck.add(now)


if flg == 0:
    print(now)
    exit()

x1 = path.index(now)
x2 = len(path)-1
x3 = (k-x1)%(x2-x1)
print(path[x1+x3])

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

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

did = set()
route = [1]

now = 1
for i in range(k):
    now = d[now]
    route.append(now)
    if now in did:
        break
    did.add(now)

if k < 10 ** 6:
    now = 1
    for i in range(k):
        now = d[now]

    print(now)

else:
    for i in range(len(route)):
        if route[i] == route[-1]:
            roop = route[i:-1]
            print(roop[(k - i) % len(roop)])
            exit()

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

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

did = set()
route = [1]

now = 1
for i in range(k):
    now = d[now]
    route.append(now)
    if now in did:
        break
    did.add(now)

if k < 10 ** 6:
    now = 1
    for i in range(k):
        now = d[now]

    print(now)

else:
    for i in range(len(route)):
        if route[i] == route[-1]:
            roop = route[i:-1]
            print(roop[(k - i) % len(roop)])
            exit()

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

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

did = set()
route = [1]

now = 1
for i in range(k):
    now = d[now]
    route.append(now)
    if now in did:
        break
    did.add(now)

if k < 10 ** 6:
    now = 1
    for i in range(k):
        now = d[now]

    print(now)

else:
    for i in range(len(route)):
        if route[i] == route[-1]:
            roop = route[i:-1]
            break

    print(roop[(k - i) % len(roop)])

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):
import sys
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue
from decimal import Decimal



sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)


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


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

    N, K = il()
    tel = [0] + il()

    
    position = 1

    
    if K <= N:
        for k in range(K):
            position = tel[position]
        print(position)
        exit()

    
    for n in range(N):
        position = tel[position]

    
    
    K -= N
    
    base = position
    cnt = 1
    while base != tel[position]:
        position = tel[position]
        cnt += 1

    
    
    for k in range(K % cnt + 1):
        position = tel[position]
    print(position)


if __name__ == '__main__':
    main()

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

if __name__ == "__main__":
    main()

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

before = 1
start = 0
end = 0

if K <= N:
    for i in range(K):
        before = a[before]
    print(before)


    
else:
    market_town = 0
    for i in range(N):
        before = a[before]
    marked_town = before
    K -= N
    counter = 1
    while marked_town != a[before]:
        before = a[before]
        counter += 1
    K = K % counter
    for i in range(K+1):
        before = a[before]
    print(before)

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

def compute(city, K):
    for i in range(K):
        
        city = As[city-1]
    print(city)

cities_visited = [False] * N
i = 0
city = 1
city_visited_time = {}





while not cities_visited[city - 1]:
    cities_visited[city - 1] = True
    city_visited_time[city-1] = i
    
    city = As[city-1]
    i += 1
    
    
loop_start = city_visited_time[city-1]

loop_end = i


div = (K - loop_start) // (loop_end - loop_start)

if K - loop_start < 0 or div == 0:
    compute(1, K)
else:
    compute(city, K - loop_start - div * (loop_end - loop_start))

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

b=[1]
Flag=True
now=1
arrived=set(b)
memo=1
while Flag:
    b.append(a[now-1])
    if a[now-1] in arrived:
        Flag=False
        memo=a[now-1]
    arrived.add(a[now-1])
    now=a[now-1]

loop=len(b)-b.index(memo)-1

first=b.index(memo)


if k>first:
    p,q=divmod(k-first,loop)
    print(b[first+q])
else:
    print(b[k])

Solution (Python):

import math

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


a.insert(0,0)


db = [a]
logk = math.ceil(math.log(k, 2)) 


for i in range(1, logk+1):
  d = [0] * (n+1)
  prev = db[i-1]
  for j in range(1, n+1):
    d[j] = prev[prev[j]]
  db.append(d)




v = 1
for i in range(logk, -1, -1):
  bit = 1 << i
  if k & bit:
    v = db[i][v]
print(v)

Solution (Python):
import glob
import os

bn = os.path.basename(__file__).split('.')[0]
dn = os.path.dirname(__file__).split('/')


REL_PATH = dn[len(dn)-2] + '\\' + dn[len(dn)-1] + '\\' + bn


TOP_PATH = 'C:\\AtCoder'

class Common:

    problem = []
    index = 0

    def __init__(self, rel_path):
        self.rel_path = rel_path

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index-1]

        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve()


class Solver(Common):

    alph = []

    def rec(self, result, now, max, N):

        if now == N:
            res = ""
            for i in result:
                res += self.alph[i]
            print(res)
            return

        if now == 0:
            result[0] = 0
            self.rec(result, now+1, 0, N)
            return

        for i in range(0, max+2):
            if i == N:
                return
            result[now] = i
            if i > max:
                self.rec(result, now+1, i, N)
            else:
                self.rec(result, now+1, max, N)

    def resolve(self):

        N, K = map(int, self.input_data().split())
        A = list(map(int, self.input_data().split()))
        A.insert(0, 0)
        town_visit_time = [-1]*(N+1)

        num_loop = 0
        loop_start_count = 0
        loop_start_town = 0

        next_town = 1
        for i in range(0,N+1):
            if town_visit_time[next_town] != -1:
                num_loop = i - town_visit_time[next_town]
                loop_start_count = town_visit_time[next_town]
                loop_start_town = next_town
                break

            town_visit_time[next_town] = i
            next_town = A[next_town]

            if i+1 == K:
                print(str(next_town))
                exit(0)

        remain = (K - loop_start_count) % num_loop

        result =  loop_start_town
        for i in range(remain):
            result = A[result]

        print(str(result))

solver = Solver(REL_PATH)
solver.exec_resolve()

Solution (Python):
from collections import deque

N,K=map(int,input().split())
A=list(map(int,input().split()))
if A[0]==1:
    print(1)
    exit()
if K==1:
    print(A[0])
    exit()
for i in range(N):
    A[i]-=1



S = [0]*N
L = deque()
S[0]=1
L.append(1)
count=1
pos=A[0]

for i in range(N+1):
    
    if S[pos]==0:
        count+=1
        S[pos]=count
        L.append(pos+1)
        pos=A[pos]
        
    else:
        roopcount=count-S[pos]+1
        L.append(pos+1)
        break


if count>K:
    ans=L[K]

else:
    K=K-(count-roopcount)
    K=K%roopcount
    for i in range(count-roopcount):
        ret=L.popleft()
    ans=L[K]
    
print(ans)

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

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

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

if k < l:
  print(d[k])
else:
  k -= l
  k %= c
  print(d[l+k])

Solution (Python):
import sys
def f(A,N):
    INF=10**18+1
    C=[0 for _ in [0]*N]
    C[0],cp=1,0
    for i in range(2,N+2):
        if C[A[cp]]>0:return (A[cp],C[A[cp]]-1,i-C[A[cp]])
        else:C[A[cp]],cp=i,A[cp]
    return (-1,0,INF)
def main():
    N,K=tuple(map(int,sys.stdin.readline().split()))
    A=list(map(lambda x:int(x)-1,sys.stdin.readline().split()))
    a,n,m=f(A,N)
    (cp,X)=(0,K) if a==-1 or K<=n else (a,(K-n)%m)
    for _ in range(X):cp=A[cp]
    print(cp+1)
if __name__=='__main__':main()

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

A = [1] + [int(n) for n in input().split()]

dest = []

current = 0
count = 0
next_dest = A[current]
seen = {}

while not next_dest in seen:
    current = next_dest
    next_dest = A[current]
    dest.append(current)
    seen[current] = count
    count += 1

cycle_start = seen[next_dest]
cycle_length = seen[current] - cycle_start + 1

if K <= cycle_start:
    print(dest[K])
else:
    print(dest[cycle_start + ((K - cycle_start) % cycle_length)])

Solution (Python):
N, K = [int(i) for i in input().split(' ')]
A = [0]
A.extend([int(a) for a in input().split(' ')])
check_list = [0] * (N + 1)

town = 1
check_point = 0
initial = -1
interval = -1

for k in range(K):
    if check_list[town] > 0:
        check_point = town
        initial = check_list[town]
        interval = k - initial
        break
    check_list[town] = k
    town = A[town]

if interval == -1:
    print(town)
    exit(0)

remain = K - k

rem = remain % interval

check_list_ = list(map(lambda x: x - initial, check_list))

for i, c in enumerate(check_list_):
    if c == rem:
        print(i)
        exit(0)

Solution (Python):
from collections import Counter

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

counter = Counter()

i = 1
while True:
    if counter[i] >= 2:
        break
    counter[i] += 1
    i = A[i - 1]

loop = len([0 for v in counter.values() if v == 2])
to_loop = len([0 for v in counter.values() if v == 1])

if K > to_loop:
    K = to_loop + (K - to_loop) % loop

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

print(i)

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

slow = 1
fast = 1
counter = 0
for _ in range(K):
    slow = A[slow - 1]
    fast = A[A[fast-1]-1]
    counter += 1
    if slow == fast:
        break




slow2 = slow
slow_start = 1
counter = 0
for _ in range(K):
    slow2 = A[slow2 - 1]
    slow_start = A[slow_start-1]
    counter += 1
    if slow2 == slow_start:
        break
lam = counter
slow3 = slow
counter = 0
for _ in range(K):
    slow3 = A[slow3 - 1]
    counter += 1
    if slow3 == slow:
        break
myu = counter

total = lam + (K-lam) % myu
length = 0
if total < K:
    length = total
else:
    length = K
current = 1
for _ in range(length):
    current = A[current-1]
print(current)

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
B = [True]*N
C = [1]
now = 0
while B[now]:
    B[now] = False
    now = A[now] - 1
    C.append(now+1)
start = C.index(C[-1])
D = C[start:-1]
L = len(D)
if K < start:
    print(C[K])
else:
    K -= start
    K %= L
    print(D[K])

Solution (Python):
n,k=map(int,input().split())
teleport=list(map(int,input().split()))
double_counter=set()

mojiretu=["1"]
index=1
double_counter.add(index)
flag=False
for i in range(k):
    tmp=teleport[index-1]
    if tmp in double_counter:
        flag=True
        break
    else:
        double_counter.add(tmp)
        
        mojiretu.append(str(tmp))
        index=tmp
else:
    print(tmp)

if flag:
    
    
    
    

    index = mojiretu.index(str(tmp))
    roop_str=mojiretu[index:]
    length=k%len(roop_str)-len(mojiretu[:index])%len(roop_str)
    print(roop_str[length])

Solution (Python):
n,k=map(int,input().split())
teleport=list(map(int,input().split()))
double_counter=set()

mojiretu=["1"]
index=1
double_counter.add(index)
flag=False
for i in range(k):
    tmp=teleport[index-1]
    if tmp in double_counter:
        flag=True
        break
    else:
        double_counter.add(tmp)
        
        mojiretu.append(str(tmp))
        index=tmp
else:
    print(tmp)

if flag:
    
    
    
    

    index = mojiretu.index(str(tmp))
    roop_str=mojiretu[index:]
    length=k%len(roop_str)-len(mojiretu[:index])%len(roop_str)
    print(roop_str[length])

Solution (Python):
n,k=map(int,input().split())
teleport=list(map(int,input().split()))
double_counter=set()

mojiretu=["1"]
index=1
double_counter.add(index)
flag=False
for i in range(k):
    tmp=teleport[index-1]
    if tmp in double_counter:
        flag=True
        break
    else:
        double_counter.add(tmp)
        
        mojiretu.append(str(tmp))
        index=tmp
else:
    print(tmp)

if flag:
    
    
    
    

    Li = mojiretu.index(str(tmp))
    Ri = len(mojiretu)
    idx = Li + (k - Li) % (Ri - Li)
    print(mojiretu[idx])

Solution (Python):
[n, k], [*a] = [[*map(int, t.split())] for t in open(0)]
count = 0
visited = dict()
tmp = 1
while count < k:
    visited[tmp] = count
    count += 1
    tmp = a[tmp - 1]
    if tmp in visited:
        k = (k - count) % (count - visited[tmp])
        visited = {tmp: 0}
        count = 0
print(tmp)

Solution (Python):
[n, k], [*a] = [[*map(int, t.split())] for t in open(0)]
count = 0
visited = dict()
tmp = 1
while count < k:
    visited[tmp] = count
    count += 1
    tmp = a[tmp - 1]
    if tmp in visited:
        break
if count < k:
    k = (k - count) % (count - visited[tmp])
    for _ in range(k):
        tmp = a[tmp - 1]
print(tmp)

Solution (Python):
n,k=map(int,input().split(" "))
a=[int(i)-1 for i in input().split(" ")]
was=[False]*n
pos=0
while k>0 and not was[pos]:
    was[pos]=True
    pos=a[pos]
    k-=1
if k>0:
    was=[False]*n
    cyc=0
    while not was[pos]:
        was[pos]=True
        pos=a[pos]
        cyc+=1
    k%=cyc
while k>0:
    pos=a[pos]
    k-=1
print(pos+1)

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

if __name__ == "__main__":
    main()

Solution (Python):

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

connection ={i:0 for i in range(1,N+1)}
for i in range(1,N+1):
    connection[i] =A[i-1]

passed =[1]
passed_set =set(passed)
intersection =0
next =connection[1]


for i in range(1,N+1): 
    if next not in passed_set:
        passed.append(next)
        passed_set.add(next)
        next =connection[next]
    else:
        intersection =next
        break

loop_s  =passed.index(intersection)
loop_f  =len(passed) 
loop_num =loop_f -loop_s


ans =0
if K < loop_f:
    ans =passed[K]
else:
    K2      =K -loop_s
    q,mod   =divmod(K2,loop_num)
    ans     =passed[loop_s +mod]
print(ans)

Solution (Python):
def main():
    n, k = map(int, input().split())
    a = [int(v) - 1 for v in input().split()]
    path_to_idx = {}
    path = []
    current_node = 0
    while True:
        path.append(current_node)
        path_to_idx[current_node] = len(path_to_idx)
        if path_to_idx.get(a[current_node]) is not None:
            cyclic = path_to_idx[a[current_node]]
            break
        current_node = a[current_node]

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


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    if a[0] == 1:
        print(1)
        exit(0)
    if a[0] == a[a[0]-1]:
        print(a[0])
        exit(0)
    pos = a[0]
    num = 1
    nums = set([1])
    while pos not in nums:
        nums.add(pos)
        num += 1
        pos = a[pos-1]
    lastnum = pos
    prefix = 0
    pos = 1
    while pos != lastnum:
        prefix += 1
        pos = a[pos-1]
    num -= prefix
    if k > prefix:
        k = (k-prefix)%num+prefix
    pos = 1
    for _ in range(k):
       pos = a[pos-1]
    print(pos)

if __name__ == "__main__":
    main()

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
result=[1]
temp=1
for i in range(n):
  temp=a[temp-1]
  result.append(temp)
temp=result.index(result[len(result)-1])
c=len(result)-temp-1
if k<n:
  print(result[k])
else:
  print(result[temp+(k-temp)%c])

Solution (Python):
N,K=map(int,input().split())
A=[int(a)-1 for a in input().split()]
city=0
if K<=N:
    for k in range(K):
        city=A[city]
    print(city+1)
else:
    visited=[-1]*N
    for k in range(K):
        if visited[city]!=-1:
            roop_start=visited[city]
            cycle_num=k-roop_start
            break
        visited[city]=k
        city=A[city]
    K=(K-roop_start)%cycle_num+roop_start
    city=0
    for k in range(K):
        city=A[city]
    print(city+1)

Solution (Python):
N,K=map(int,input().split())
A=[0]+list(map(int,input().split()))
city=1
if K<=N:
    for k in range(K):
        city=A[city]
    print(city)
else:
    visited=[-1]*(N+1)
    for k in range(K):
        if visited[city]!=-1:
            roop_start=visited[city]
            cycle_num=k-roop_start
            break
        visited[city]=k
        city=A[city]
    K=(K-roop_start)%cycle_num+roop_start
    city=1
    for k in range(K):
        city=A[city]
    print(city)