#include <bits/stdc++.h>
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  std::cout.tie(0);
  std::cerr.tie(0);
  long long n, m;
  while (std::cin >> n >> m) {
    std::vector<long long> cnt1(m, n / m);
    for (long long i = n / m * m + 1; i <= n; ++i) {
      cnt1[i % m]++;
    }
    std::vector<long long> cnt2(m, 0);
    for (int i = 0; i < m; ++i) {
      cnt2[i * i % m] += cnt1[i];
    }
    long long answ = 0;
    for (int rem = 0; rem < m; ++rem) {
      answ += (long long)cnt2[rem] * cnt2[(m - rem) % m];
    }
    std::cout << answ << std::endl;
  }
  return 0;
}
