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()))
B = []
j = 1
for i in range(n):
    B.append(j)
    j = A[j-1]
    
visited = [-1] * (n+1)
cs = -1
i = 1
count = 0
while True:
    visited[i] = count
    count += 1
    i = A[i-1]
    if visited[i] != -1:
        cs = i
        break

if k < n:
    print(B[k])
    exit()
cl = count - visited[cs]
a =(k-visited[cs]) % cl
current = cs-1
for i in range(a):
    current = A[current] -1
print(current+1)

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))
l=[1]
X=[True]*N
X[0]=False
num=1
for i in range(K):
    num=A[num-1]
    if X[num-1]:
        l.append(num)
        X[num-1]=False
    else:
        q=l.index(num)
        l=l[q:]
        print(l[(K-q)%len(l)])
        exit()
print(l[-1])

Solution (Python):


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    Acnt = [0] * N
    Acnt[0] = 1
    telepo = [1]
    i = 0
    while True:
        Acnt[A[i] - 1] += 1
        if Acnt[A[i] - 1] > 1:
            break
        telepo.append(A[i])
        i = A[i] - 1
    roop_s = telepo.index(A[i])
    roop = len(telepo) - roop_s

    ans = 0
    if K < roop_s:
        ans = K
    else:
        ans = K - roop_s
        ans %= roop
        ans += roop_s

    print(telepo[ans])


if __name__ == "__main__":
    main()

Solution (Python):
n,k=map(int,input().split())
a=[0]+list(map(int,input().split()))
c=[0]*(n+1)
c[1]=1
q=[0,1]
while True:
  i=a[q[-1]]
  if c[i]:break
  c[i]=c[q[-1]]+1
  q+=[i]
if c[i]<k:
  k-=c[i]-1
  t=c[q[-1]]-c[i]+1
  print(q[c[i]+k%t])
else:print(q[k+1])

Solution (Python):
n,k = map(int, input().split())
b = list(map(int, input().split()))
a=0
for i in range(n):
    b[i] -= 1
if k<n:
    for i in range(k):
        a = b[a]
    print(a+1)
    exit()
c = [-1]*n
cc = 0
se = set()
while a not in se:
    c[a] = cc
    cc += 1
    se.add(a)
    a = b[a]
for i in range((k-c[a])%(cc-c[a])):
    a = b[a]
print(a+1)

Solution (Python):


n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
count_arr = [0] * n
count = 0

now = 0
loop_length = -1
while True:
    if count == k:
        print(now + 1)
        exit()
    if count_arr[now] != 0:
        loop_length = count - count_arr[now] + 1
        break
    count += 1
    count_arr[now] = count
    now = a[now]


rest = (k - count) % loop_length


if rest == 0:
    print(now + 1)
    exit()

while rest > 0:
    rest -= 1
    if rest == 0:
        break
    now = a[now]
print(a[now] + 1)

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,k=I()
a = l()
town = []
num = [0]*(n+1)
cnt = 1
num[0]=1

for i in range(n):
    num[a[cnt-1]] += 1
    if num[a[cnt-1]] == 2:
        ans = a[cnt-1]
        p = town.index(ans)
        b = town[p:]
        x = len(town)
        y = len(b)
        q = k-x+y
        if y == 0:
            print(town[n-1])
            sys.exit()
        if q < 0:
            print(town[k-1])
            sys.exit()
        z = q%y
        print(b[z-1])
        sys.exit()
        break
    town.append(a[cnt-1])
    cnt = a[cnt-1]

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,k=I()
a = l()
town = []
num = [0]*(n+1)
cnt = 1
num[0]=1

for i in range(n):
    num[a[cnt-1]] += 1
    if num[a[cnt-1]] == 2:
        ans = a[cnt-1]
        p = town.index(ans)
        b = town[p:]
        x = len(town)
        y = len(b)
        q = k-x+y
        if y == 0:
            print(town[n-1])
            sys.exit()
        if q < 0:
            print(town[k-1])
            sys.exit()
        z = q%y
        if z == 0:
            print(town[z-1])
        else:
            print(b[z-1])
        sys.exit()
        break
    town.append(a[cnt-1])
    cnt = a[cnt-1]

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque

