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):

        tmp = gcd(i, j)

        if tmp == 1:

            ans += K

        else:

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

                ans += gcd(tmp, 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):
        tmp = gcd(i, j)
        if tmp == 1:
            ans += K
        else:
            for k in range(1, K + 1):
                ans += gcd(tmp, k)
print(ans)

-        for k in range(1, K + 1): -            ans += gcd(gcd(i, j), k) +        tmp = gcd(i, j) +        if tmp == 1: +            ans += K +        else: +            for k in range(1, K + 1): +                ans += gcd(tmp, k)