#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, m;
  cin >> n >> m;
  vector<long long> cnt(m, (n / m)), rm(m, 0);
  while (n % m != 0) {
    cnt[n % m]++;
    n--;
  }
  for (int i = 1; i < m; i++) rm[i] = (i * i) % m;
  vector<long long> fcnt(m, 0);
  for (int i = 0; i < m; i++) {
    fcnt[rm[i]] += cnt[i];
  }
  long long ans = fcnt[0] * fcnt[0];
  for (int i = 1; i < (m + 1) / 2; i++) {
    ans += (2 * fcnt[i] * fcnt[m - i]);
  }
  if (m % 2 == 0) ans += (fcnt[m / 2] * fcnt[m / 2]);
  cout << ans << endl;
}
void sj26() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int main() {
  bool q = 0;
  sj26();
  int t = 1;
  if (q) cin >> t;
  while (t--) {
    solve();
  }
}
