#include <bits/stdc++.h> 

using namespace std;

typedef long long ll;

#define rep(i,p,q) for(ll i = p; i < q; i++)

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

ll gcd(ll a, ll b, ll c){
    ll ab = gcd(a,b);
    ll bc = gcd(b,c);
    return gcd(ab,bc);
}

int main(void){
    ll k;
    cin >> k;
    k++;
    ll ans = 0;
    rep(i,1,k){
        rep(j,1,k){
            rep(m,1,k){
                ans += gcd(i,j,m);
            }
        }
    }
    cout << ans << endl;
}