#include <inttypes.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

intmax_t gcd(intmax_t a, intmax_t b)
{
    while (b != 0) {
        intmax_t r = a % b;
        a = b;
        b = r;
    }
    return a;
}

int main(void)
{
    intmax_t K;
    intmax_t ans = 0;

    scanf("%jd", &K);

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

    printf("%jd\n", ans);

    return 0;
}
