#include <iostream>

using namespace std;
using ll = long long;

ll gcd2(int a, int b) {
  int x = min(a, b);
  int y = max(a, b);
  while (y % x) {
    int z = x;
    x = y % x;
    y = z;
  }
  return x;
}

ll gcd(int a, int b, int c) {
  return gcd2(gcd2(a, b), c);
}

int main() {
  int n;
  cin >> n;
  ll vsum = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      for (int k = 1; k <= n; ++k)
        vsum += gcd(i, j, k);
  cout << vsum << endl;
}