#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, f = 1;
  char ch;
  while (!isdigit(ch = getchar()))
    if (ch == '-') f = -1;
  x = ch - '0';
  while (isdigit(ch = getchar())) x = x * 10 + ch - '0';
  return x * f;
}
long long n, m;
long long ans;
long long cnt[1007];
int main() {
  n = read();
  m = read();
  for (int i = 0; i < m; i++) cnt[i * i % m] += (n + i) / m;
  for (int i = 0; i < m; i++) ans += cnt[i] * cnt[(m - i) % m];
  cout << ans;
  return 0;
}
