from math import gcd





n = int(eval(input()))

c = 0

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

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

        a = gcd(i, j)

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

            c += gcd(a, k)



print(c)
from math import gcd



def solve():

    K = int(eval(input()))

    ans = 0



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

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

            b = gcd(i, k)

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

                ans += gcd(b, m)

    return ans



print((solve()))



from math import gcd

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

from math import gcd


def solve():
    K = int(eval(input()))
    ans = 0
    for i in range(1, K + 1):
        for k in range(1, K + 1):
            b = gcd(i, k)
            for m in range(1, K + 1):
                ans += gcd(b, m)
    return ans


print((solve()))

-n = int(eval(input())) -c = 0 -for i in range(1, n + 1): -    for j in range(1, n + 1): -        a = gcd(i, j) -        for k in range(1, n + 1): -            c += gcd(a, k) -print(c) + +def solve(): +    K = int(eval(input())) +    ans = 0 +    for i in range(1, K + 1): +        for k in range(1, K + 1): +            b = gcd(i, k) +            for m in range(1, K + 1): +                ans += gcd(b, m) +    return ans + + +print((solve()))