#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const int N = (int)1e6 + 7, mod = 1000000007, M = 2e9;
long long n, m, ans, c[N], t;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= min(n, m); i++) {
    t = (n / m) + (n % m >= i);
    c[(i * i) % m] += t;
  }
  ans = c[0] * c[0];
  for (int i = 1; i / 2 <= m; i++) ans += c[i] * c[m - i];
  cout << ans;
  return 0;
}
