#include <bits/stdc++.h>
using namespace std;
bool mat[1001][1001];
long long row[1001];
long long n, m;
long long solveA() {
  long long ret = 0;
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= n; j++)
      if ((i * i + j * j) % m == 0) ret++;
  return ret;
}
long long getH(long long r, long long c) {
  long long ret = 0;
  for (long long i = 1; i <= c; i++)
    if ((r * r + i * i) % m == 0) ret++;
  return ret;
}
void calc(long long r) { row[r] = getH(r, m) * (n / m) + getH(r, n % m); }
long long getV(long long r) {
  long long ret = 0;
  for (long long i = 1; i <= r; i++) ret += row[i];
  return ret;
}
long long solveB() {
  for (long long i = 1; i <= m; i++) calc(i);
  long long ret = getV(m) * (n / m) + getV(n % m);
  return ret;
}
int main() {
  cout << setprecision(10);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  if (n > m)
    cout << solveB() << '\n';
  else
    cout << solveA() << '\n';
  return 0;
}
