#include<iostream>

using namespace std;

int gcd(int, int);

int main() {
	int n;
	cin >> n;
	long long sum = 0;
	for (int i = 1;i <= n;i++) {
		for (int j = 1;j <= n;j++) {
			for (int k = 1;k <= n;k++) {
				sum += gcd(gcd(i, j), k);
			}
		}
	}
	cout << sum << "\n";
	return 0;
}

int gcd(int a, int b) {
	int work;
	if (b > a) {
		work = a;
		a = b;
		b = work;
	}
	while (a % b != 0) {
		work = a % b;
		a = b;
		b = work;
	}
	return b;
}