#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define rep(i,l,n) for(int (i)=(l);(i)<(n);(i)++)
#define min(a,b) (a)<(b)?(a):(b)
#define max(a,b) (a)>(b)?(a):(b)
#define abs(a) (a)>0?(a):-(a)
#define llong long long

int gcd3(int,int,int);
int gcd(int,int);

int main(){
	int N; scanf("%d",&N);
	N++;
	llong cnt=0;
	rep(i,1,N){ rep(j,1,N){ rep(k,1,N){
		cnt += gcd3(i,j,k);
	}}}
	printf("%lld",cnt);
	return 0;
}

int gcd3(int a,int b,int c){
	return gcd(gcd(a,b),gcd(b,c));
}

int gcd(int a,int b){
	if( b==0 ) return a;
	return gcd(b,a%b);
}
