#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;

int gcd(int a, int b);

int main(void){
        int i,j,k;
        int n,sum;

   // printf("%d\n",gcd(1,2));
        sum=0;
        scanf("%d",&n);
        for(i=1;i<=n;i++){
                for(j=1;j<=n;j++){
                        for(k=1;k<=n;k++){
                                sum += gcd(gcd(i,j),k);
                        }
                }
        }
        printf("%d\n", sum);
        return 0;
}

int gcd(int a, int b){
        while(a!=0 && b!=0){
                if(a<b){
                        b = b%a;
                } else {
                        a = a%b;
                }
        }
        return (a+b);
}
