#include <bits/stdc++.h>
class DivideCandies {
  std::vector<long long> squareMod;
  int n, m;
  void preProcess() {
    squareMod.resize(m);
    for (int i = 1; i <= m; ++i) {
      squareMod[(i * i) % m] += n / m + (n % m > (i - 1 + m) % m);
    }
  }
  long long calcDivideCandiesSquare() {
    long long ret = 0;
    for (int i = 0; i < m; ++i) {
      ret += squareMod[(m - i) % m] * squareMod[i];
    }
    return ret;
  }

 public:
  void run(std::istream &in, std::ostream &out) {
    in >> n >> m;
    preProcess();
    out << calcDivideCandiesSquare() << '\n';
  }
};
int main() {
  std::ios_base::sync_with_stdio(false);
  DivideCandies().run(std::cin, std::cout);
  return 0;
}
