#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <algorithm>
#include <iomanip>
#include <string.h>

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define ALL(a)  (a).begin(),(a).end()

typedef long long lint;

using namespace std;

lint gcd(lint m,lint n){
  if(n==0)return m;
  else return gcd(n,m%n);
}

int main(){
  lint K;
  cin>>K;
  lint S=0;
  FOR(a,1,K+1)FOR(b,1,K+1)FOR(c,1,K+1){
    S+=gcd(a,gcd(b,c));
  }
  cout<<S<<endl;
  return 0;
}
