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