#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, m;
  cin >> n >> m;
  long long ans_m = 0, res = 0, ans_u = 0, ans_l = 0, ans_ul = 0;
  long long k = n / m;
  long long l = n % m;
  for (long long i = 1; i <= m; i++) {
    for (long long j = 1; j <= m; j++) {
      if ((i * i + j * j) % m == 0) {
        if (j <= l) {
          ans_l++;
        }
        if (i <= l) {
          ans_u++;
        }
        if (i <= l && j <= l) {
          ans_ul++;
        }
        ans_m++;
      }
    }
  }
  if (n <= m) {
    long long ans_n = 0;
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        if ((i * i + j * j) % m == 0) {
          ans_n++;
        }
      }
    }
    cout << ans_n << "\n";
  } else {
    res = ans_m * k * k + (ans_l + ans_u) * k + ans_ul;
    cout << res << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}
