#include<stdio.h>

int gcd(int a, int b){
    return a%b==0 ? b : gcd(b,a%b);
}

int main() {
    int k,a,b,c;
    long int sum=0;
    scanf("%d",&k);

    for(a=1; a<=k; a++)
        for(b=1; b<=k; b++)
            for(c=1; c<=k; c++){
                if(a<=b<=c) {if(gcd(c,b) >= a) sum+=gcd(gcd(c,b),a); 
                                else sum+=gcd(a,gcd(c,b)); }
                else if(a<=c<=b) {if(gcd(b,c)>=a) sum+=gcd(gcd(b,c),a); 
                                    else sum+= gcd(a,gcd(b,c)); }
                if(b<=a<=c) {if(gcd(c,a) >= b) sum+=gcd(gcd(c,a),b); 
                                else sum+=gcd(b,gcd(c,a)); }
                else if(b<=c<=a) {if(gcd(a,c)>=b) sum+=gcd(gcd(a,c),b); 
                                    else sum+= gcd(b,gcd(a,c)); }
                if(c<=b<=a) {if(gcd(a,b) >= c) sum+=gcd(gcd(a,b),c); 
                                else sum+=gcd(c,gcd(a,b)); }
                else {if(gcd(b,a)>=c) sum+=gcd(gcd(b,a),c); 
                                    else sum+= gcd(c,gcd(b,a)); }
            }
    printf("%d",sum/3);
                
    return 0;

}