// Apna Time Aayega...
#include <bits/stdc++.h>
using namespace std;

#define MOD 1000000007
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl "\n"
#define ll long long
#define inf 100000000000000000
int addMod(int x, int y)
{
    x += y;
    while(x >= MOD) x -= MOD;
    while(x < 0) x += MOD;
    return x;
}

int mulMod(int x, int y)
{
    return (x * 1ll * y) % MOD;
}

int binpow(int x, int y)
{
    int z = 1;
    while(y)
    {
        if(y & 1) z = mulMod(z, x);
        x = mulMod(x, x);
        y >>= 1;
    }
    return z;
}

int inv(int x)
{
    return binpow(x, MOD - 2);
}

int divide(int x, int y)
{
    return mulMod(x, inv(y));
}

ll gcd(int a, int b) 
{   if (a == 0) return b; 
    if (b == 0) return a;  
    if (a == b) return a; 
    if (a > b)  return gcd(a-b, b); 
    return gcd(a, b-a); 
} 

ll lcm(ll a, ll b){
    return (a*b)/gcd(a, b);
}

ll h(ll* A, int N){
    ll pro = 1;
    for(int i=0; i<N; ++i) pro = mulMod(A[i], pro);
    return pro;
}

ll f(ll* A, int N){
    int alpha = A[0];
    for(int i=1; i<N; ++i){
        alpha = gcd(alpha, A[i])%MOD;
    }
    return alpha;
}

void generateNormal(string ans, int N, int charUsed){
    if(N==0) {cout<<ans<<endl; return;}
    for(int i=0; i<charUsed+1; ++i){
        char currentChar = 'a' + i;
        generateNormal(ans+currentChar, N-1, i+1);
    }
    return;
}
int N, X;
int A[100005];
vector <int> adj[100005];
int dfs(int u, int p){
    int sum = A[u];
    for(auto &i:adj[u]){
        if(i!=p){
            sum += dfs(i, u);
        }
    }
    return max(sum, -X);
}

void cl()
{
    for(int i=1;i<=N;i++)
        adj[i].clear();
}

unsigned long long int counting(int a, int b){
    unsigned long long int count = 1;
    while(a>0 || b>0){
        if(a&1 && b&1){
            count = mulMod(2, count);
        }
        a = a>>1;
        b = b>>1;
    }
    return count;
}


int solve(priority_queue<int> A, 
          priority_queue<int> B,
          priority_queue<int> C, int X, int Y)
{
    cout<<"INside"<<endl;
    if(X==0 && Y==0) return 0;
    int alpha = A.top();
    int beta = B.top();
    int gamma = C.top();
    A.pop();
    int alpha1 = 0, beta1 = 0, gamma1 = 0, gamma2 = 0;
    if(X!=0)
    alpha1 = alpha + solve(A, B, C, X-1, Y);
    A.push(alpha);
    B.pop();
    if(Y!=0)
    beta1 = beta + solve(A, B, C, X, Y-1);
    B.push(beta);
    C.pop();
    if(X!=0)
    gamma1 = gamma + solve(A, B, C, X-1, Y);
    if(Y!=0)
    gamma2 = gamma + solve(A, B, C, X, Y-1);
    C.push(gamma);
    return max(alpha1, max(beta1, max(gamma1, gamma2)));
}

int zero_one_knapsack(int* val, int* weights, int n, int W)
{
    int dp_table[n+1][W+1];
    for(int i=0; i<n+1; i++)    // This is n
    {
        for(int j=0; j<W+1; j++)    // This is w
        {
            if(i==0 || j==0) dp_table[i][j] = 0;
            else if(weights[i-1]>j) dp_table[i][j] = dp_table[i-1][j];
            else dp_table[i][j] = max(val[i-1] + dp_table[i-1][j-weights[i-1]], dp_table[i-1][j]);
            cout<<dp_table[i][j]<<" ";
        }
        cout<<endl;
    }
    return dp_table[n][W];
}

