#include <bits/stdc++.h>
using namespace std;
const int N = 4e5;
int n, m;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  long long ans = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= m; j++) {
      if ((i * i + j * j) % m != 0) continue;
      if (i > n || j > n) continue;
      int cnt1 = (n - i) / m + 1;
      int cnt2 = (n - j) / m + 1;
      ans += 1ll * cnt1 * cnt2;
    }
  }
  cout << ans << "\n";
  return 0;
}
