#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int arr[1000], m, n;
  long long int x, p, i, l, s, y, j, k;
  cin >> n >> m;
  if ((n > m) || (n == m)) {
    for (i = 0; i < m; i++) {
      arr[i] = 0;
    }
    for (i = 1; i <= m; i++) {
      p = (i * i) % m;
      arr[p]++;
    }
    l = (n / m);
    for (i = 0; i < m; i++) {
      arr[i] = (l)*arr[i];
    }
    y = l * m;
    y++;
    while (y <= n) {
      i = (y * y) % m;
      arr[i]++;
      y++;
    }
    if ((m % 2) == 0) {
      x = (arr[0] * (arr[0] - 1)) + arr[0] + (arr[m / 2] * (arr[m / 2] - 1)) +
          arr[m / 2];
      p = 0;
      for (i = 1; i < m / 2; i++) {
        p = p + (arr[i] * 2 * arr[m - i]);
      }
      x = x + p;
    }
    if ((m % 2) != 0) {
      s = m / 2;
      x = (arr[0] * (arr[0] - 1)) + arr[0];
      p = 0;
      for (i = 1; i <= s; i++) {
        p = p + (arr[i] * 2 * arr[m - i]);
      }
      x = x + p;
    }
    cout << x;
  }
  if ((n < m) && ((2 * n * n) < m)) {
    cout << "0";
  }
  if ((n < m) && ((2 * n * n) >= m)) {
    p = (2 * n * n) / m;
    s = 0;
    for (i = 1; i <= p; i++) {
      l = i * m;
      k = l / 2;
      y = sqrt(k);
      for (j = 1; j <= y; j++) {
        x = sqrt(l - (j * j));
        if ((x * x + j * j == l) && (x == j) && (x <= n) && (j <= n)) s++;
        if ((x * x + j * j == l) && (x != j) && (x <= n) && (j <= n)) s = s + 2;
      }
    }
    cout << s;
  }
}
