#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long p1 = 998244353;
long long power(long long x, long long y) {
  long long ans = 1;
  x = x % p1;
  while (y > 0) {
    if (y & 1) ans = (ans * x) % p1;
    y = y >> 1;
    x = (x * x) % p1;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long n, m;
  cin >> n >> m;
  if (n <= m) {
    long long ans = 0;
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        if ((i * i + j * j) % m == 0) ans++;
      }
    }
    cout << ans;
    return 0;
  }
  long long ans = 0, sum = 0;
  long long op[m];
  for (long long i = 1; i <= m; i++) {
    sum = 0;
    for (long long j = 1; j <= m; j++) {
      long long vl = (i * i) % m + (j * j) % m;
      if (vl % m == 0) {
        ans++;
        sum++;
      }
    }
    op[i % m] = sum;
  }
  ans = ans * (n / m) * (n / m);
  if (n % m == 0) {
    cout << ans;
    return 0;
  }
  long long rem = n % m;
  long long add = 0, sub = 0;
  for (long long i = 1; i <= rem; i++) {
    add += (n / m) * 2 * op[i];
  }
  for (long long i = 1; i <= rem; i++) {
    for (long long j = 1; j <= rem; j++) {
      if ((i * i + j * j) % m == 0) sub++;
    }
  }
  cout << ans + add + sub;
  return 0;
}
