#include <bits/stdc++.h>
using namespace std;
int n, m;
long long cnt[1231], Ans;
int main(void) {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= min(n, m); i++) {
    cnt[i * i % m] += (n - i) / m + 1;
  }
  for (int i = 0; i < m; i++) {
    Ans += cnt[i] * cnt[(m - i) % m];
  }
  cout << Ans << endl;
}
