#include<stdio.h>
#include<stdlib.h>
int fun(int a, int b);
int main()
{
	int i,j,k,n;
	long long int sum = 0;
	scanf("%d", &n);
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= n; j++)
		{
			for (k = 1; k <= n; k++)
			{
				sum += fun(i, fun(j, k));
			}
		}
	}
	printf("%lld", sum);
	return 0;
}
int fun(int a, int b)
{
	int r;
	do
	{
		r = a % b;
		a = b;
		b = r;
	} while (r != 0);
	return a;
}