#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int gcd(int,int,int);

int main()
{
int n,i,j,k,a=0;
scanf("%d",&n);

for(i=1;i<=n;i++)
{
    for(j=1;j<=n;j++)
    {
        for(k=1;k<=n;k++)
        {
            a += gcd(i,j,k);
        }
    }
}

printf("%d",a);
return 0;
}

int gcd(int a,int b,int c)
{
    int x,y,z,tmp;
    if(a<b)
    {
        tmp = a;
        a = b;
        b = tmp;
    }
    int gcd=0;
    x = a%b;
    while(x!=0)
    {
        a = b;
        b = x;
        x = a%b;
    }
    gcd = b;
if(c<gcd)
    {
        tmp = c;
        c = gcd;
        gcd = tmp;
    }
    x = c%gcd;
    while(x!=0)
    {
        c = gcd;
        gcd = x;
        x = c%gcd;
    }
return gcd;
}
