#include<stdio.h>

int gcd(int a, int b){
	int c = a;
	int d = b;
	int e = 0;
	/*if(a<0 || b<0){
		return -1;
	}*/
	while(d > 0){
		e = d;
		d = c%e;
		c = e;
	}
	return c;
}

int gcdThree(int a, int b, int c){
	int g;
	g = gcd(gcd(a,b), c);
	return g;
}

long long int gcdSum(int K){
	int i = 0, j, k;
	long long int g = 0;
	for(i = 1; i <= K; i++){
		for(j = 1; j <= K; j++){
			for(k = 1; k <= K; k++){
				g += gcdThree(i, j, k);
			}
		}
	}	
	return g;
}

int main(){
	int K;
	long long int ret = 0;
	scanf("%d", &K);
	ret = gcdSum(K);
	printf("%lld\n", ret);
	return 0;
}