#include <stdio.h>

int gcd(int a,int b);
int gcds(int a,int b,int c);

int main(){

	int k,i,j,l;
	long int sum=0;
	
	scanf("%d",&k);

	for(i=1;i<=k;i++){
		for(j=1;j<=k;j++){
			for(l=1;l<=k;l++){
				sum+=gcds(i,j,l);
			}
		}
	}
	printf("%ld\n",sum);
	return 0;
}

int gcds(int a,int b,int c){
	int gcd_ab,gcd_abc;
	gcd_ab=gcd(a,b);
	gcd_abc=gcd(gcd_ab,c);
	return gcd_abc;
}

int gcd(int a,int b){
	int tmp,r;
	if(a<b){
   	 tmp = a;
   	 a = b;
   	 b = tmp;
  }

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

return b;
}