#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <sstream>
#include <cmath>
#include <functional>
#include <fstream>
#include <cstdlib>
typedef long long ll;
const ll MOD = 1e9+7;


#define REP(i, N) for (ll i = 0; i < (N); i++)
#define REPP(i, a, b) for (ll i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define VSORT(c) sort((c).begin(),(c).end())
#define SZ(x) ((ll)(x).size())
//vvintを作る　マクロで　 #define vvint(N,M) vector<vector<ll>>

using namespace std;

int gcd(int a, int b , int c){
    if(b < c) swap(b,c);
    if(a < b) swap(a,b);
    if(b < c) swap(b,c);

    while(1) {
    if(a < c) swap(a, c);
    if(!c) break;
    a %= c;
  }
  while(1) {
    if(a < b) swap(a, b);
    if(!b) break;
    a %= b;
  }

  return a;

}

int main(){
    
    cin.tie(0);
    ios::sync_with_stdio(false);

    int k;
    cin >> k;
    ll sum = 0;
    for(ll i = 1; i < k+1;i++){
        for(ll l = 1; l < k+1;l++){
            for(ll m = 1; m < k+1;m++){
              sum+=gcd(i,l,m);
            }
        }
    }
    cout << sum << endl;

    return 0;
}