Arkady and his friends love playing checkers on an $n \times n$ field. The rows and the columns of the field are enumerated from $1$ to $n$.

The friends have recently won a championship, so Arkady wants to please them with some candies. Remembering an old parable (but not its moral), Arkady wants to give to his friends one set of candies per each cell of the field: the set of candies for cell $(i, j)$ will have exactly $(i^2 + j^2)$ candies of unique type.

There are $m$ friends who deserve the present. How many of these $n \times n$ sets of candies can be split equally into $m$ parts without cutting a candy into pieces? Note that each set has to be split independently since the types of candies in different sets are different.


-----Input-----

The only line contains two integers $n$ and $m$ ($1 \le n \le 10^9$, $1 \le m \le 1000$) — the size of the field and the number of parts to split the sets into.


-----Output-----

Print a single integer — the number of sets that can be split equally.


-----Examples-----
Input
3 3

Output
1

Input
6 5

Output
13

Input
1000000000 1

Output
1000000000000000000



-----Note-----

In the first example, only the set for cell $(3, 3)$ can be split equally ($3^2 + 3^2 = 18$, which is divisible by $m=3$).

In the second example, the sets for the following cells can be divided equally:   $(1, 2)$ and $(2, 1)$, since $1^2 + 2^2 = 5$, which is divisible by $5$;  $(1, 3)$ and $(3, 1)$;  $(2, 4)$ and $(4, 2)$;  $(2, 6)$ and $(6, 2)$;  $(3, 4)$ and $(4, 3)$;  $(3, 6)$ and $(6, 3)$;  $(5, 5)$. 

In the third example, sets in all cells can be divided equally, since $m = 1$.

Difficulty: interview

Starter code:

Solutions:
s = input().split()
n, m = int(s[0]), int(s[1])
qr = {}
for i in range(1, m+1):
    num = (n-i)//m+1
    qr[(i**2)%m] = qr.get((i**2)%m,0)+ num
print(sum(qr.get(i%m,0) * qr.get((m-i)%m,0) for i in range(m)))


n, m = list(map(int, input().split()))
nOfM = [0 for i in range(m)]
for md in range(m):
    if md <= n:
        nOfM[md] = (n - md) // m
        if md != 0:
            nOfM[md] += 1
res = 0
for m1 in range(m):
    for m2 in range(m):
        if (m1 * m1 + m2 * m2) % m == 0:
            res += nOfM[m1] * nOfM[m2]
print(res)


#      
import collections, atexit, math, sys, bisect 

sys.setrecursionlimit(1000000)
def getIntList():
    return list(map(int, input().split()))    

try :
    #raise ModuleNotFoundError
    import numpy
    def dprint(*args, **kwargs):
        #print(*args, **kwargs, file=sys.stderr)
        # in python 3.4 **kwargs is invalid???
        print(*args,  file=sys.stderr)
    dprint('debug mode')
except Exception:
    def dprint(*args, **kwargs):
        pass



inId = 0
outId = 0
if inId>0:
    dprint('use input', inId)
    sys.stdin = open('input'+ str(inId) + '.txt', 'r') #标准输出重定向至文件
if outId>0:
    dprint('use output', outId)
    sys.stdout = open('stdout'+ str(outId) + '.txt', 'w') #标准输出重定向至文件
    atexit.register(lambda :sys.stdout.close())     #idle 中不会执行 atexit
    
N, M = getIntList()
#print(N)