n,k = [int(x) for x in stdin.readline().rstrip().split()]
a = [0] + [int(x) for x in stdin.readline().rstrip().split()]

m = 0
b = [1]
visited = [-1 for i in range(n+1)]

c = 1
visited[1] = 1
for i in range(n):
    c = a[c]
    if visited[c] != 1:
        b.append(c)
        visited[c] = 1
        if i == k-1:
            print(c)
            sys.exit()
    else:
        break

idx = b.index(c)
bn = len(b[idx:])
sn = len(b[:idx])


kk = (k-sn) % bn
print(b[idx+kk])

Solution (Python):
D=60
f=lambda:[*map(int,input().split())]
n,k=f()
l=f()
t=[[0]*n for _ in range(D)]
for i in range(n):
  t[0][i]=l[i]-1
for i in range(D-1):
  for j in range(n):
    t[i+1][j]=t[i][t[i][j]]
a=0
for i in range(D):
  if k%2: a=t[i][a]
  k//=2
print(a+1)

Solution (Python):

import sys
sys.setrecursionlimit(10**6)
N,K=map(int, sys.stdin.readline().split())
A=[None]+map(int, sys.stdin.readline().split())
visit=[ 0 for _ in range(N+1) ]

L=[]    

def func(fro):
    global K
    to=A[fro]
    visit[fro]+=1
    if visit[fro]==2:
        L.append(fro)
    if visit[to]<=1:    
        K-=1        
        if K==0:    
            print to
            quit()
        func(to)

func(1)
K-=1
l=len(L)
print L[K%l]

Solution (Python):

def solve():
    n,k=map(int,input().split())
    a=[-1]+list(map(int,input().split()))

    stp=[-1]*(n+1)

    vis=1
    stp[1]=0
    while k >= 0 :
        if k==0 :
            return vis
        nxt=a[vis]
        if stp[nxt] == -1 :
            stp[nxt]=stp[vis]+1
            vis=nxt
            k-=1
        else:
            loop=stp[vis]+1-stp[nxt]
            k%=loop
            for i in range(0,k) :
                vis=a[vis]
            return vis

print(solve())

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

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

visit={}
first={}
for n in range(N):
    visit[n+1]=0
    first[n+1]=-1
    
v=1
Loop=[]
for n in range(2*N):   
    Loop.append(v)
    if visit[v] == 0:
        first[v] = n
    visit[v]+=1

    if visit[v] == 2:
        break
        
    v=A_[v]

if K-first[v] < 0:
    print(Loop[K])
else:  
    ans_idx=(K-first[v])%(n-first[v])
    print(Loop[first[v]:n][ans_idx])

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

d = 60
dp = [[-1]*N for _ in range(d+1)]
for i in range(N):
    dp[0][i] = a[i]-1

for i in range(d):
    for j in range(N):
        dp[i+1][j] = dp[i][dp[i][j]]

ans = 0

for i in range(d):
    if K & (1<<i):
        ans = dp[i][ans]

print(ans+1)

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

s = []
o = [-1] * (n + 1)
count = 0

v = 1
while o[v] == -1:
    o[v] = count
    s.append(v)
    v = a[v-1]
    count += 1

l = o[v]
c = len(s) - o[v]
    
if k < l:
    print(s[k])
else:
    k -= l
    k = k % c
    print(s[l+k])

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

b = 0
r = k + 1
seen = [-1] * n
seen[0] = 0
p = 1
for i in range(k):
    p = a[p - 1]
    if seen[p - 1] != -1:
        b = seen[p - 1]
        r = i + 1 - b
        for j in range((k - b) % r):
            p = a[p - 1]
        print(p)
        exit()
    seen[p - 1] = i + 1
else:
    print(p)

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

