#include <bits/stdc++.h>
using namespace std;
int cnt[1005];
int main() {
  int n, m, k;
  long long ans = 0;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    k = (i * i) % m;
    cnt[k] += (n - i + 1) / m;
    if ((n - i + 1) % m && i <= n) cnt[k]++;
  }
  cnt[m] = cnt[0];
  for (int i = 0; i < m; i++) {
    ans += (long long)cnt[i] * (long long)cnt[m - i];
  }
  printf("%lld\n", ans);
  return 0;
}
