import Data.List
import qualified Data.Map as Map

f::Int->Int->[Int]
f 0 _=[]
f n k=(n `mod` k):f (n-1) k

f2 ::Int->[[Int]]->[(Int,Int)]
f2 _ []=[]
f2 k (x:xs)=if (head x)*2 `mod` k==0 then ((k-(head x)) `mod` k,(length x)):f2 k xs else f2 k xs

f3 ::[[Int]]->[(Int,Int)]
f3 []=[]
f3 (x:xs)=(head x,length x):f3 xs

f4 :: [(Int,Int)]->[(Int,Int)]->Int
f4 [] _=0
f4 _ []=0
f4 ((x,x2):xs) ((y,y2):ys)
       |x<y=f4 xs ((y,y2):ys)
       |x>y=f4 ((x,x2):xs) ys
       |otherwise=x2^2*y2+f4 xs ((y,y2):ys) 
main = do
       a<-getLine
       let (n:k:[])=(map read . words) a::[Int]
           as=group $ sort $ f n k
           bs= Map.toList $ Map.fromListWith (+) $ f2 k as
           cs=f3 as
       print $ f4 bs cs