#include <stdio.h>

int gcd(int a, int b, int c);

int main(void)
{
    int K,a,b,c;
    long long sum=0;

    scanf("%d",&K);

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

    printf("%lld\n",sum);

    return 0;
}

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

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

    return b;
}