#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  long long result = 0;
  cin >> n >> m;
  map<long long, long long> mapa;
  set<long long> s;
  map<long long, long long>::iterator it1;
  if (n >= m) {
    long long resto = n % m;
    long long c = n / m;
    for (long long i = 1; i <= m; i++) {
      long long aux = (i * i) % m;
      mapa[aux]++;
      s.insert(aux);
    }
    for (it1 = mapa.begin(); it1 != mapa.end(); it1++) {
      it1->second = it1->second * c;
    }
    for (long long i = n; i > n - resto; i--) {
      long long aux1 = i % m;
      long long aux2 = (aux1 * aux1) % m;
      mapa[aux2]++;
      s.insert(aux2);
    }
  } else {
    for (long long i = 1; i <= n; i++) {
      long long aux = (i * i) % m;
      mapa[aux]++;
      s.insert(aux);
    }
  }
  set<long long>::iterator it;
  for (it = s.begin(); it != s.end(); it++) {
    if (*it == 0) result = result + (mapa[*it]) * (mapa[*it]);
    if (binary_search(s.begin(), s.end(), (m - (*it))))
      result = result + mapa[*it] * mapa[m - *it];
  }
  cout << result;
}
