#include <bits/stdc++.h>
using namespace std;
long long a[345678], b[456111], c, n, i, l, s = 0, k = 0, m, j, t, x, y;
string p, q, r, rr;
int main() {
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    a[i] = n / m;
  }
  for (i = 1; i <= n % m; i++) a[i]++;
  for (i = 0; i < m; i++) {
    for (j = 0; j < m; j++) {
      if ((i * i + j * j) % m == 0) {
        s += a[i] * a[j];
      }
    }
  }
  cout << s << endl;
}
