#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : ", ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T>
ostream &operator<<(ostream &o, tuple<T...> const &t) {
  o << "(";
  _ot<0>(o, t);
  o << ")";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, pair<T, U> const &p) {
  o << "(" << p.first << ", " << p.second << ")";
  return o;
}
template <class T>
ostream &operator<<(ostream &o, const stack<T> &a) {
  o << "{";
  for (auto tmp = a; tmp.size(); tmp.pop())
    o << (a.size() == tmp.size() ? "" : ", ") << tmp.top();
  o << "}";
  return o;
}
template <class T, class Container, class Compare>
ostream &operator<<(ostream &os, priority_queue<T, Container, Compare> a) {
  os << "{ (top) ";
  while (a.size()) os << a.top() << (a.size() == 1 ? "" : ", "), a.pop();
  os << " }";
  return os;
}
template <class T, class Container>
ostream &operator<<(ostream &os, queue<T, Container> a) {
  os << "{ ";
  while (a.size()) os << a.front() << (a.size() == 1 ? "" : ", "), a.pop();
  os << " }";
  return os;
}
template <class T,
          class = typename iterator_traits<decltype(begin(T()))>::value_type,
          class = typename enable_if<!is_same<T, string>::value>::type>
ostream &operator<<(ostream &o, const T &a) {
  for (auto ite = begin(a); ite != end(a); ++ite)
    o << (ite == begin(a) ? "" : " ") << *ite;
  return o;
}
int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  int n, m;
  cin >> n >> m;
  ll ans = 0;
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= m; j++) {
      if ((i * i + j * j) % m == 0) {
        (42);
        ans += (ll)(n / m) * (n / m);
        if (i <= n % m) {
          ans += (ll)(n / m);
        }
        if (j <= n % m) {
          ans += (ll)(n / m);
        }
        if (i <= n % m && j <= n % m) {
          ans += 1;
        }
      }
    }
  cout << ans << endl;
  return 0;
}
