import math



K = int(eval(input()))



total = 0

for a in range(1, K + 1):

    for b in range(1, K + 1):

        ab = math.gcd(a, b)

        for c in range(1, K + 1):

            total += math.gcd(ab, c)



print(total)
import math



K = int(eval(input()))



total = 0

for a in range(1, K + 1):

    for b in range(1, K + 1):

        ab = math.gcd(a, b)

        if ab == 1:

            total += K

        else:

            for c in range(1, K + 1):

                total += math.gcd(ab, c)



print(total)
import math

K = int(eval(input()))
total = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        ab = math.gcd(a, b)
        for c in range(1, K + 1):
            total += math.gcd(ab, c)
print(total)

import math

K = int(eval(input()))
total = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        ab = math.gcd(a, b)
        if ab == 1:
            total += K
        else:
            for c in range(1, K + 1):
                total += math.gcd(ab, c)
print(total)

-        for c in range(1, K + 1): -            total += math.gcd(ab, c) +        if ab == 1: +            total += K +        else: +            for c in range(1, K + 1): +                total += math.gcd(ab, c)