#include<stdio.h>

int gcd(int, int);

int main() {
	int K;
	int x = scanf("%d", &K);
	long long int sum = 0;
	for (int i = 1;i <= K;++i) {
		for (int j = 1;j <= K;++j) {
			for (int k = 1;k <= K;++k) {
				sum += gcd(gcd(i, j), k);
			}
		}	
	}
	
	printf("%lld\n", sum);
	return 0;
}

int gcd(int x, int y) {
	int a = x, b = y;
	while (true) {
		if (a == b) {
			return a;
		}
		else if (a < b) {
			int t = a;
			a = b;
			b = t;
		}

		a -= b;
	}
}
