#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 1;
int MOD = 1e9 + 7;
const int INF = 1e9;
const long long LINF = 1e18;
void solve() {
  long long n;
  cin >> n;
  long long m;
  cin >> m;
  long long ans = 0;
  for (long long i = 1; i <= min(n, m); ++i) {
    for (long long j = 1; j <= min(n, m); ++j) {
      if ((i * i + j * j) % m == 0) {
        long long c = (1 + ((n - i) / m)) * ((1 + (n - j) / m));
        ans += c;
      }
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
}
