import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import Control.Monad

solve :: Int -> Int -> VU.Vector Int -> Int
solve n k xs = 
    sum [(xs VU.! a) * (xs VU.! b) * (xs VU.! c) |
            a <- [0..k-1]
            , let b = (k-a) `rem` k
            , let c = (k-a) `rem` k
            , ((b + c) `rem` k) == 0]

main = do
    [n,k] <- map read . words <$> getLine :: IO [Int]
    xs <- VUM.replicate (k+1) 0
    forM_ [1..n] $ \i -> do
        VUM.modify xs (+1) (i `rem` k)
    xs' <- VU.unsafeFreeze xs
    print $ solve n k xs'