#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < n; ++i)
using namespace std;

int gcd(int a, int b){
    int buff;
    int r;
    if(a < b){
        gcd(b,a);
    }
    while((r = a%b)){
        a = b;
        b = r;
    }
    return b;
}

int main(){
    int K;
    cin >> K;
    int i,j,k, r;
    long long r_sum = 0;
    for(i=1; i<= K; i++){
        for(j=1; j<= K; j++){
            for(k = 1; k <= K; k++){
                r = gcd(gcd(i,j),k);
                r_sum += r;
            }
        }
    }
    cout << r_sum << endl;
    return 0;
}