#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  int m;
  cin >> n >> m;
  long long cnt = 0;
  if (m == 1) {
    cout << n * n << endl;
    return 0;
  }
  if (m < n) {
    for (int i = 1; i <= m; i++)
      for (int j = 1; j <= m; j++)
        if ((i * i + j * j) % m == 0) cnt++;
    long long mul = n / m;
    mul = mul * mul;
    cnt *= mul;
    long long index = (n / m) * m;
    index++;
    if (index <= n) {
      long long cnt1 = 0;
      for (long long i = index; i <= n; i++)
        for (long long j = 1; j <= m; j++) {
          long long i1 = i % m;
          long long j1 = j % m;
          if ((i1 * i1 + j1 * j1) % m == 0) cnt1++;
        }
      mul = n / m;
      mul *= 2;
      cnt1 *= mul;
      cnt += cnt1;
      for (long long i = index; i <= n; i++)
        for (long long j = index; j <= n; j++) {
          long long i1 = i % m;
          long long j1 = j % m;
          if ((i1 * i1 + j1 * j1) % m == 0) cnt++;
        }
    }
  } else {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if ((i * i + j * j) % m == 0) cnt++;
  }
  cout << cnt << endl;
  return 0;
}
