#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
using namespace std;
long long const p = 239;
long long const MAXN = 1e5 + 5;
struct hasher {
  long long MOD;
  ;
  vector<long long> h, pw;
  string s;
  void precalc() {
    long long n = (long long)s.size();
    h.resize(MAXN);
    pw.resize(MAXN);
    pw[0] = 1;
    for (long long i = 1; i < MAXN; i++) {
      pw[i] = pw[i - 1] * p;
      pw[i] %= MOD;
      if (pw[i] < 0) pw[i] += MOD;
    }
    h[0] = s[0];
    for (long long i = 1; i < n; i++) {
      h[i] = h[i - 1] * p + s[i];
      h[i] %= MOD;
      if (h[i] < 0) h[i] += MOD;
    }
  }
  long long calc(long long l, long long r) {
    long long ret = h[r] - (l ? h[l - 1] * pw[r - l + 1] : 0);
    ret %= MOD;
    if (ret < 0) ret += MOD;
    return ret;
  }
  hasher(long long _MOD, string _s) {
    s = _s;
    MOD = _MOD;
    precalc();
  }
};
vector<long long> zfunc(string s) {
  long long n = s.size();
  vector<long long> z(s.size());
  for (long long i = 1, l = 0, r = 0; i < n; i++) {
    if (i <= r) {
      z[i] = min(z[i - l], r - i + 1);
    }
    while (i + z[i] < n && s[z[i]] == s[i + z[i]]) {
      z[i]++;
      r++;
      l = i;
    }
  }
  return z;
}
vector<long long> pref(string s) {
  long long n = s.size();
  vector<long long> pi(n);
  for (long long i = 1; i < n; i++) {
    long long j = pi[i - 1];
    while (j > 0 && s[i] != s[j]) {
      j = pi[j - 1];
    }
    if (s[i] == s[j]) j++;
    pi[i] = j;
  }
  return pi;
}
mt19937_64 gen(time(NULL));
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  long long cnt = 0;
  for (long long i = 1; i <= m; i++) {
    for (long long j = 1; j <= m; j++) {
      if ((i * i + j * j) % m == 0) cnt++;
    }
  }
  long long ans = 0;
  ans += (n / m) * (n / m) * cnt;
  long long nn = n % m;
  for (long long i = 1; i <= nn; i++) {
    for (long long j = 1; j <= nn; j++) {
      if ((i * i + j * j) % m == 0) ans++;
    }
  }
  long long cnt1 = 0;
  for (long long i = 1; i <= nn; i++) {
    for (long long j = 1; j <= m; j++) {
      if ((i * i + j * j) % m == 0) cnt1++;
    }
  }
  ans += cnt1 * (n / m) * 2;
  cout << ans;
  return 0;
}
