#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  long long TESTS = 1;
  while (TESTS--) {
    long long n, m;
    cin >> n >> m;
    long long p = n / m, sum = 0;
    for (long long i = 1; i < m + 1; i++) {
      for (long long j = 1; j < m + 1; j++)
        if ((i * i + j * j) % m == 0) sum++;
    }
    long long total = sum * p * p;
    sum = 0;
    for (long long i = (n / m) * m + 1; i <= n; i++) {
      for (long long j = 1; j <= m; j++)
        if ((i * i + j * j) % m == 0) sum++;
    }
    total += sum * (n / m) * 2;
    for (long long i = (n / m) * m + 1; i <= n; i++) {
      for (long long j = (n / m) * m + 1; j <= n; j++) {
        if ((i * i + j * j) % m == 0) total++;
      }
    }
    cout << total;
  }
}
