#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int n, m, cnt, rem, frq[N];
long long ans;
int main() {
  cin >> n >> m;
  cnt = frq[0] = frq[m] = n / m;
  rem = n % m;
  for (int i = 1; i < m; ++i) {
    frq[i] = cnt + (i <= rem);
  }
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= m; ++j) {
      if ((i * i + j * j) % m == 0) {
        ans += frq[i] * 1LL * frq[j];
      }
    }
  }
  cout << ans << endl;
  return 0;
}
