#include <bits/stdc++.h>
using namespace std;
long long modexp(long long b, long long p) {
  b = b % 1000000007;
  if (p == 0) return 1;
  long long a = modexp((b * b) % 1000000007, p / 2);
  if (p % 2)
    return (a * b) % 1000000007;
  else
    return a % 1000000007;
}
inline long long isint(long double d) {
  return (long long)d == (long long)ceil(d);
}
void solve() {
  long long n, m, ans = 0;
  cin >> n >> m;
  vector<long long> c(m + 1, 0);
  for (long long i = 1; i < m + 1; i++) {
    c[(i * i) % m] += (n / m);
  }
  for (long long i = (n / m) * m + 1; i < n + 1; i++) {
    c[(i * i) % m]++;
  }
  for (long long i = 0; i < m; i++) {
    for (long long j = i; j < m; j++) {
      if ((i + j) % m == 0) {
        if (i == j)
          ans += (c[i] * c[j]);
        else
          ans += (2 * c[i] * c[j]);
      }
    }
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long TESTS = 1;
  while (TESTS--) {
    solve();
  }
  return 0;
}
