#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  vector<long long> cnt(m, 0);
  long long d = n % m;
  for (long long i = 0; i < m; i++) {
    cnt[i * i % m] += n / m;
    if (i && i <= d) {
      cnt[i * i % m]++;
    }
  }
  long long res = 0;
  for (long long i = 0; i < m; i++) {
    res += cnt[i] * cnt[(m - i) % m];
  }
  cout << res;
}
