#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i < (int)(n); i++)
using ull = unsigned long long;
using ll = long long;

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

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(gcd(a, b), c);
            }
        }
    }

    cout << ans << endl;

    return 0;
}

