
int gcd(int a,int b){
    if(a%b==0){
        return b;
    }    
    return gcd(b,a%b);
}
/*
int main(void){
	printf("nyan");
	printf("%d",gcd(gcd(24,6),12));
}
 
*/
 
 
int main(void){
    //printf("input\n");
 
    int i,j,k;
    // int l,m,n;
    int K;
    int ret;
    // scanf("%d",&K);
    // scanf("%d",&j);
    ret = scanf("%d",&K);
    //printf("line1\n");
    int total = 0; 
 
    for(i=1;i<=K;i++){
    	//printf("line3\n");
        for(j=1;j<=K;j++){
    	    //printf("line4\n");
            //printf("%d \n",gcd(i,j));
            for(k=1;k<=K;k++){
		//printf("line5\n");
		//printf("%d:%d:%d \n",i,j,k);
                total += gcd(gcd(i,j),k);                
               	//printf("total:%d\n",total);
 
            }   
        }   
    }
 
 
    
    printf("%d",total);
    
}