#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, unsigned long long int y) {
  long long int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x;
    }
    y = y >> 1;
    x = x * x;
  }
  return res;
}
long long int powermod(long long int x, unsigned long long int y,
                       long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int32_t main() {
  long long int n, m;
  cin >> n >> m;
  long long int ans = 0;
  for (long long int i = (long long int)(1); i < (long long int)(m + 1); i++) {
    for (long long int j = (long long int)(1); j < (long long int)(m + 1);
         j++) {
      if (((i * i) + (j * j)) % m == 0) {
        long long int x = n / m;
        long long int y = n / m;
        if (n % m >= i) x++;
        if (n % m >= j) y++;
        ans += x * y;
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
