#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll fun(ll x){
    return x*(x+1)/2;
}
int gcd(int x,int y ,int z){
    int gc = __gcd(x,y);
    return __gcd(gc,z);
}
int main (){
    ll n;cin >> n;

    ll sum = 0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            for(int k=1;k<=n;k++){
                sum+=gcd(i,j,k);
            }
        }
    }
    cout<<sum<<endl;
    return 0;
}
