from math import gcd



K = int(eval(input()))

ans = 0

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

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

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

            ans += gcd(gcd(a, b), c)

print(ans)
from math import gcd



K = int(eval(input()))

ans = 0

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

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

        x = gcd(a, b)

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

            ans += gcd(x, c)

print(ans)
from math import gcd

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

from math import gcd

K = int(eval(input()))
ans = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        x = gcd(a, b)
        for c in range(1, K + 1):
            ans += gcd(x, c)
print(ans)

+        x = gcd(a, b) -            ans += gcd(gcd(a, b), c) +            ans += gcd(x, c)