#include<stdio.h>
int gcd(int a,int b){
    if(a%b == 0) return b;
    else return gcd(b,a%b);
    return 0;
}

int main(){
    int K,tmp,i,j,l;
    long ans = 0;
    scanf("%d",&K);
    for(i = 1; i <= K; i++){
        for(j = 1; j <= K; j++){
            for(l = 1; l <= K; l++){
                tmp = gcd(i,j);
                tmp = gcd(tmp,l);
//                printf("%d %d %d gcd is %d\n",i,j,l,tmp);
                ans += tmp;
                tmp = 0;
            }
        }
    }
    printf("%ld\n",ans);
    return 0;
}
