#include <stdio.h>
using namespace std;
 
long long N;
long long ans;

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

int main(void){
	int a, b, c,d,tmp,K;
	long ans;
	cin >> K;
	for(a=1;a<=K;a++){
		for(b=1;b<=K;b++){
			for(c=1;c<=K;c++){
				if(a<b){
					tmp = a;
					a = b;
					b = tmp;
				}
				d=gcd(a, b);
				if(c<d){
					tmp = c;
					c = d;
					d = tmp;
				}
				ans += gcd(c, d);
			}
		}
	}
	cout >> ans + "\n";
	return 0;
}