#include <stdio.h>
#include <math.h>

int gcd(int a, int b){
    if (a == b){
        return a;
    }
    else if (a > b){
        return gcd(a-b, b);
    }
    else{
        return gcd(a, b-a);
    }
}

int main(void) {
    long int K,i,j,k,m,sum = 0;
    scanf("%ld",&K);
     for (i=1;i<=K;++i){
       for (j=1;j<=K;++j){
       for (k=1;k<=K;++k){
    
    m = gcd(gcd(i,j),k);
    sum = sum + m;

    }
    }
    }
    printf("%ld\n",sum);
 
  
  return 0;
}