#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int main() {
  int n, m;
  cin >> n >> m;
  long long ans = 0;
  for (int i = 1; i <= m; i++) {
    long long cnt = 0;
    for (int j = 1; j <= m; j++) {
      if ((i * i + j * j) % m == 0) {
        cnt++;
      }
    }
    cnt = cnt * (n / m);
    for (long long j = (n / m) * m + 1; j <= n; j++) {
      if ((i * i + j * j) % m == 0) {
        cnt++;
      }
    }
    ans += cnt;
  }
  ans = ans * (n / m);
  for (long long i = (n / m) * m + 1; i <= n; i++) {
    long long cnt = 0;
    for (int j = 1; j <= m; j++) {
      if ((i * i + j * j) % m == 0) {
        cnt++;
      }
    }
    cnt = cnt * (n / m);
    for (long long j = (n / m) * m + 1; j <= n; j++) {
      if ((i * i + j * j) % m == 0) {
        cnt++;
      }
    }
    ans += cnt;
  }
  cout << ans;
}
