#include <bits/stdc++.h>
using namespace std;
std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
long long count_mod0(long long n, long long m, long long b) {
  assert(m >= 0);
  if (b < 0 || b >= m || b > n)
    return 0;
  else
    return (n - b) / m + 1;
}
long long count_mod(long long i, long long j, long long m, long long b) {
  return count_mod0(j, m, b) - count_mod0(i - 1, m, b);
}
long long count_mod_slow(long long i, long long j, long long m, long long mod) {
  long long ans = 0;
  for (long long p = i; p <= j; ++p) {
    if (p % m == mod) ++ans;
  }
  return ans;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  vector<long long> dp(m + 1);
  for (long long i = 0; i < m; ++i) {
    long long mod = (i * i) % m;
    dp[mod] += count_mod(1, n, m, i);
  }
  long long ans = dp[0] * dp[0];
  for (int i = 1; i < m; ++i) {
    ans += dp[i] * dp[m - i];
  }
  cout << ans << endl;
}
