#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"

ll gcd(ll a, ll b) { return b ? gcd(b,a % b) : a; }

int main() {
    int K;
    cin >> K;

    ll ans = 0;
    for (int a = 1; a <= K; ++a) {
        for (int b = 1; b <= K; ++b) {
            for (int c = 1; c <= K; ++c) {
                ans += gcd(a, gcd(b, c));
            }
        }
    }

    cout << ans << endl;
    return 0;
}
