import Data.List

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

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

f3::Int->Int->[Int]->[(Int,Int)]
f3 _ _ []=[]
f3 n k (x:xs)=(n,x):f3 (n+1) k 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*y2+f4 xs ((y,y2):ys) 
main = do
       a<-getLine
       let (n:k:[])=(map read . words) a::[Int]
           as=map length $ group $ sort $ f n k
           bs=map (^2) as
           cs=sort $ f2 0 k bs
           ds=f3 0 k as
       print $ f4 cs ds