#include <bits/stdc++.h>
using namespace std;
long long f[1005];
int main() {
  long long n, m;
  scanf("%lld%lld", &n, &m);
  for (long long i = 1; i <= min(n, m); ++i) {
    long long cate = (n - i) / m + 1;
    f[(i * i) % m] += cate;
  }
  long long ans = 0;
  for (long long i = 0; i < m; ++i) {
    long long x = (m - i) % m;
    ans += 1LL * f[i] * f[x];
  }
  printf("%lld\n", ans);
  return 0;
}
