#include<stdio.h>

int main(void)
{
	int K, a, b, c;
	long int sum = 0;

	scanf("%d", &K);
	for (a = 1; a <= K; a++) {
		for (b = 1; b <= K; b++) {
			for (c = 1; c <= K; c++) {
				sum += gcd3(a, b, c);
			}
		}
	}
  printf("%d",sum);

	return 0;
}

int gcd2(int a, int b) {
	if (b == 0)
		return a;
	return(b, a % b);
}

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