def tortoise_and_hare(s,f):
    tortoise = f[s]
    hare = f[f[s]]

    while tortoise != hare:
        tortoise = f[tortoise]
        hare = f[f[hare]]

    rho = 0
    tortoise = s
    while tortoise != hare:
        tortoise = f[tortoise]
        hare = f[hare]
        rho += 1

    lamb = 1
    hare = f[hare]

    while tortoise != hare:
        hare = f[hare]
        lamb += 1

    return rho,lamb

rho, lamb = tortoise_and_hare(0,f)

if k <= rho:
    s = 0
    for i in range(k):
        s = f[s]
else:
    s = 0
    for i in range(rho):
        s = f[s]
    k = (k-rho)%lamb
    for i in range(k):
        s = f[s]

print(s+1)

Solution (Python):


import sys
from collections import defaultdict
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

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


seen = defaultdict(list)
count = 0
pos = 0
seen[pos].append(count)

while True:
    pos = A[pos]
    count += 1
    seen[pos].append(count)
    if len(seen[pos]) == 2:
        head = seen[pos][0]
        loop = count - head
        break


if K < head:
    pos = 0
    for i in range(K):
        pos = A[pos]
    print(pos+1)


else:
    K = K - head
    res = K % loop
    for i in range(res):
        pos = A[pos]

    print(pos+1)

Solution (Python):
from collections import Counter
N, K = map(int,input().split())
tele = list(map(int, input().split()))


now = 0
goal,loop,visit = [],[],[0]*N

while visit[now]!=2:

  if visit[now]==0:
    goal.append(now)
  else:
    loop.append(now)
    
  visit[now] += 1
  now = tele[now]-1

if len(goal)>K:
  print(goal[K]+1)
else:
  amari = len(goal)-len(loop)
  print(loop[(K-amari)%len(loop)]+1)

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

i = 0
dist[0] = 0
while True:
    nxt = a[i] - 1
    if dist[nxt] >= 0:
        if k > dist[nxt]:
            k -= dist[nxt]
            k %= dist[i] - dist[nxt] + 1
            k += dist[nxt]
        print(dist.index(k) + 1)
        break
    dist[nxt] = dist[i] + 1
    i = nxt

Solution (Python):
def f():
    n,k=map(int,input().split())
    l=list(map(int,input().split()))
    now=1
    for d in range(k.bit_length()):
        k,f=divmod(k,2)
        if f:now=l[now-1]
        l=tuple(l[i-1]for i in l)
    print(now)
if __name__ == "__main__":
    f()

Solution (Python):
def f():
    n,k=map(int,input().split())
    l=list(map(int,input().split()))
    now=1
    for d in range(k.bit_length()):
        if k%2:now=l[now-1]
        l=tuple(l[i-1]for i in l)
        k//=2
    print(now)
if __name__ == "__main__":
    f()

Solution (Python):
def f():
    n,k=map(int,input().split())
    l=list(map(int,input().split()))
    now=1

    for i in range(k.bit_length()):
        if k%2:now=l[now-1]
        l=tuple(l[l[i]-1]for i in range(n))
        k//=2
    print(now)
if __name__ == "__main__":
    f()

Solution (Python):
def f():
    n,k=map(int,input().split())
    l=list(map(int,input().split()))
    now=1

    for i in range(k.bit_length()):
        if k%2:now=l[now-1]
        l=[l[l[i]-1]for i in range(n)]
        k//=2
    print(now)
if __name__ == "__main__":
    f()

Solution (Python):
def f():
    n,k=map(int,input().split())
    l=list(map(int,input().split()))
    now=1

    for i in range(k.bit_length()):
        if k and k%2:now=l[now-1]
        l=tuple(l[l[i]-1]for i in range(n))
        k//=2
    print(now)
if __name__ == "__main__":
    f()

Solution (Python):
n,k=map(int,input().split())
l=list(map(int,input().split()))
now=1
while k:
    k,f=divmod(k,2)
    if f:now=l[now-1]
    l=[l[l[i]-1]for i in range(n)]
print(now)

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

c = [0] * (n + 1)

q = [1]

while True:
  p = m[p]
  if (c[p] == 1):
    break
  c[p] += 1
  q.append(p)
