#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m;
  cin >> n >> m;
  long long a[m];
  long long d = n / m;
  for (int i = 0; i < m; i++) {
    a[i] = d;
  }
  long long r = n % m;
  for (int i = 1; i <= r; i++) {
    a[i]++;
  }
  long long c1[m], c2[m];
  memset(c1, 0, sizeof c1);
  memset(c2, 0, sizeof c2);
  for (int i = 0; i < m; i++) {
    long long t = (i * i) % m;
    c1[t] += a[i];
    c2[t] += a[i];
  }
  long long sum = c1[0] * c2[0];
  for (long long i = 1; i < m; i++) {
    sum += (c1[i] * c2[m - i]);
  }
  cout << sum << endl;
}
