#include <bits/stdc++.h>
using namespace std;
long long n, i, j, res, x, y, cmp, m;
void solve() {
  cin >> n >> m;
  cmp = 2 * n * n;
  for (i = 1; i <= min(n, m); i++) {
    for (j = 1; j <= min(n, m); j++) {
      x = i * i;
      y = j * j;
      if ((x + y) % m == 0) {
        x = (n - i) / m + 1;
        y = (n - j) / m + 1;
        res += (x * y);
      }
    }
  }
  cout << res;
}
int main() { solve(); }
