#include <stdio.h>

int fun(int a,int b);

int main()
{
	long long int n,sum = 0;
	int a,b,c;
	
	scanf("%lld",&n);
	
	for(a = 1;a <= n;a++)
	{
		for(b = 1;b <= n;b++)
		{
			for(c = 1;c <= n;c++)
				sum += fun(a,fun(b,c));
		}
	}
	
	printf("%lld\n",sum);
	
	return 0;
}

int fun(int x,int y)
{
	int r;
	
	if(x < y)
	{
		r = x;
		x = y;
		y = r;
	}
	r = x % y;
	
	while(r != 0)
	{
		x = y;
		y = r;
		r = x % y;
	}
	
	return y;
}