#include<stdio.h>
#include<string.h>
#include <stdlib.h>
int gcd(int a,int b){
    int r;
    r=a%b;
    while (r!=0) {
        a=b;
        b=r;
        r=a%b;
    }
    return b;
}
int main(void){
    int K,ab,SUM=0;
    scanf("%d",&K);
    for (int a=1; a<=K; a++) {
        for (int b=1; b<=K; b++) {
            if (a>=b) {
                ab=gcd(a,b);
            }
            else{
                ab=gcd(b,a);
            }
            for (int c=1; c<=K; c++) {
                if (ab>=c) {
                    SUM+=gcd(ab,c);
                }
                else{
                    SUM+=gcd(c,ab);
                }
            }
        }
    }
    printf("%d\n",SUM);
    return 0;
}