zs = [ N//M for _ in range(M)]

for i in range(1,N%M+1):
    zs[i] +=1
dprint(zs)
zs1 = [0 for _ in range(M)]

for i in range(M):
    t = i*i%M
    zs1[t] += zs[i]

dprint(zs1)
    
re = 0

for i in range(1,M):
    re += zs1[i] * zs1[M-i]

re += zs1[0] * zs1[0]

print(re)








n,m=map(int,input().split())
k=n%m
j=(n-k)//m
total=0
actual=0
for i in range(1,m+1):
    for l in range(1,m+1):
        if (i**2+l**2)%m==0:
            total+=1
            if 0<i<=k and 0<l<=k:
                actual+=(j+1)**2
            elif 0<i<=k or 0<l<=k:
                actual+=j*(j+1)
            else:
                actual+=j**2
if k==0:
    print(j**2*total)
else:
    print(actual)

n,m=list(map(int,input().split()))



#各iはn//m
#+
#if i <=n%m:
#+1

LIST=[None]*(m+1)

for i in range(1,m+1):
    LIST[i]=n//m

    if i<=n%m:
        LIST[i]+=1

ANS=0
for i in range(1,m+1):
    for j in range(1,m+1):
        if (i**2+j**2)%m==0:
            ANS+=LIST[i]*LIST[j]

print(ANS)


n, m = list(map(int, input().split()))
cnt = [0] * m
for i in range(m):
  cnt[i] = n // m
  if i == 0:
    cnt[i] -= 1
  if i <= n % m:
    cnt[i] += 1
r = 0
for i in range(m):
  for j in range(m):
    if ((i ** 2) + (j ** 2)) % m == 0:
      r += cnt[i] * cnt[j]
print(r)


n, m = map(int, input().split())
z = [0] * m
if n >= m:
	for i in range(1, 1 + m):
		z[i * i % m] += n // m

for i in range(1, 1 + n % m):
	z[i * i % m] += 1
#print(z)
ans = 0
#print(z)
for i in range(m):
	q = (m - i) % m
	#print(i, q, z[i], z[q])
	ans += max(z[i] * z[q], 0)
print(ans) 

n, m = list(map(int, input().split()))
cnt = [0 for _ in range(m)]
for x in range(min(m, n + 1)):
    z = n // m
    if n % m >= x:
        z += 1
#    print(x, z)
    cnt[x * x % m] += z
cnt[0] -= 1
#print(cnt)
res = 0
for i  in range(m):
    res += cnt[i] * cnt[(m - i) % m]
print(res)


def brute(n, m):
	ans = 0
	for i in range(1, n + 1, 1):
		for j in range(1, n + 1, 1):
			val = (i*i + j*j) % m;
			if val == 0:
				ans += 1
	return ans

def solve(n, m):
	cnt = [0] * m;
	for i in range(1, m * m + 1, 1):
		upto = n // (m * m)
		extra = n - upto * (m * m)
		if extra >= i:
			upto += 1
		cnt[(i * i) % m] += upto

	ans = 0
	for i in range(0, m):
		for j in range(0, m):
			if (i + j) % m == 0:
				ans += cnt[i] * cnt[j]
	return ans

(n, m) = list(map(int, input().split(' ')))
print(solve(n, m))

# for n in range(1, 50, 1):
# 	for m in range(1, 50, 1):
# 		a = brute(n, m)
# 		b = solve(n, m)
# 		# print(a, b)
# 		if a != b:
# 			1/0

# print(ans)

# print(brute(n, m))


import sys
from math import *

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

n, m = mints()
k = n//m
c = [0] * m
for i in range(m):
	c[(i*i)%m] += k
c[0] -= 1
for i in range(k*m,n+1):
	c[(i*i)%m] += 1
r = 0
c.append(c[0])
for i in range(m):
	r += c[i]*c[m-i]
print(r)

n, m=map(int, input().split())
a=[0 for i in range(m)]
for i in range(m):
  a[i]=n//m
for i in range(1, n%m+1):
  a[i]+=1
res=0
for i in range(m):
  for j in range(m):
    if ((i*i+j*j)%m==0):
      res+=a[i]*a[j];
print (res)

s=input().split()
n=int(s[0])
m=int(s[1])
ans=0
for i in range(1,min(n+1,m+1)):
    ii=i**2
    for j in range(i,min(n+1,m+1)):
        jj=j**2
        if (ii+jj)%m==0:
            if j>i:
                ans+=(1+(n-j)//m)*(1+(n-i)//m)*2
            else:
                ans+=(1+(n-j)//m)**2
print(ans)

n, m = map(int, input().split())
f = lambda r: ((n - r)//m) - ((m - r)//m) + 1
res = 0
for i in range(m):
    for j in range(i, m):
        if ((i**2) + (j**2)) % m == 0:
            # print(i, j, f(i), f(j))
            res += f(i) * f(j) * (2 if i != j else 1) 
print(res)

n, k = map(int, input().split(' '))

def calc(x):
  m = n + x
  return (m // k)
total = 0
for i in range(k):
  for j in range(k):
    if ((i**2 + j**2) % k) == 0:
      total += calc(i) * calc(j)

print(total)

from math import ceil
n, m = map(int, input().split())
ans = 0
for i in range(1, m + 1):
    for j in range(1, m + 1):
        if (i ** 2 + j ** 2) % m == 0:
            ch1, ch2 = ceil((n - i + 1) / m), ceil((n - j + 1) / m)
            ans += ch1 * ch2
print(ans)

n, m = list(map(int, input().split()))
x = n % m
y = n // m
s = 0
for i in range(1, m + 1):
    for j in range(i, m + 1):
        if (i ** 2 + j ** 2) % m == 0:
            a, b = 0, 0
            if i <= x:
                a = 1
            if j <= x:
                b = 1
            r = (y + a) * (y + b)
            if i != j:
                s += r * 2
            else:
                s += r
print(s)


import math
n,m = list(map(int, input().split()))

modulo = [ 0 for x in range(0,m)]

for x in range(1,m+1):
    modulo[(x*x)%m]+=1
#mnoze ile trzeba
for index in range(m):
    modulo[index]*=math.floor(n/m)

war = math.floor(n/m)*m
for x in range(war+1,n+1):
    modulo[(x*x)%m]+=1

suma = 0

for ind in range(1,m):
    war = modulo[ind]*modulo[m-ind]
    suma = suma + war


suma+=modulo[0]*modulo[0]

print(suma)



n, m = [int(w) for w in input().split()]

d = n // m
r = n % m
s = d * d

a = 0

for i in range(1, m+1):
    for j in range(1, m+1):
        if (i*i+j*j) % m  == 0:
            a += d*d
            if i <= r:
                a += d
            if j <= r:
                a += d
            if i <= r and j <= r:
                a += 1

print(a)


n, m = list(map(int, input().split()))
k = n // m
rest = [0] * m
for i in range(1, m+1):
    rest[i**2 % m] += k
for i in range(1, n % m + 1):
    rest[i**2 % m] += 1

r = rest[0]**2
for i in range(1, m):
    r += rest[i]*rest[-i]
print(r)


a, b = map(int, input().split())
A = set()
A_ = set()
B_ = set()
A__ = {}
B__ = {}
calc = 0
for i in range(1, b + 1):
    for j in range(1, b + 1):
        if (i ** 2 + j ** 2) % b == 0:
            A.add((i % b, j % b))
            B_.add(j % b)
            A_.add(i % b)
for i in A_:
    A__[i] = a // b
    if a % b >= i and i != 0:
        A__[i] += 1
for i in B_:
    B__[i] = a // b
    if a % b >= i and i != 0:
        B__[i] += 1
for i in A:
    calc += A__[i[0]] * B__[i[1]]
print(calc)

n,m = map(int, input().split())
arr = {}
first = []
times = n//m

for i in range(1,m+1):
    tmp = i%m
    if tmp != 0:
        tmp *= tmp
        tmp %= m

    tmp2 = arr.get(tmp,0)
    arr[tmp] = tmp2+times
    first.append(tmp)

for i in range(n-(times*m)):
    arr[first[i]] += 1

result = 0
for key in arr.keys():
    comp = (m-key)%m
    e1 = arr[key]
    e2 = arr.get(comp,0)

    if comp != 0 or (m%2==0 and comp!=m//2):
        result += e1*e2
    else:
        result += (e1*e1)
print(result)

from operator import itemgetter
#int(input())
#map(int,input().split())
#[list(map(int,input().split())) for i in range(q)]
#print("YES" * ans + "NO" * (1-ans))
n,m = list(map(int,input().split()))
ai = [0] * m
for i in range(m):
    ai[i*i % m] += 1
ai2 = [0] * m
n3 = n % m
for i in range(1,n3+1):
    ai2[i*i % m] += 1
n2 = n // m

ans = 0
for i in range(m):
    i2 = (m - i) % m
    ans += ((ai[i] * n2 + ai2[i]) * (ai[i2] * n2 + ai2[i2]))
print(ans)


n,m=[int(i) for i in input().split()]
a=[(n-i)//m+1 for i in range(0,m+1)] #a[1],a[2],...,a[m]
s=0
for i in range(1,m+1):
    for j in range(1,m+1):
        if((i*i+j*j)%m==0):
            s+=a[i]*a[j]
print(s)