#include <stdio.h>

int gcd(int, int);

int main(){
    int K;
    long sum = 0;
    int ans_1;
    scanf("%d", &K);

    for(int a = 1; a <= K; a++){
        for(int b = 1; b <= K; b++){
            ans_1 = gcd(a, b);

            for(int c = 1; c <= K; c++){
                sum += gcd( ans_1, c );
            }
        }
    }
    printf("%ld", sum);

    return 0;
}

int gcd(int a, int b){
    if (a % b == 0)
        return b;
    else
        return gcd(b, a % b);
}