#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX(a, b) ((a) > (b) ? (a) : (b))

int gcd(int a, int b)
{
  int r, tmp;
 
  /* 自然数 a > b を確認・入替 */
  if (a < b) {
    tmp = a;
    a = b;
    b = tmp;
  }
 
  /* ユークリッドの互除法 */
  r = a % b;
  while(r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

int main(void)
{
long K, x, y, z, sum;

  scanf("%ld", &K);
  sum = 0;
  for (x = 1; x <= K; ++x) {
    for (y = 1; y <= K; ++y) {
      for (z = 1; z <= K; ++z) {
        sum += gcd(gcd(x, y), z);
      }
    }
  }
  printf("%d\n", sum);
    
  return 0;
}
