#include <bits/stdc++.h>
using namespace std;

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

int main () {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int K;
    cin >> K;
    long long sum = 0;
    for (int i = 1; i <= K; ++i) {
        for (int j = 1; j <= K; ++j) {
            for (int k = 1; k <= K; ++k) {
                sum += gcd(i, j, k);
            }
        }
    }
    cout << sum << endl;
    return 0;
}