#include <bits/stdc++.h>
#define r(i,n) for(int i = 1; i<=n; i++)
typedef long long ll;
using namespace std;
const ll mod=1000000007;

ll gcd(ll a, ll b){
  if(b==0)return a;
  else return gcd(b,a%b);
}

int main() {
  int k,sum=0;
  cin >> k;
  r(i,k){
    r(j,k){
      r(h,k){
        sum+=gcd(i,gcd(j,h));
      }
    }
  }
  cout<<sum<<endl;
}