import Control.Applicative import Data.List import Data.Char (digitToInt) main :: IO () main = solve <$> f <*> getLine >>= print where f = map read <$> words <$> getLine solve :: [Int] -> String -> Int solve [n, k] s | q == 0 = fst . ice 0 $ take r is | cb <= cat = cb | otherwise = cb + (q-1) * (cb - cat) + (fst . ice cat $ take r is) where is = map digitToInt s (q, r) = divMod k n (cb, cat) = ice 0 is ice :: Int -> [Int] -> (Int, Int) ice at ibs = f 0 at ibs where f cib cat [] = (cib, cat) f cib cat xs | cat == 0 = f (cib + 1) (head xs) (tail xs) | otherwise = let (as, bs) = splitAt cat xs in f cib (cat - length as + sum as) bs