#include <bits/stdc++.h>
using namespace std;
long long freq[10001];
int main() {
  int n, m;
  cin >> n >> m;
  int adaos = n / m;
  int rem = n % m;
  for (int i = 1; i <= m; ++i) {
    freq[(i * i) % m] += adaos;
    if (i <= rem) {
      freq[(i * i) % m] += 1;
    }
  }
  long long answer = 0;
  for (int i = 0; i < m; ++i) {
    answer += freq[i] * freq[(m - i) % m];
  }
  cout << answer;
  return 0;
}
