#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  long long sum = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= m; j++) {
      if ((i * i + j * j) % m == 0) {
        long long commi = n / m;
        long long commj = commi;
        long long rem = n % m;
        if (i <= rem) commi++;
        if (j <= rem) commj++;
        sum += commi * commj;
      }
    }
  }
  cout << sum << endl;
}
