#include <bits/stdc++.h>
using namespace std;

int gcd(int l,int s) {
  int L,S;
  while(l%s!=0) {
    L=l;
    S=s;
    l=max(S,L%S);
    s=min(S,L%S);
  }
  return s;
}
int gcd3(int a,int b,int c) {
  b = gcd(max(a,b),min(a,b));
  return gcd(max(b,c),min(b,c));
}

int main() {
  int K,ans=0;
  cin >> K;
  for(int i=1;i<=K;i++) {
	for(int j=1;j<=K;j++) {
      for(int k=1;k<=K;k++) {
        ans+=gcd3(i,j,k);
      }
    }
  }
  cout << ans << endl;
}