i = q.index(p)
q1 = q[:i]
q2 = q[i:]
if len(q1) > 0 and k < len(q1):
  print(q1[k])
else: 
  print(q2[(k - len(q)) % len(q2)])

Solution (Python):

import bisect
import heapq
import itertools
import math
import numpy as np
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from math import gcd
from operator import add, itemgetter, mul, xor
def cmb(n,r,mod):
  bunshi=1
  bunbo=1
  for i in range(r):
    bunbo = bunbo*(i+1)%mod
    bunshi = bunshi*(n-i)%mod
  return (bunshi*pow(bunbo,mod-2,mod))%mod
mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]





def now_place(li,n):
    now = 1
    for i in range(n):
        now = li[now-1]
    return now



def root(li,p):
    ans = []
    now = 1
    for j in range(p):
        ans.append(now_place(li,j+1))
    return ans



n,k = MI()
a = LI()

town = []

visit=[0]*n
p = 1
while visit[p-1] == 0:
    town.append(p)
    visit[p-1] = 1
    p = a[p-1]


l = town.index(p)
if k <len(town):
    print(town[k])
else:
    print(town[l + (k-l)%(len(town[l:]))])

Solution (Python):

import bisect
import heapq
import itertools
import math
import numpy as np
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from math import gcd
from operator import add, itemgetter, mul, xor
def cmb(n,r,mod):
  bunshi=1
  bunbo=1
  for i in range(r):
    bunbo = bunbo*(i+1)%mod
    bunshi = bunshi*(n-i)%mod
  return (bunshi*pow(bunbo,mod-2,mod))%mod
mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]





def now_place(li,n):
    now = 1
    for i in range(n):
        now = li[now-1]
    return now



def root(li,p):
    ans = []
    now = 1
    for j in range(p):
        ans.append(now_place(li,j+1))
    return ans



n,k = MI()
a = LI()

town = []

visit=[0]*n
p = 1
while visit[p-1] == 0:
    town.append(p)
    visit[p-1] = 1
    p = a[p-1]
l = town.index(p)
if k <len(town):
    print(town[k])
else:
    print(town[l + (k-l)%(len(town[l:]))])

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline

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

    
    
    
    
    
    
    path = list()
    order = [-1 for _ in range(n + 1)]  
    place = 1

    
    
    while order[place] == -1:
        order[place] = len(path)  
        path.append(place)  
        place = a[place - 1]  

    
    cycle = len(path) - order[place]
    before_cycle_count = order[place]

    if (k < before_cycle_count):
        print(path[k])
    else:
        k -= before_cycle_count
        k %= cycle  
        print(path[before_cycle_count + k])


if __name__ == '__main__':
    main()

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline

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

    
    
    path = list()
    order = [-1 for _ in range(n + 1)]
    place = 1

    while order[place] == -1:
        order[place] = len(path)
        path.append(place)
        place = a[place - 1]

    cycle = len(path) - order[place]
    before_cycle_count = order[place]

    if (k < before_cycle_count):
        print(path[k])
    else:
        k -= before_cycle_count
        k %= cycle
        print(path[before_cycle_count + k])


if __name__ == '__main__':
    main()

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline

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

    
    
    path = list()
    order = [-1 for _ in range(n + 1)]
    place = 1

    while order[place] == -1:
        order[place] = len(path)
        path.append(place)
        place = a[place - 1]

    cycle = len(path) - order[place]
    before_cycle_count = order[place]

    if (k < before_cycle_count):
        print(path[k])
    else:
        k -= before_cycle_count
        k %= cycle
        print(path[before_cycle_count + k])


if __name__ == '__main__':
    main()

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

dis=[-1]*(n+1)
now=1
time=0
while 1 :
    if dis[now]>=0 : break
    dis[now]=time
    time+=1
    now=alst[now]

if time>k :
    print(dis.index(k))
else :
    head=dis[now]
    roop_unit=time-dis[now]
    work=(k-head)%roop_unit
    print(dis.index(head+work))

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

