#include <stdio.h>

int gcd(int a,int b);
int main(void)
{
        int a,b,c,k,s,i,j,l;

        scanf("%d",&k);

        s=0;
        for(i=1;i<=k;i++)
                for(j=1;j<=k;j++)
                        for(l=1;l<=k;l++)
                                s=s+gcd(i,gcd(j,l));

        printf("%d",s);
        return 0;
}
int gcd(int a,int b)
{
        int r,tmp;

        if(a<b){
                tmp=a;
                a=b;
                b=tmp;
        }

        while(r!=0){
                r=a%b;
                a=b;
                b=r;
        }

        return a;
}
