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$.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Number theory

Mathematics


Solutions:
from collections import defaultdict, deque, Counter
from sys import stdin, stdout
from heapq import heappush, heappop
import math
import io
import os
import math
import bisect

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def digits(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def ceil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def mapin():
	return map(int, input().split())
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(n, m) = mapin()
ans = 0
for i in range(1, m + 1):
	for j in range(1, m + 1):
		if (i * i + j * j) % m == 0:
			ans += ((n - i) // m + 1) * ((n - j) // m + 1)
print(ans)


(n, m) = 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)


def ans(n, m):
	res = 0
	table = [[0 for j in range(m)] for i in range(m)]
	s = 0
	for i in range(1, min(m, n) + 1):
		for j in range(i, min(m, n) + 1):
			if (i * i + j * j) % m == 0:
				table[i - 1][j - 1] = 1
				s += 1
				if i != j:
					table[j - 1][i - 1] = 1
					s += 1
	res = (n // m) ** 2 * s
	rem = n % m
	s = 0
	row = 0
	col = 0
	for i in range(rem):
		for j in range(min(m, n)):
			row += table[i][j]
			col += table[j][i]
		for j in range(rem):
			s += table[i][j]
	return res + s + n // m * (row + col)
(n, m) = map(int, input().split())
print(ans(n, m))


(n, m) = map(int, input().split())
rec = [0] * (m + 1)
for i in range(1, m + 1):
	rec[i] += n // m
for i in range(1, n % m + 1):
	rec[i] += 1
rec2 = [0] * m
for i in range(m + 1):
	rec2[i ** 2 % m] += rec[i]
ans = 0
for i in range(m):
	if i == m - i and i > m - i:
		continue
	if i == 0:
		ans += rec2[i] * rec2[i]
	else:
		ans += rec2[i] * rec2[m - i]
print(ans)


import math

def main():
	(n, m) = map(int, input().split())
	inc = 0
	x = m
	for a in range(1, x + 1):
		for b in range(1, x + 1):
			if (a ** 2 + b ** 2) % m == 0:
				inc += ((n - a) // m + 1) * ((n - b) // m + 1)
	print(inc)
main()


(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:
			if n % m >= i:
				if n % m >= j:
					ans += (n // m + 1) * (n // m + 1)
				else:
					ans += (n // m + 1) * (n // m)
			elif n % m >= j:
				ans += (n // m + 1) * (n // m)
			else:
				ans += n // m * (n // m)
print(ans)


(n, m) = map(int, input().split())
mat = [[0 for i in range(m)] for j in range(m)]
for i in range(m):
	for j in range(m):
		mat[i][j] = (i + 1) ** 2 + (j + 1) ** 2
		mat[i][j] %= m
zeros = [[0 for i in range(m)] for j in range(m)]
for j in range(m):
	if mat[0][j] == 0:
		zeros[0][j] = 1
	for i in range(1, m):
		zeros[i][j] = zeros[i - 1][j]
		if mat[i][j] == 0:
			zeros[i][j] += 1
downExtend = [0 for i in range(m)]
for i in range(m):
	downExtend[i] = zeros[m - 1][i] * (n // m)
	if n % m != 0:
		downExtend[i] += zeros[n % m - 1][i]
SUM = sum(downExtend)
ans = SUM * (n // m) + sum(downExtend[:n % m])
print(ans)


(n, m) = map(int, input().split())
cnt2 = [0] * m
for i in range(m):
	cnt2[i * i % m] += n // m
for i in range(1, n % m + 1):
	cnt2[i * i % m] += 1
ans = 0
for i in range(m):
	ans += cnt2[i] * cnt2[(m - i) % m]
print(ans)


from sys import stdin
(a, b) = map(int, stdin.readline().strip().split())
ans = 0
dp = [[0 for i in range(b + 1)] for j in range(b + 1)]
dp1 = [[0 for i in range(b + 1)] for j in range(b + 1)]
for i in range(1, b + 1):
	for j in range(1, b + 1):
		if (i * i % b + j * j % b) % b == 0:
			dp[i][j] += 1
		dp[i][j] += dp[i - 1][j] + (dp[i][j - 1] - dp[i - 1][j - 1])
md = a % b
a1 = a - md
ans = a1 * a1 // (b * b) * dp[-1][-1]
md = a % b
ans += a // b * dp[-1][md]
ans += a // b * dp[md][-1]
ans += dp[md][md]
print(ans)


def calc(f, t, m):
	count = 0
	for i in range(1, f + 1):
		for j in range(1, t + 1):
			if (i * i + j * j) % m == 0:
				count += 1
	return count
(n, m) = [int(x) for x in input().split()]
(c, r) = divmod(n, m)
m2m = calc(m, m, m)
r2m = calc(r, m, m)
r2r = calc(r, r, m)
ans = m2m * c * c + 2 * r2m * c + r2r
print(ans)


from collections import *
(n, m) = map(int, input().split())
C = lambda k: Counter((i * i % m for i in range(1, k + 1)))
(c, d) = (C(m), C(n % m))
r = Counter(dict(((k, n // m * c[k] + d[k]) for k in c)))
print(sum((r[k] * r[(m - k) % m] for k in r)))


(n, m) = map(int, input().split())
ans = 0
for i in range(1, m + 1):
	for j in range(1, m + 1):
		if (i * i + j * j) % m == 0:
			a = b = 0
			if n % m >= i:
				a = 1
			if n % m >= j:
				b = 1
			ans += (n // m + a) * (n // m + b)
print(ans)


import sys
from math import sqrt
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 1000000007
(n, m) = map(int, input().split())
t = n // m
ck = n % m
ans = 0
for i in range(m):
	for j in range(m):
		if (i ** 2 + j ** 2) % m == 0:
			a = b = t
			if i <= ck and i:
				a += 1
			if j <= ck and j:
				b += 1
			ans += a * b
print(ans)


(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) = map(int, input().split())
cnt = 0
cnt2 = 0
cnt3 = 0
for i in range(1, m + 1):
	for j in range(i + 1, m + 1):
		if (i ** 2 + j ** 2) % m == 0:
			cnt += 2
	if i ** 2 * 2 % m == 0:
		cnt += 1
for i2 in range(n - n % m + 1, n + 1):
	for j2 in range(1, m + 1):
		if (i2 ** 2 + j2 ** 2) % m == 0:
			cnt2 += 1
for i3 in range(n - n % m + 1, n + 1):
	for j3 in range(i3 + 1, n + 1):
		if (i3 ** 2 + j3 ** 2) % m == 0:
			cnt3 += 2
	if i3 ** 2 * 2 % m == 0:
		cnt3 += 1
print(cnt * (n // m) ** 2 + cnt2 * 2 * (n // m) + cnt3)


(n, m) = 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
	cnt[x * x % m] += z
cnt[0] -= 1
res = 0
for i in range(m):
	res += cnt[i] * cnt[(m - i) % m]
print(res)


nm = input().split()
n = int(nm[0])
m = int(nm[1])
x = [0 for _ in range(m)]
for i in range(1, m + 1):
	x[int(i ** 2) % m] += 1
for i in range(m):
	x[i] *= n // m
for i in range(n // m * m + 1, n + 1):
	x[int(i ** 2) % m] += 1
s = 0
for i in range(m):
	s += x[i] * x[-i]
print(s)


(n, m) = map(int, input().split())
arr = [n // m] * m
for i in range(1, n % m + 1):
	arr[i] += 1
ost = [i for i in range(m)]
ans = 0
for x in ost:
	for y in ost:
		if (x ** 2 + y ** 2) % m == 0:
			ans += arr[x] * arr[y]
print(ans)


(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)


(n, m) = map(int, input().split())
sum1 = 0
sum2 = 0
sum3 = 0
for i in range(1, m + 1):
	for j in range(1, m + 1):
		if (i ** 2 + j ** 2) % m == 0:
			sum1 += 1
for i in range(1, m + 1):
	for j in range(1, n % m + 1):
		if (i ** 2 + j ** 2) % m == 0:
			sum2 += 1
for i in range(1, n % m + 1):
	for j in range(1, n % m + 1):
		if (i ** 2 + j ** 2) % m == 0:
			sum3 += 1
print((n // m) ** 2 * sum1 + n // m * sum2 * 2 + sum3)


(n, m) = map(int, input().split())
out = 0
for i in range(1, min(m, n) + 1):
	for j in range(1, min(m, n) + 1):
		if (i * i + j * j) % m == 0:
			out += ((n - i) // m + 1) * ((n - j) // m + 1)
print(out)


from sys import stdin
from collections import Counter
(n, m) = map(int, stdin.readline().split())
(Ca, Cb) = (Counter(), Counter())
r = n % m
for i in range(1, m + 1):
	x = i % m * (i % m) % m
	if i <= r:
		Cb[x] += 1
	Ca[x] += 1
d = n // m
for c in Ca:
	Ca[c] = Ca[c] * d + Cb[c]
C = 0
for i in range(m // 2 + 1):
	if Ca[i]:
		j = (m - i) % m
		if i == j:
			C += Ca[i] * Ca[i]
		else:
			C += Ca[i] * Ca[j] * 2
print(C)


(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)


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


from math import *
(n, m) = map(int, input().split())
ans = 0
for i in range(1, min(n, m) + 1):
	for j in range(1, min(n, m) + 1):
		if (i * i % m + j * j % m) % m == 0:
			count1 = n // m
			count2 = n // m
			if n % m >= i:
				count1 += 1
			if n % m >= j:
				count2 += 1
			ans += count1 * count2
print(ans)


from sys import stdin, stdout
I = lambda : stdin.readline()
(n, m) = map(int, I().split())
a = [0] * m
s = 0
for i in range(1, m + 1):
	a[i * i % m] += (n - i) // m + 1
for i in range(m):
	s += a[i] * a[-i]
stdout.write(str(s) + '\n')


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


def DivideCandies(n, m):
	paresCount = 0
	(cociente, resto) = divmod(n, m)
	for i in range(1, m + 1):
		for j in range(1, i + 1):
			if (i ** 2 + j ** 2) % m == 0:
				filaCount = cociente
				colCount = cociente
				if j <= resto:
					colCount += 1
				if i <= resto:
					filaCount += 1
				actualCount = filaCount * colCount
				if i != j:
					actualCount *= 2
				paresCount += actualCount
	return paresCount
(n, m) = map(int, input().split())
paresCount = DivideCandies(n, m)
print(paresCount)


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


import sys
from math import sqrt, gcd
from bisect import bisect
from collections import defaultdict
inp = sys.stdin.readline
read = lambda : list(map(int, inp().strip().split()))

def solve():
	(n, m) = read()
	dic = [0] * m
	for i in range(m):
		dic[i ** 2 % m] += 1
	for i in range(m):
		dic[i] *= n // m
	for i in range(1, n % m + 1):
		dic[i ** 2 % m] += 1
	ans = 0
	for i in range(m):
		ans += dic[i] * dic[(m - i) % m]
	print(ans)
solve()


(n, m) = map(int, input().split())
s = list()
for i in range(1, m + 1):
	for j in range(1, m + 1):
		if (i ** 2 + j ** 2) % m == 0:
			s.append([i, j])
ans = 0
for k in s:
	ky = (n - k[0]) // m + 1
	my = (n - k[1]) // m + 1
	ans += ky * my
print(ans)


(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())
i = 1
k = []
while i <= m:
	j = 1
	while j <= m:
		if (i * i + j * j) % m == 0:
			k += [[i, j]]
		j += 1
	i += 1
k.sort()
s = 0
for i in k:
	if i[1] <= n and i[0] <= n:
		s += ((n - i[0]) // m + 1) * ((n - i[1]) // m + 1)
print(s)


(n, m) = map(int, input().split())
cnt = 0
if n <= m:
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			if (pow(i, 2) + pow(j, 2)) % m == 0:
				cnt += 1
else:
	for i in range(1, m + 1):
		for j in range(1, m + 1):
			if (pow(i, 2) + pow(j, 2)) % m == 0:
				cnt += 1
	cnt *= pow(n // m, 2)
	a = 0
	for i in range(1, m + 1):
		for j in range(1, n % m + 1):
			if (pow(i, 2) + pow(j, 2)) % m == 0:
				a += 1
	cnt += a * (n // m)
	b = 0
	for i in range(1, n % m + 1):
		for j in range(1, m + 1):
			if (pow(i, 2) + pow(j, 2)) % m == 0:
				b += 1
	cnt += b * (n // m)
	for i in range(1, n % m + 1):
		for j in range(1, n % m + 1):
			if (pow(i, 2) + pow(j, 2)) % m == 0:
				cnt += 1
print(cnt)


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


import sys
from collections import *

def solve(io):
	N = io.read_int()
	M = io.read_int()
	mod = [0] * M
	base_freq = N // M
	for i in range(0, M):
		f = base_freq
		if i < N % M:
			f += 1
		j = i + 1
		m = j * j % M
		mod[m] += f
	cnt = mod[0] * mod[0]
	for i in range(1, M):
		cnt += mod[i] * mod[M - i]
	io.println(cnt)

class IO:
	in_stream = None
	out_stream = None
	raw = ''
	buf = []
	pos = 0

	def __init__(self, input_stream, output_stream):
		self.in_stream = input_stream
		self.out_stream = output_stream

	def read_to_buffer(self):
		self.raw = self.in_stream.readline().rstrip('\n')
		self.buf = self.raw.split()
		self.pos = 0

	def read_string(self):
		while self.pos == len(self.buf):
			self.read_to_buffer()
		ans = self.buf[self.pos]
		self.pos += 1
		return ans

	def read_int(self):
		return int(self.read_string())

	def read_float(self):
		return float(self.read_string())

	def read_string_array(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.read_string())
		return arr

	def read_int_array(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.read_int())
		return arr

	def read_float_array(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.read_float())
		return arr

	def read_line(self):
		while self.pos == len(self.buf):
			self.read_to_buffer()
		if self.pos > 0:
			raise ValueError('Cannot call read_line in the middle of a line.')
		return self.raw

	def print(self, s):
		self.out_stream.write(str(s))

	def println(self, s):
		self.print(s)
		self.print('\n')

	def println_array(self, arr, sep=' '):
		self.println(sep.join((str(x) for x in arr)))

	def flush_output(self):
		self.out_stream.flush()
pythonIO = IO(sys.stdin, sys.stdout)
solve(pythonIO)
pythonIO.flush_output()


def calc(x, y):
	cnt = 0
	for i in range(1, y + 1):
		for j in range(1, x + 1):
			if (pow(i, 2) + pow(j, 2)) % m == 0:
				cnt += 1
	return cnt
(n, m) = map(int, input().split())
cnt = 0
if n <= m:
	cnt = calc(n, n)
else:
	cnt = calc(m, m)
	cnt *= pow(n // m, 2)
	a = calc(n % m, m)
	cnt += a * (n // m) * 2
	cnt += calc(n % m, n % m)
print(cnt)


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) = 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)


from math import *
(n, m) = list(map(int, input().split()))
ans = 0
for i in range(1, min(m, n) + 1):
	for k in range(1, min(m, n) + 1):
		if (i * i + k * k) % m == 0:
			ans += ((n - i) // m + 1) * ((n - k) // m + 1)
print(ans)


def mul(i, n, m):
	if i == 0:
		i += m
	return n // m + (i <= n % m)
from math import ceil
(n, m) = map(int, input().split())
cnt = 0
for i in range(m):
	for j in range(i, m):
		if (i * i + j * j) % m == 0:
			cnt += mul(i, n, m) * mul(j, n, m)
			if i != j:
				cnt += mul(i, n, m) * mul(j, n, m)
print(cnt)


import collections
import time
import os
import sys
import bisect
import heapq
(N, M) = map(int, input().split())
pairs = []
for x in range(1, M + 1):
	for y in range(1, M + 1):
		if (x ** 2 + y ** 2) % M == 0:
			pairs.append((x, y))
ans = 0
segs = N // M
rem = N % M
ans = 0
for (x, y) in pairs:
	(cx, cy) = (segs, segs)
	if x <= rem:
		cx += 1
	if y <= rem:
		cy += 1
	ans += cx * cy
print(ans)


(n, m) = 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)


(n, m) = map(int, input().split())
a = 0
an = 0
ans = 0
x = n % m
y = n // m
for i in range(m):
	for j in range(m):
		if (i * i + j * j) % m == 0:
			a += 1
for i in range(1, x + 1):
	for j in range(1, x + 1):
		if (i * i + j * j) % m == 0:
			an += 1
for i in range(1, x + 1):
	for j in range(m):
		if (i * i + j * j) % m == 0:
			ans += 1
print(y * y * a + 2 * y * ans + an)


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max, nsmallest, nlargest

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	tc = 1
	for _ in range(tc):
		(n, m) = ria()
		d = {}
		z = []
		for i in range(1, m + 1):
			for j in range(i, m + 1):
				if (i * i + j * j) % m == 0:
					z.append([i, j])
		ans = 0
		for i in z:
			times1 = 1 + (n - i[0]) // m
			times2 = 1 + (n - i[1]) // m
			ans += times1 * times2
			if i[0] != i[1]:
				ans += times1 * times2
		wi(ans)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


(n, m) = map(int, input().split())
lst = [0] * m
for i in range(1, m + 1):
	lst[i * i % m] += (n - i) // m + 1
print(sum((x * lst[-i] for (i, x) in enumerate(lst))))


import os
import sys
from io import BytesIO, IOBase
from collections import Counter

def main():
	(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:
				ans += ((n - i) // m + 1) * ((n - j) // m + 1)
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from operator import itemgetter
(n, m) = 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)


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) = list(map(int, input().split()))
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) = 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
ans = 0
for i in range(m):
	q = (m - i) % m
	ans += max(z[i] * z[q], 0)
print(ans)


def candies(n, m):
	(v, d) = ([0] * (m + 1), n // m)
	r = n - d * m
	for i in range(1, m + 1):
		modulo = i * i % m
		if i <= r:
			v[modulo] += d + 1
		else:
			v[modulo] += d
	s = v[0] * v[0]
	for i in range(1, m):
		s += v[i] * v[m - i]
	return s
(N, M) = [int(x) for x in input().split()]
print(candies(N, M))


import sys
import random
import copy
from itertools import permutations, combinations
from math import sqrt, fabs, ceil
from collections import namedtuple
in_file_path = 'input.txt'
output_file_path = 'output.txt'
SUBMIT = True

def get_array(x, initial=None):
	dimension = len(x)
	if dimension == 1:
		return [copy.deepcopy(initial) for _ in range(x[0])]
	else:
		return [get_array(x[1:], initial) for _ in range(x[0])]

def read_num(fin, num_type=int):
	tmp_list = [num_type(x) for x in fin.readline().strip().split()]
	if len(tmp_list) == 1:
		return tmp_list[0]
	else:
		return tuple(tmp_list)

def read_num_list(fin, num_type=int):
	return [num_type(x) for x in fin.readline().strip().split()]

def solve(fin):
	(n, m) = read_num(fin)
	a = [n // m] * m
	for i in range(1, n % m + 1):
		a[i] += 1
	new_a = [0] * m
	for i in range(0, m):
		new_a[i * i % m] += a[i]
	a = new_a
	ans = 0
	for x in range(m):
		y = (m - x) % m
		ans += a[x] * a[y]
	print(ans)
if SUBMIT:
	solve(sys.stdin)
else:
	solve(open(in_file_path, 'r'))


(n, m) = list(map(int, input().split()))
a = {}
for i in range(m):
	d = (i + 1) ** 2 % m
	if d not in a.keys():
		a[d] = 0
	a[d] += n // m
	if i < n % m:
		a[(i + 1) ** 2 % m] += 1
s = 0
for i in a:
	c = (m - i) % m
	if c in a.keys():
		s += a[c] * a[i]
print(s)


ln = [int(i) for i in input().split(' ')]
n = ln[0]
m = ln[1]
c = 0
for i in range(1, m + 1):
	for j in range(1, m + 1):
		if (i * i % m + j * j % m) % m == 0:
			c += 1
sq = n // m
c = c * (sq * sq)
l = n % m
nc = 0
for i in range(1, l + 1):
	for j in range(1, m + 1):
		if (i * i % m + j * j % m) % m == 0:
			nc += 1
c += nc * sq * 2
for i in range(1, l + 1):
	for j in range(1, l + 1):
		if (i * i % m + j * j % m) % m == 0:
			c += 1
print(c)


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))


cnt = 0
(n, m) = map(int, input().strip().split())
mod = [0 for i in range(0, m)]
i = 1
la = []
while i <= m:
	v = i % m * (i % m) % m
	mod[v] += 1
	i += 1
mod = [x * (n // m) for x in mod]
for i in range(1, n % m + 1):
	v = i % m * (i % m) % m
	mod[v] += 1
for (i, x) in enumerate(mod):
	if i != 0 and x != 0:
		cnt += mod[m - i] * x
	else:
		cnt += x * x
print(cnt)


(n, m) = 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)


(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) = map(int, input().split())
rem = [0] * m
sqrem = [i * i % m for i in range(m)]
res = 0
for i in range(m):
	c = n // m
	if i <= n % m and i > 0:
		c += 1
	rem[sqrem[i]] += c
for i in range(0, m):
	res += rem[i] * rem[(m - i) % m]
print(res)


(n, m) = map(int, input().split())
t = [0] * (m + 1)
for i in range(1, m + 1):
	if i <= n % m:
		t[i * i % m] += n // m + 1
	else:
		t[i * i % m] += n // m
ans = t[0] * t[0]
for i in range(1, m):
	ans += t[i] * t[m - i]
print(ans)


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def ceil(a, b):
	return (a + b - 1) // b
S1 = 'abcdefghijklmnopqrstuvwxyz'
S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
(n, m) = mp()
ans = 0
for i in range(1, m + 1):
	for j in range(1, m + 1):
		if (i * i % m + j * j % m) % m == 0:
			ans += ((n - i) // m + 1) * ((n - j) // m + 1)
print(ans)


(a, b) = input().split()
n = int(a)
m = int(b)
i = 0
j = m
q = int(n / m)
r = n % m
kol = 0
for i in range(0, m):
	for j in range(i, m):
		u = 0
		g = 0
		if (pow(i, 2) + pow(j, 2)) % m == 0:
			u = q
			g = q
			if r >= i and i != 0:
				u += 1
			if r >= j and j != 0:
				g += 1
			if i == j:
				kol += u * g
			else:
				kol += u * g * 2
print(int(kol))


(n, m) = list(map(int, input().split()))
a = 0
for i in range(m):
	for j in range(m):
		if (i ** 2 + j ** 2) % m == 0:
			a += (1 + (n - m + i) // m if n >= m - i else 0) * (1 + (n - m + j) // m if n >= m - j else 0)
print(a)


(n, m) = map(int, input().split())
ans = 0
for div1 in range(m):
	for div2 in range(div1 + 1):
		if (div1 ** 2 + div2 ** 2) % m != 0:
			continue
		num1 = n // m
		if div1 != 0 and div1 <= n % m:
			num1 += 1
		num2 = n // m
		if div2 != 0 and div2 <= n % m:
			num2 += 1
		ans += num1 * num2 * (2 if div1 != div2 else 1)
print(ans)


(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)


(n, m) = map(int, input().split())
d = list(map(lambda x: 0, range(m)))
r = 0
for i in range(0, m):
	d1 = i
	d2 = i * i % m
	add = n // m
	if n % m >= d1 > 0:
		add += 1
	d[d2] += add
for i in range(m):
	d1 = i
	d2 = (m - d1) % m
	r += d[d1] * d[d2]
print(r)


(n, m) = [int(x) for x in input().split()]
A = []
for i in range(1, m + 1):
	for j in range(1, m + 1):
		if (i ** 2 + j ** 2) % m == 0:
			A.append([i, j])
ans = 0
for (i, j) in A:
	c1 = (n - i) // m + 1
	c2 = (n - j) // m + 1
	ans = ans + c1 * c2
print(ans)


(n, m) = [int(x) for x in input().split()]
r = n % m
Sol = []
for i in range(0, m):
	for j in range(0, m):
		if (i * i + j * j) % m == 0:
			Sol.append((i, j))
ans = 0
for pair in Sol:
	if pair[0] == 0:
		x = int(n / m)
	elif pair[0] <= n:
		x = int((n - pair[0]) / m) + 1
	else:
		x = 0
	if pair[1] == 0:
		y = int(n / m)
	elif pair[1] <= n:
		y = int((n - pair[1]) / m) + 1
	else:
		y = 0
	ans += x * y
print(ans)


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) = [int(i) for i in input().split()]
a = [(n - i) // m + 1 for i in range(0, m + 1)]
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)


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) = [int(k) for k in input().split()]
m_matrix = []
for i in range(m):
	m_matrix.append([])
	for j in range(m):
		m_matrix[i].append(0)

def count(matrix, r_limit, d_limit):
	result = 0
	for i in range(r_limit):
		for j in range(d_limit):
			if matrix[i][j] == 0:
				result += 1
	return result
for i in range(m):
	for j in range(m):
		m_matrix[i][j] = ((i + 1) ** 2 + (j + 1) ** 2) % m
result = 0
count_repetitions = n // m
result += count_repetitions ** 2 * count(m_matrix, m, m)
delta = n % m
if delta > 0:
	result += count_repetitions * 2 * count(m_matrix, delta, m)
	result += count(m_matrix, delta, delta)
print(result)


def helper(n, m, x):
	(ans, mod) = divmod(n, m)
	if x == 0:
		return ans
	if mod < x:
		return ans
	if mod >= x:
		return ans + 1

def solve(n, m):
	res = 0
	for x in range(m):
		for y in range(m):
			if (x ** 2 + y ** 2) % m == 0:
				res += helper(n, m, x) * helper(n, m, y)
	return res
(n, m) = map(int, input().split())
print(solve(n, m))


from math import *
(n, m) = map(int, input().split())
rem = [(n - i) // m + 1 for i in range(1, m + 1)]
rempow = [(i % m) ** 2 % m for i in range(1, m + 1)]
rem2 = [0] * m
for i in range(1, m + 1):
	rem2[rempow[i % m]] += rem[i % m]
rem = rem2
print(sum([rem[i % m] * rem[(m - i % m) % m] for i in range(m)]))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import factorial
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')

def main():
	(n, m) = RL()
	res = 0
	t = n // m
	for i in range(1, m + 1):
		for j in range(1, m + 1):
			if (i ** 2 + j ** 2) % m == 0:
				res += ((n - i) // m + 1) * ((n - j) // m + 1)
	print(res)
main()


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


(n, m) = map(int, input().split())
setsCount = 0
(cociente, resto) = divmod(n, m)
for i in range(1, m + 1):
	for j in range(1, i + 1):
		if (i ** 2 + j ** 2) % m == 0:
			filaCount = cociente
			colCount = cociente
			if j <= resto:
				colCount += 1
			if i <= resto:
				filaCount += 1
			actualCount = filaCount * colCount
			if i != j:
				actualCount *= 2
			setsCount += actualCount
print(setsCount)


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
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) = 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:
			res += f(i) * f(j) * (2 if i != j else 1)
print(res)


(n, m) = list(map(int, input().split()))
mod = []
for i in range(1, m + 1):
	mod.append(i * i % m)
num = [n // m for _ in range(m)]
for i in range(n % m):
	num[i] += 1
ans = 0
for i in range(m):
	for j in range(m):
		if (mod[i] + mod[j]) % m == 0:
			ans += num[i] * num[j]
print(ans)


import collections
import functools
import math
import random
import sys
import bisect

def In():
	return map(int, sys.stdin.readline().split())
input = sys.stdin.readline

def divcandy():
	(n, m) = In()
	cl = [0] * m
	left = n % m
	l = [0] * m
	for i in range(1, m + 1):
		res = i * i % m
		cl[res] += 1
		if i <= left:
			l[res] += 1
	times = n // m
	ans = 0
	for i in range(m):
		cl[i] = cl[i] * times + l[i]
	for i in range(m):
		ans += cl[i] * cl[(m - i) % m]
	return ans
print(divcandy())


import collections, atexit, math, sys, bisect
sys.setrecursionlimit(1000000)

def getIntList():
	return list(map(int, input().split()))
try:
	import numpy

	def dprint(*args, **kwargs):
		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())
(N, M) = getIntList()
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)