#include <stdio.h>
#include <stdlib.h>

int gcd(int a, int b, int c){
    int tmp, f;
    if(a < b){
        tmp = a;
        a = b;
        b = tmp;
    }
    f = a % b;
    while(f != 0){
        a = b;
        b = f;
        f = a % b;
    }

    if (b < c)
    {
        tmp = b;
        b = c;
        c = tmp;
    }
    f = b % c;
    while (f != 0)
    {
        b = c;
        c = f;
        f = b % c;
    }
    return c;
}

int main(){
    int K,a,b,c;
    unsigned long N;

    scanf("%d", &K);

    N = 0;
    for (a = 1; a <= K; a++){
        for (b = 1; b <= K; b++){
            for (c = 1; c <= K;c++){
                N += gcd(a, b, c);
            }
        }
    }

    printf("%ld", N);

    return 0;
}
