#include <bits/stdc++.h>
using namespace std;
int n, k, h, l;
long long d[1001][1001], a[1001], b[1001];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= k; i++) a[i] = (i * i) % k, b[i] = (k - a[i]) % k;
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++)
      d[i][j] = d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1] + (a[i] == b[j]);
  h = n / k;
  l = n % k;
  printf("%lld", d[k][k] * h * h + 2 * h * d[l][k] + d[l][l]);
  return 0;
}
