#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  long long x;
  cin >> n >> m;
  if (m == 1) {
    cout << n * n << "\n";
    return 0;
  }
  vector<long long> a(m);
  long long chast = n / m;
  long long ost = n % m;
  for (long long i = 1; i <= m; i++) {
    x = (i * i) % m;
    a[x] += chast;
  }
  for (int i = 1; i <= ost; i++) {
    x = (i * i) % m;
    a[x]++;
  }
  long long ans = 0;
  ans = a[0] * a[0];
  if (m == 2) {
    ans += a[1] * a[1];
    cout << ans << "\n";
    return 0;
  }
  for (long i = 1; i < m; i++) {
    if (i == m / 2 && m % 2 == 0) {
      ans += a[i] * a[m - i];
      break;
    }
    ans += a[i] * a[m - i] * 2;
    if (i == m / 2) break;
  }
  cout << ans << "\n";
  return 0;
}
