#include <stdio.h>
#define rep(i, n) for(int i=1; i<(int)(n);i++)

typedef long long ll;

int main(){
  ll a, b, c, k, ans=0;
  ll gcd_3(ll ,ll ,ll );
  scanf("%lld",&k);
  rep(a,k+1){
    rep(b,k+1){
      rep(c,k+1){
        ans += gcd_3(a,b,c);
      }
    }
  }
  printf("%lld",ans);
}


ll gcd_3(ll a,ll b,ll c){
  ll gcd(ll ,ll );
  return gcd(gcd(a,b),c);
}
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}