#include <stdio.h>

int gcd(int a, int b)
{
	
	int t;

	if (a < b)
	{
		t = a;
		a = b;
		b = t;
	}

	if (b == 0)
        return (a);

    return gcd(b, a % b);
}

int gcd3(int a, int b, int c)
{
	int d;

	d = gcd(a, b);
	return (gcd(d,c));
}

int main(void)
{
	int a = 1;
	int b = 1;
	int c = 1;
	int k = 0;
	long sum = 0;

	scanf("%d", &k);
	for (a = 1; a < k+1; a++)
	{
		for (b = 1; b < k+1; b++)
		{
			for (c = 1; c < k+1; c++)
			{
				sum += gcd3(a, b, c);
			}
		}
	}
	printf("%ld\n", sum);
	return (0);
}