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

static void swap (int *a, int *b) {
    int tmp;

    tmp = *a;
    *a = *b;
    *b = tmp;
}

static int gcd (int a, int b) {
    int r;

    if ( a < b )
        swap(&a, &b);

    r = a % b;
    while ( r != 0 ) {
        a = b;
        b = r;
        r = a % b;
    }

    return b;
}

static int gcd3 (int a, int b, int c) {
    return gcd(gcd(a, b), c);
}

int main (void) {
    int         K;
    int         i, j, k;
    long long   ans = 0;

    scanf("%d", &K);

    for ( i=1; i<=K; i++ ) {
        for ( j=1; j<=K; j++ ) {
            for ( k=1; k<=K; k++ ) {
                ans += gcd3(i, j, k);
            }
        }
    }

    printf("%lld\n", ans);
    return EXIT_SUCCESS;
}

