#include <bits/stdc++.h>
using namespace std;
const int MAX = 500000 + 5;
long long n, k, sol;
long long f() {
  long long ret = 0;
  for (int i = (0) + 1, _for = (k) + 1; i < _for; ++i)
    for (int j = (0) + 1, _for = (k) + 1; j < _for; ++j)
      if ((i * i + j * j) % k == 0) ret++;
  return ret;
}
long long g(long long a) {
  long long ret = 0;
  for (int i = (0) + 1, _for = (a) + 1; i < _for; ++i)
    for (int j = (0) + 1, _for = (k) + 1; j < _for; ++j)
      if ((i * i + j * j) % k == 0) ret++;
  return ret;
}
long long h(long long a, long long b) {
  long long ret = 0;
  for (int i = (0) + 1, _for = (a) + 1; i < _for; ++i)
    for (int j = (0) + 1, _for = (b) + 1; j < _for; ++j)
      if ((i * i + j * j) % k == 0) ret++;
  return ret;
}
int main() {
  scanf("%lld%lld", &n, &k);
  long long t = n / k;
  if (n >= k) {
    sol += f() * t * t;
    n %= k;
    sol += g(n) * t * 2;
    sol += h(n, n);
    printf("%lld\n", sol);
    return 0;
  }
  sol += h(n, n);
  printf("%lld\n", sol);
  return 0;
}
