#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

uint64_t gcd(uint64_t a,uint64_t b); 
uint64_t gcd3(uint64_t a,uint64_t b,uint64_t c); 
int main(void) {
    char buf[8];
    char *p;
    int N;
    int i,j,k,cnt = 0;
    uint64_t sum=0;

    p = fgets(buf, 8, stdin);
    N = atoi(buf);

    for(i=1;i<=N;i++){
        for(j=1;j<=N;j++){
            for(k=1;k<=N;k++){
                sum += gcd3(i,j,k);
            }
        }
    }

    printf("%lu\n", sum);

    return 0;
}

uint64_t gcd3(uint64_t a,uint64_t b,uint64_t c) {
    uint64_t ggg;
    if(a == 1 || b == 1 || c == 1){
    ggg = 1;
    } else {

    ggg= gcd(a,gcd(b,c));
    }

//    printf("%lu,%lu,%lu => %lu\n", a,b,c,ggg);
    return ggg;
}

uint64_t gcd(uint64_t a,uint64_t b) {
    if(b == 0) {
        return a;
    }

    if(b > a) {
        return gcd(a, b%a);
    } else {
        return gcd(b, a%b);
    }
}
