import Data.Bool
import Data.List
import Data.Maybe
import qualified Data.ByteString.Char8 as B
import qualified Data.IntMap as IM
main=print.solve.map(fst.fromJust.B.readInt).B.words=<<B.getContents
solve(n:k:as)=fst.foldl' f(0,foldr g IM.empty cs)$zip[0..]$zip(ds++repeat k)bs where
  bs=scanl'(\s a->(s+a+k-1)`mod`k)0$as
  (cs,ds)=splitAt k bs
  f(j,m)(i,(c,b))=(j+IM.findWithDefault 0 b n,g c n)where n=IM.update h b m
g b m=IM.insertWith(+)b 1 m
h i=bool Nothing(Just$i-1)$i>1