#include <bits/stdc++.h>
using namespace std;
const long long SuperINF = 2 * 1e18;
const long long MAX_SIZE = 2 * 1e5, INF = 2 * 1e9, MOD = 1e9 + 7;
const double eps = 1e-6;
void files() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
long long n, m;
long long answer = 0;
long long cnt(long long ost, long long p) {
  long long l = 0, r = n;
  long long midle;
  while (r - l > 1) {
    midle = (l + r) / 2;
    if (midle * p + ost > n)
      r = midle;
    else
      l = midle;
  }
  long long j = 0;
  if (ost <= n && ost != 0) {
    j = 1;
  }
  if ((r * p + ost > n))
    return l + j;
  else
    return r + j;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  long long p;
  if (n == m)
    p = n;
  else if (n > m)
    p = m;
  else if (n < m)
    p = n + 1;
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      long long one = (i * i) % m;
      long long two = (j * j) % m;
      if ((one + two) % m == 0) {
        long long cnt_1 = cnt(i, m);
        long long cnt_2 = cnt(j, m);
        answer += cnt_1 * cnt_2;
      }
    }
  }
  cout << answer;
  return 0;
}
