#include <bits/stdc++.h>
using namespace std;
long long c[1009];
int main() {
  long long i, j, k, l, n, m, rem, cnt;
  cin >> n >> m;
  for (i = 1; i <= min(n, m); i++) {
    rem = ((i % m) * (i % m)) % m;
    cnt = (n - i) / m + 1;
    c[rem] += cnt;
  }
  long long ans = 0;
  for (i = 1; i < m; i++) {
    ans += (c[i] * c[m - i]);
  }
  ans += c[0] * c[0];
  cout << ans << endl;
  return 0;
}
