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