{-# LANGUAGE BangPatterns         #-}
{-# LANGUAGE DerivingStrategies   #-}
{-# LANGUAGE FlexibleContexts     #-}
{-# LANGUAGE MultiWayIf           #-}
{-# LANGUAGE RankNTypes           #-}
{-# LANGUAGE ScopedTypeVariables  #-}
{-# LANGUAGE UndecidableInstances #-}
import           Control.Monad.ST
import           Control.Monad.State.Strict
import qualified Data.ByteString.Char8        as BS
import           Data.Char
import           Data.List
import qualified Data.Vector.Unboxing         as VU
import qualified Data.Vector.Unboxing.Mutable as VUM

main :: IO ()
main = do
  [n,k] <- readLnAsListWith unconsInt
  avec <- readLnAsUVecWith unconsInt n

  print $ solve n k avec

  return ()

solve :: Int -> Int -> VU.Vector Int -> Int
solve n k avec = runST $ do
  count <- VUM.replicate n (-1)
  (i,t,l) <- go count 0 0

  if | k <= t -> ho count 0 t
     | otherwise -> ho count ((t-k)`mod`l) i



  where go :: VUM.MVector s Int -> Int -> Int -> ST s (Int,Int,Int)
        go cvec i c = do
          let i' = avec VU.! i - 1
          c' <- VUM.read cvec i
          if | c' /= -1 -> return (i',c',c-c')
             | otherwise -> do VUM.write cvec i' c
                               go cvec i' (c+1)

        ho :: VUM.MVector s Int -> Int -> Int -> ST s Int
        ho _ 0 i = return (i+1)
        ho cvec m i = do
          i' <- VUM.read cvec i
          ho cvec (m-1) (i'-1)

-- converter
unconsInt :: StateT BS.ByteString Maybe Int
unconsInt = StateT $ BS.readInt . BS.dropWhile isSpace

-- 1D Data
-- for list
readLnAsListWith :: StateT BS.ByteString Maybe a -> IO [a]
readLnAsListWith !st = unfoldr (runStateT st) <$> BS.getLine

-- for unboxed vector
readLnAsUVecWith :: VU.Unboxable a => StateT BS.ByteString Maybe a -> Int -> IO (VU.Vector a)
readLnAsUVecWith !st !n = VU.unfoldrN n (runStateT st) <$> BS.getLine
