#include<bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(i, n) for (int i=1; i<=n; i++)
using namespace std;
using f=float; using ll=long long; using ld=long double;
using ull=unsigned long long; using vi=vector<int>; using pii=pair<int, int>;

int gcd(int a, int b) {
    if ( b < a ) swap(a, b);
    int t;
    while ( b != 0 ) {
        t = a%b; a = b; b = t;
    }
    return a;
}
int gcd3(int a, int b, int c) {
    int d = gcd(a, b);
    return gcd(d, c);
}
int main() {
//    int a = gcd3(3, 6, 12);
    int n;
    cin >> n;
    ll sum = 0;
    REP(i, n ) {
        REP(j, n) {
            REP(k, n) {
                sum += gcd3(i, j, k);
            }
        }
    }
    cout << sum << endl;
    return 0;
}