from math import gcd

K = int(eval(input()))

ans = 0

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

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

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

            ans += gcd(gcd(i, j), k)

print(ans)
from math import gcd

K = int(eval(input()))

ans = 0

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

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

        a = gcd(i, j)

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

            ans += gcd(a, k)

print(ans)
from math import gcd

K = int(eval(input()))
ans = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        for k in range(1, K + 1):
            ans += gcd(gcd(i, j), k)
print(ans)

from math import gcd

K = int(eval(input()))
ans = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        a = gcd(i, j)
        for k in range(1, K + 1):
            ans += gcd(a, k)
print(ans)

+        a = gcd(i, j) -            ans += gcd(gcd(i, j), k) +            ans += gcd(a, k)