#include <iostream>

#define REP(i, e) for(int (i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for(int (i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define ACC(c) (accumulate(ALL(c), 0LL))
#define PRINT(x) cout << (x) << "\n"

using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;

inline int std_in(int& a) {return scanf("%d", &a);}
auto pint_compare = [](pint x, pint y) -> bool{return (x.first < y.first);};
const int MOD = 1000000007;

//aとbの最大公約数を返す
long long gcd(long long a, long long b){
    while (a != 0 && b != 0){
        if (a >= b) a %= b;
        else b %= a;
    }
    return a + b;
}

signed main(){
    ll K, ans = 0;
    cin >> K;
    for(ll a = 1; a <= K; a++){
        for(ll b = 1; b <= K; b++){
            for(ll c = 1; c <= K; c++){
                ans += gcd(gcd(a, b), c);
            }
        }
    }
    PRINT(ans);
    return 0;
}