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