#include<stdio.h>

void Swap(int *a, int *b){
    int tmp;
    tmp = *a;
    *a = *b;
    *b = tmp;
}

int Gcd(int a, int b, int c){
    int r, s;
    if(a < b){
        Swap(&a, &b);
    }
    while(b != 0){
        r = a % b;
        a = b;
        b = r;
    }
    if(a < c){
        Swap(&a, &c);
    }
    while(c != 0){
        s = a % c;
        a = c;
        c = s;
    }
    return a;
}

int main(void){
    int K,Sum = 0;

    scanf("%d",&K);
    for(int i = 1; i <= K; i++){
        for(int j = 1; j <= K; j++){
            for(int k = 1; k <= K; k++){
                Sum += Gcd(i, j, k);                    
            }
        }
    }
    printf("%d\n",Sum);
    return 0;
}