#include <bits/stdc++.h>
using namespace std;
long long s1[1001], s2[1001];
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    s1[i] = n / m;
  }
  int remainder = n % m;
  for (int i = 0; i < remainder; ++i) {
    ++s1[(i + 1) % m];
  }
  for (int i = 0; i < m; ++i) {
    s2[(i * i) % m] += s1[i];
  }
  long long ans = 0;
  for (int first = 0; first < m; ++first) {
    int second = (m - first) % m;
    ans += s2[first] * s2[second];
  }
  cout << ans;
}
