#include <bits/stdc++.h>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,n) FOR(i,0,n)
#define out(a) cout << a << endl
using namespace std;
typedef long long ll;

int gcd2(int a, int b){
    if (a%b==0){
        return b;
    } else {
        return gcd2(b, a%b);
    }
}
int gcd3(int a, int b, int c){
    if (a%c==0 && b%c==0){
        return c;
    } else if (a%c==0) {
        return gcd2(c, b);
    } else if (b%c==0) {
        return gcd2(c, a);
    } else {
        if (a%c < b%c) return gcd3(c, b%c, a%c);
        else return gcd3(c, a%c, b%c);
    }
}

int main() {
    int K;
    ll count=0;
    cin >> K;
    FOR(a, 1, K+1){
    FOR(b, 1, K+1){
    FOR(c, 1, K+1){
        count += gcd3(a,b,c);
    }
    }
    }
    out(count);
}


