#include<stdio.h>
int main()
{
  int K;
  scanf("%d",&K);
  int i,j,l;
  int a,b,c,tmp,r;
  unsigned long sum=0;
  for(i=1;i<K+1;i++){
    for(j=1;j<K+1;j++){
      for(l=1;l<K+1;l++){
        a=i;
        b=j;
        c=l;
        if(a<b){
          tmp=a;
          a=b;
          b=tmp;
        }
        r = a % b;
        while(r!=0){
          a = b;
          b = r;
          r = a % b;
        }
        if(b<c){
          tmp=c;
          c=b;
          b=tmp;
        }
        r = b % c;
        while(r!=0){
          b = c;
          c = r;
          r = b % c;
        }
        sum=sum+c;
      }
    }
  }
  printf("%lu",sum);
  return 0;
}