class node{
    public:
    int data;
    node* left = NULL;
    node* right = NULL;
};

int isSumProperty(node* Node){
    int left_data = 0, right_data = 0;
    if(Node==NULL || (Node->left==NULL && Node->right==NULL)) return 1;
    if(Node->left!=NULL) left_data = Node->left->data;
    if(Node->right!=NULL) right_data = Node->right->data;
    if((Node->data == left_data + right_data) && (isSumProperty(Node->left) && isSumProperty(Node->right))) return 1;
    else return 0;
}

int largestSumSubarray(int* array, int length)
{
    int dp_table[length] = {0};
    dp_table[0] = array[0];
    int answer = dp_table[0];
    for(int i=1; i<length; ++i){
        dp_table[i] = max(dp_table[i-1] + array[i], array[i]);
        if(dp_table[i]>answer) answer = dp_table[i];
    }
    if(answer<0) return 0;
    return answer;
}   

void reverse(int* binaryNumber, int length){
    for(int i=1; i<=length/2; ++i){
        int temp = binaryNumber[i];
        binaryNumber[i] = binaryNumber[length-i+1];
        binaryNumber[length-i+1] = temp;
    }
    return;
}
void compliment(int* binaryNumber, int length){
    for(int i=1; i<length; ++i){
        if(binaryNumber[i]==0) binaryNumber[i] = 1;
        else binaryNumber[i] = 0;
    }
    return;
}
#include <chrono>

string lA(string* array, int N){
    int pointers[N];
    int max = 0, index = -1;
    for(int i=0; i<N; ++i){
        pointers[i] = array[i].length();
        if(array[i].length()>max) {max = array[i].length(); index = i;}
    }
    string solution = "";
    bool valid = true;
        for(int i=0; i<max; ++i){
        char current = array[index][max-1-i];
        // if(current!='*')
        solution = current + solution;
        for(int i=0; i<N; ++i){
            if(array[i][pointers[i]-1]==current) pointers[i]--;
            else if(array[i][pointers[i]-1]=='*');
            else{
                valid = false;
                break;
            }
        }
    }
    if(valid) return solution;
    else return "*";
    // n++;
}

string rA(string* array, int N){
    int pointers[N];
    int max = 0, index = -1;
    for(int i=0; i<N; ++i){
        pointers[i] = array[i].length();
        if(array[i].length()>max) {max = array[i].length(); index = i;}
    }
    int p[N] = {0};
    string solution = "";
    bool valid = true;
        for(int i=0; i<max; ++i){
        char current = array[index][i];
        solution = solution + current;
        for(int i=0; i<N; ++i){
            if(array[i][p[i]]==current) p[i]++;
            else if(array[i][p[i]]=='*');
            else{
                valid = false;
                break;
            }
        }
    }
    if(valid) return solution;
    else return "*";
    // n++;
}

string mA(string* array, int N){
    string arrayLA[N];
    string arrayRA[N];
    for(int i=0; i<N; ++i){
        string left = "", right = "";
        int pointer = 0;
        while(array[i][pointer]!='*' && pointer<array[i].length()) right = right + array[i][pointer++];
        right = right + "*";
        arrayRA[i] = right;
        while(pointer<array[i].length()) left = left + array[i][pointer++];
        arrayLA[i] = left;
    } 
    string left = lA(arrayLA, N);
    string right = rA(arrayRA, N);
    string answer = right;
    for(int i=1; i<left.length(); ++i) answer += left[i];
    return answer;
}

int main(){
    FIO
    long long int sum = 0;
    int K; cin>>K;
    for(int i=1; i<=K; ++i){
        for(int j=1; j<=K; j++){
            for(int k=1; k<=K; k++){
                // cout<<"gcd("<<i<<", "<<j<<", "<<k<<") = "<<gcd(i, gcd(j, k))<<endl;
                sum+= gcd(i, gcd(j, k));
            }
        }
    }
    cout<<sum<<endl;
}


Measured memory_bytes: 6000