#include <stdio.h>

int min(int a, int b) {

    if(a <= b) return a;
    else return b;
}

/* 
int gcd(int a, int b, int c) {
    if (c == 0) return a;
    return gcd(c, a % c, b % c); 
}
*/

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

int main() {

int i;
int a, b, c;
int K;
int sum;

scanf("%d", &K);

sum = 0;

    for (a = 1; a <= K; a++) {
        //sum++;
        for (b = 1; b <= K; b++) {
            //sum++;
            for (c = 1; c <= K; c++) {
                sum += gcd(gcd(a, b), c);
                //sum += gcd(a, b, c);
            }
        }
    }

printf("%d\n", sum);
    return 0;
}

