#include <bits/stdc++.h>
int main() {
  const uint64_t NO_VAL = 0xFFFFFFFF;
  uint64_t num = 0, mod = 0;
  scanf("%llu %llu", &num, &mod);
  auto dp_ans = std::vector<uint64_t>(mod * mod);
  std::fill(dp_ans.begin(), dp_ans.end(), 0);
  for (uint64_t x = 1; x <= mod; ++x) {
    for (uint64_t y = 1; y <= mod; ++y) {
      if (x >= 2)
        dp_ans[(x - 1) + mod * (y - 1)] += dp_ans[(x - 2) + mod * (y - 1)];
      if (y >= 2)
        dp_ans[(x - 1) + mod * (y - 1)] += dp_ans[(x - 1) + mod * (y - 2)];
      if (x >= 2 && y >= 2)
        dp_ans[(x - 1) + mod * (y - 1)] -= dp_ans[(x - 2) + mod * (y - 2)];
      if ((x * x + y * y) % mod == 0) ++dp_ans[(x - 1) + mod * (y - 1)];
    }
  }
  uint64_t ans = 0;
  ans += (num / mod) * (num / mod) * dp_ans[(mod - 1) + mod * (mod - 1)];
  if (num % mod) {
    ans += (num / mod) * dp_ans[(num % mod - 1) + mod * (mod - 1)];
    ans += (num / mod) * dp_ans[(mod - 1) + mod * (num % mod - 1)];
    ans += dp_ans[(num % mod - 1) + mod * (num % mod - 1)];
  }
  printf("%llu", ans);
  return 0;
}