now = A[0]
root_set = set([now])
root_array = [now]
if k<= n:
    for i in range(k-1):
        now = A[now-1]
        root_set.add(now)
        root_array.append(now)
    print(root_array[-1])
else:
    for i in range(n-1):
        now = A[now-1]
        if not now in root_set:
            root_set.add(now)
            root_array.append(now)
        else:
            index = root_array.index(now)
            f = root_array[:index]
            loop = root_array[index:]
            break
    len_f = len(f)
    len_loop = len(loop)
    print(loop[(k-len_f)%len_loop-1])

Solution (Python):

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

dp = [[0] * (N + 1) for _ in range(100)]
dp[0] = [0] + X
for k in range(1, 100):
    for i in range(N + 1):
        dp[k][i] = dp[k - 1][dp[k - 1][i]]

ans = 1
for k in range(100):
    if K >> k & 1:
        ans = dp[k][ans]

print(ans)

Solution (Python):


def main():
    N, K = map(int, input().split())
    As = list(map(int, input().split()))
    As = [0]+As
    ikisaki = As[1]
    aru = {1}
    itta = [1]
    imaitta = 1
  
    while not ikisaki in aru:
        imaitta = ikisaki
        ikisaki = As[ikisaki]
        itta.append(imaitta)
        aru.add(imaitta)
    ind = itta.index(ikisaki)
    kurikaesi = itta[ind:]
    itidokiri = itta[:ind]
    ans = itidokiri[K] if len(itidokiri) > K else kurikaesi[(K-len(itidokiri))%len(kurikaesi)]


    return ans

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

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

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

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

point = 1

dev = [1]
flag = 0

for i in range(1, k + 1):
    if cheak[point]:
        loop = i -cheak[point]
        pre_loop = cheak[point]
        flag = 1
        break
    else:
        cheak[point] = i
        point = a[point]
        

if flag:
    rest = (k + 1- pre_loop) % loop
    for j in range(rest):
        point = a[point]
        

print(point)

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

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

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

point = 1

dev = [1]
flag = 0

for i in range(1, k + 1):
    if cheak[point]:
        loop = i -cheak[point]
        pre_loop = cheak[point]
        flag = 1
        break
    else:
        cheak[point] = i
        point = a[point]
        dev.append(point)

if flag:
    rest = (k + 1- pre_loop) % loop
    for j in range(rest):
        point = a[point]
        dev.append(point)

print(point)

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

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

ans=0
hub_city=0
visited=[-1]*n
now=0
cnt_warp=0
loop_cnt=0
first_visit_hub_city=0

for i in range(n):
    if visited[now]>-1:
        hub_city=now
        first_visit_hub_city=visited[now]
        loop_cnt=cnt_warp-first_visit_hub_city
        break
    else:
        visited[now]=cnt_warp
    
    now=A[now]
    cnt_warp+=1

idoukaizu=min(first_visit_hub_city+(k-first_visit_hub_city)%loop_cnt,k)

now=0
for i in range(idoukaizu):
    now=A[now]

print(now+1)

Solution (Python):
N, K = map(int, input().split())
A = list(map(lambda x: x - 1, map(int, input().split())))
current = 0
memo = {
    0: 0
}
for i in range(1, K + 1):
    current = A[current]
    if current in memo:
        
        length = i - memo[current]
        tmp = (K - memo[current]) % length
        for j in range(tmp):
            current = A[current]
        break
    else:
        memo[current] = i
print(current + 1)

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

if k == 1:exit(print(A[0]))
t = 0
dp = [-1] * n
dp[0] = 0
flg = 0
for i in range(1,k+1):
  t = A[t]-1
  if dp[t] != -1:
    flg = 1
    break
  dp[t] = i
if flg == 0:
  exit(print(t+1))
elif k == 2:
  exit(print(t+1))

l = i - dp[t]
K = (k-dp[t])%l + dp[t]

for i in range(n):
  if dp[i] == K:exit(print(i+1))

Solution (Python):
from collections import Counter

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

