#include <stdio.h>

int gcd2(int,int);
int gcd3(int,int,int);

int main(){
  int K;
  int count = 0;
  scanf("%d",&K);
  for(int i=1;i<=K;i++){
    for(int j=1;j<=K;j++){
      for(int k=1;k<=K;k++){
	count += gcd3(i,j,k);
      }
    }
  }
  printf("%d",count);
  return 0;
}

int gcd2(int a,int b){
  int temp;
  if(a<b){
    temp = a;
    a = b;
    b = temp;
  }
  while(b>0){
    temp = b;
    b = a%b;
    a = temp;
  }
  return a;
}

int gcd3(int a,int b,int c){
  int d;
  d = gcd2(b,c);
  d = gcd2(a,d);
  return d;
}