#include <stdio.h>

int gcd3(int a, int b, int c);
int gcd2(int a, int b);
void swap(int *a, int *b);

int main(){
	int k;
	if(scanf("%d",&k)==EOF){return 0;}
	int sum = 0;
	for(int a = 1; a<=k; a++){
		for(int b = 1; b<=k; b++){
			for(int c = 1; c<=k; c++){
				sum += gcd3(a,b,c);
			}
		}
	}
	printf("%d\n", sum);
	return 0;

}


int gcd3(int a, int b, int c){
	int ab = gcd2(a,b);
	int bc =  gcd2(b,c);
	return gcd2(ab, bc);
}

int gcd2(int a,int b){
	if(a<b){swap(&a,&b);		}
	int c;
	c = a%b;
	if(c == 0){return b;}
	while(c!=0){
		swap(&a,&b);
		swap(&b,&c);
		//a=b;
		//b=c;
		c=a%b;
		if(c==1){break;}
	}
	if(c==1){return 1;}
	if(c==0){return b;}
	return 0;
}


void swap(int *a, int *b){
	int temp;
	temp =*a;
	*a = *b;
	*b = temp;
}