#include <stdio.h>  /*入出力*/
#include <stdlib.h> /*標準ライブラリ*/
#include <string.h> /*文字列*/
#include <math.h>   /*計算*/
#include <assert.h> /*確認*/
#include <ctype.h>  /*文字操作*/
#include <stdbool.h>/*bool型*/
#include <limits.h>/*限界*/
//---------------------------------------------------
#define rep(i, n) for(int  i = 0; i < (n); i++)
#define max(a,b) ((a>b)?a:b)
#define ABS(a) (((a)>(0))?a:-(a))
#define LL long long
#define MOD 1000000007//10^9+7(素数)
//---------------------------------------------------
void swap(LL *a, LL *b)
{
  LL temp;
  temp = *a;*a = *b;*b = temp;
}

//abc c
long long gcd (LL a, LL b);
int main(void)
{
  int n;
  long long ans = 0;
  scanf("%d", &n);
  for (LL i = 1; i <= n; i++) {
    for (LL j =1; j <= n; j++) {
      for (LL k = 1; k <= n; k++) {
          ans += gcd(gcd(i,j),k);
      }
    }
  }

  printf("%lld\n" , ans);
}

LL gcd(LL a, LL b)
{
  if (a < b) {
    swap(&a,&b);
  }
  if (b == 0) {
    return a;
  }
  return gcd(b,a%b);
}
