#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<deque>
using namespace std;
#define ll long long
const int mod = 1000000007;
const ll INF = 1000000000000000000;

ll gcd(ll a, ll b)
{
	if (a < b) return gcd(b, a);
	if (b == 0) return a;
	else return gcd(b, a % b);
}

int main()
{
	int K;
	cin >> K;
	ll ans = 0;
	for (int i = 1; i <= K; i++) {
		for (int j = 1; j <= K; j++) {
			if (gcd(i, j) == 1) {
				ans += K;
			}
			else {
				int num = gcd(i, j);
				for (int k = 1; k <= K; k++) {
					ans += gcd(num, k);
				}
			}
		}
	}
	cout << ans << endl;
}
