#include <bits/stdc++.h>
using namespace std;
const int maxp = 22;
const long double EPS = 1e-18;
const long long INF = 2e18;
const int MOD = 1e9 + 7;
const int N = 3e5 + 1;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  int i, j;
  long long ans = 0;
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= m; j++) {
      int tot = i * i + j * j;
      if (tot % m) continue;
      long long p1 = n / m, p2 = n / m;
      if (n % m >= i) p1++;
      if (n % m >= j) p2++;
      ans += p1 * p2;
    }
  }
  cout << ans << '\n';
}
