#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iomanip>
#define ll long long

using namespace std;

long long MOD = 1000000007;

long long gcd(long long N, long long M){
    while (true){
        if (min(N,M)==0){
            return max(N,M);
        }else if (N>M){
            N%=M;
        }else{
            M%=N;
        }
    }
}

int main(){
    int K;
    cin >> K;
    ll sum_=0;
    for(ll i=1; i<=K; i++){
        for(ll j=1; j<=K; j++){
            for(ll k=1; k<=K; k++){
                sum_+=gcd(gcd(i,j),k);
            }
        }
    }
    cout << sum_ << endl;
}