#include <bits/stdc++.h>
using namespace std;
const long long Inf = 9223372036854775807ll;
const int inf = 2147483647;
int n, m;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  long long ans = 0;
  for (int i = 1; i <= min(n, m); ++i)
    for (int j = 1; j <= min(n, m); ++j)
      if ((i * i + j * j) % m == 0) {
        int mult1 = (n - i) / m, mult2 = (n - j) / m;
        ans += 1ll * (mult1 + 1) * (mult2 + 1);
      }
  cout << ans;
  return 0;
}