city=[0]*n
city[0]+=1
dict={}
for x in range(n-1):
  b=city[x]-1
  c=a[b]
  city[x+1]+=c
  
for y in range(n):
  d=city[y]
  e=str(d)
  if e in dict:
    check=d
    end=y
    break
  if  e not in dict:
    dict[e]=y

start=city.index(d)
w=end-start
if k<start:
  print(city[k])
  
else:
  kk=k-start
  rest=kk%w
  q=start+rest
  print(city[q])

Solution (Python):
N,K = map(int,input().split())
cc = list(map(int,input().split()))
memo = []
aa = ['.']*N
a = 1
while aa[a-1] == '.':
  memo.append(a)
  aa[a-1] = 'v'
  a = cc[a-1]
if len(memo)>K:
  print(memo[K])
else:
  b = memo.index(a)
  m = len(memo)-b
  mm = (K-b) % m
  del memo[:b]
  print(memo[mm])

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

ls = [-1] * n
now = 0
cnt = 0
path = []
while 1:
    if ls[now] != -1:
        b = ls[now]
        break
    ls[now] = cnt
    path.append(now)
    now = a[now]
    cnt += 1
loop = path[b:]
if k <= b:
    print(path[k] + 1)
else:
    print(loop[(k - b) % len(loop)] + 1)

Solution (Python):
"""
    Satwik_Tiwari ;) .
    12th july , 2020  - Sunday
"""



from __future__ import division, print_function

from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase

from itertools import *
import bisect
from heapq import *
from math import *
from collections import deque
from collections import Counter as counter  
from itertools import combinations as comb 
from itertools import permutations as permutate
from bisect import bisect_left as bl


from bisect import bisect_right as br
from bisect import bisect





BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)






mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") 
def out(var): sys.stdout.write(str(var))  
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())

def zerolist(n): return [0]*n
def nextline(): out("\n")  
def testcase(t):
    for p in range(t):
        solve()
def printlist(a) :
    for p in range(0,len(a)):
        out(str(a[p]) + ' ')
def lcm(a,b): return (a*b)//gcd(a,b)
def power(a,b):
    ans = 1
    while(b>0):
        if(b%2==1):
            ans*=a
        a*=a
        b//=2
    return ans
def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1)))
def isPrime(n) : 
    if (n <= 1) : return False
    if (n <= 3) : return True
    if (n % 2 == 0 or n % 3 == 0) : return False
    i = 5
    while(i * i <= n) :
        if (n % i == 0 or n % (i + 2) == 0) :
            return False
        i = i + 6
    return True



def bfs(g,st,lcatable,level):
    visited = [0]*(len(g))
    visited[st] = 1
    queue = []
    queue.append(st)
    new = []
    while(len(queue) != 0):
        s = queue.pop()
        new.append(s)
        for i in g[s]:
            if(visited[i] == 0):
                level[i] = level[s]+1
                lcatable[i][0] = s
                visited[i] = 1
                queue.append(i)

    return new

def lcatable(g,n,level): 
    mx = 0
    while(2**(mx) <=n):
        mx+=1
    
    lcatable = [[-1]*mx for i in range(n+1)]
    bfs(g,1,lcatable,level)

    for j in range(1,mx):
        for i in range(1,n+1):
            if(lcatable[i][j-1] != -1):
                lcatable[i][j] = lcatable[lcatable[i][j-1]][j-1]

    return lcatable

def dist(x0,y0,x1,y1):
    return (abs(x0-x1) + abs(y0-y1))

def djkistra(g,st,dist): 
    pq = []
    dist[st] = 0
    pq.append([0,st])
    while(len(pq) != 0):
        curr = heappop(pq)[1]
        for i in range(0,len(g[curr])):
            b = g[curr][i][0]
            w = g[curr][i][1]
            if(dist[b] > dist[curr] + w):
                dist[b] = dist[curr]+w
                pq.append([dist[b],b])
    return dist


