#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  long long ans = 0;
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= m; ++j) {
      int ci = (n / m) + (i <= n % m);
      int cj = (n / m) + (j <= n % m);
      if ((i * i + j * j) % m == 0) {
        ans += 1ll * ci * cj;
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
