#include <stdio.h>
int gcd(int a,int b);
int main(void){
    int n;
    int a,b,c;
    int tmp1,tmp2;
    int ret = 0;
    scanf("%d",&n);
    for ( a = 1 ; a <= n ; a++){
        for ( b = 1 ; b <= n ; b++){
            for ( c = 1 ; c <= n ; c++){
                tmp1 = gcd(a,b);
                tmp2 = gcd(c,tmp1);
                ret = ret + tmp2;
            }
        }
    }

    printf("%d\n",ret);
}
int gcd(int a,int b){
    int c;
    int tmp;
    if(b > a){  //bがaより大きい場合bとaを入れ替える
        c = a;
        a = b;
        b = c;
    }
    tmp = a % b;
    while(tmp!=0){
        a = b;
        b = tmp;
        tmp = a % b;
    }
    return b;
}
