#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long N = 1e5 + 5;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, m;
  cin >> n >> m;
  if (m == 1) {
    cout << n * n << "\n";
    return 0;
  }
  vector<long long> cnt(m + 1, 0);
  for (long long i = 1; i <= m; i++) {
    long long r = (i * i) % m;
    cnt[r]++;
  }
  for (long long r = 0; r < m; r++) cnt[r] *= (n / m);
  for (long long i = (n / m) * m + 1; i <= n; i++) {
    long long r = (i * i) % m;
    cnt[r]++;
  }
  long long ans = 0;
  for (long long r = 0; r <= m; r++) {
    ans += cnt[r] * cnt[(m - r) % m];
  }
  cout << ans << "\n";
  return 0;
}
