#include <bits/stdc++.h>
int n, m;
int calc(int n, int m, int rem) { return n / m + ((n % m >= rem) ? 1 : 0); }
int main() {
  scanf("%d%d", &n, &m);
  long long ans = 0;
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= m; j++)
      if ((i * i + j * j) % m == 0) {
        ans += 1LL * calc(n, m, i) * calc(n, m, j);
      }
  printf("%I64d\n", ans);
  return 0;
}