def solve():
    n,k = sep()
    a = lis()
    visited = [-1]*n

    curr = a[0]
    cnt = 1
    visited[0] = cnt
    cnt+=1
    ind = 0
    while(True):
        if(k==0):
            print(ind+1)
            return
        
        if(visited[curr-1] == -1):
            k-=1
            visited[curr-1] = cnt
            ind = curr-1
            curr = a[curr-1]
            cnt+=1

        else:
            diff = cnt - visited[curr-1]
            
            ind = curr-1
            curr = a[curr-1]
            k-=1
            break
    
    k = k%diff
    
    
    while(k!=0):
        ind = curr-1
        curr = a[curr-1]
        k-=1
    print(ind+1)







testcase(1)

Solution (Python):
import sys
from collections import deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, k, *a = map(int, read().split())

l = [0] * (n + 1)

q = deque([])
i = 1
while True:
    q.append(i)
    l[i] += 1
    if l[i] == 2:
        left = q.index(i)
        right = len(q)-2
        break
    i = a[i - 1]

length = right - left + 1

if k > left:
    b = k - left
    k = b % length
    print(q[left+k])
else:
    print(q[k])

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

visitTowns = list()
visitTimes = [0 for _ in range(N + 1)]

curTown = 1
timeBackTo = 0

curTime = 0
while True:
    if visitTimes[curTown] > 0:
        timeBackTo = visitTimes[curTown]
        break

    visitTowns.append(curTown)
    visitTimes[curTown] = curTime

    
    curTown = portals[curTown]

    curTime += 1

nonLoopCount = timeBackTo
loopSpan = len(visitTowns) - nonLoopCount

if K <= nonLoopCount:
    print(visitTowns[K])
    exit()

rem = (K - nonLoopCount) % loopSpan
print(visitTowns[nonLoopCount + rem])

Solution (Python):
n,k = map(int,input().split())
alist = list(map(int,input().split()))
memo = []
p = 1
vis = ['.']*n
while vis[p-1] == '.':
    memo.append(p)
    vis[p-1] = 'v'
    p = alist[p-1]
 
loop = memo.index(p)
if k < len(memo):
    print(memo[k])
else:
    print(memo[loop+(k-loop)%(len(memo)-loop)])

Solution (Python):
import numpy as np

N, K = map(int, input().split())
A = list(map(int, input().split()))
A = np.int32(A) - 1

nxt = np.zeros((70, N), dtype=int)
nxt[0] = A
for i in range(0, 69):
    nxt[i + 1] = nxt[i, nxt[i]]

pos = 0
for i, v in enumerate(reversed(bin(K)[2:])):
    if v == '1':
        pos = nxt[i, pos]

print(pos + 1)

Solution (Python):
import sys,math,collections,itertools
input = sys.stdin.readline

N,K=list(map(int,input().split()))
A =[0]+list(map(int,input().split()))
town = 1
town_list = [1]
towntown = [0]*(N+1)
towntown[1]=1
count = 0
while True:
    town = A[town]
    count +=1
    if count == K:
        print(town)
        exit()
    if towntown[town] == 0:
        town_list.append(town)
        towntown[town] =1
    else:
        roop_town_num = town
        break
    
l_ini_index = town_list.index(roop_town_num)
len_roop = len(town_list)-l_ini_index 

if K <= l_ini_index:
    print(town_list[K])
else:
    print(town_list[((K-l_ini_index)%len_roop+l_ini_index)])

Solution (Python):
import sys

if sys.subversion[0] == "PyPy":
    import io, atexit

    sys.stdout = io.BytesIO()
    atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))

    sys.stdin = io.BytesIO(sys.stdin.read())
    input = lambda: sys.stdin.readline().rstrip()
RS = raw_input
RA = lambda x=int: map(x, RS().split())
RN = lambda x=int: x(RS())


def solve():
    curr = 1
    left = k
    vis = {}
    while left > 0:
        curr = a[curr-1]
        left -= 1
        if not curr in vis:
            vis[curr] = k - left
        else:
            loopsize = k - left - vis[curr]
            left %= loopsize
            break
    while left > 0:
        curr = a[curr-1]
        left -= 1
    print(curr)


    return





n, k = RA()
a = RA()
solve()