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