#include <bits/stdc++.h>
using namespace std;
const unsigned long long mn = 2000;
unsigned long long used[100001];
signed main() {
  unsigned long long n, m;
  cin >> n >> m;
  unsigned long long cnt = 0;
  unsigned long long col = n / m;
  unsigned long long mod = n % m;
  for (unsigned long long i = 0; i < m; i++) {
    used[i] = col;
  }
  for (unsigned long long i = 0; i <= mod; i++) {
    used[i]++;
  }
  used[0]--;
  for (unsigned long long i = 0; i < m; i++) {
    for (unsigned long long j = i; j < m; j++) {
      if ((i * i + j * j) % m == 0) {
        if (i != j) {
          cnt = cnt + used[i] * used[j] * 2;
        } else {
          cnt += used[i] * used[j];
        }
      }
    }
  }
  cout << cnt << endl;
  return 0;
}
