from math import gcd 

K=int(eval(input()))



s=0



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

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

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

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



print(s)
from math import gcd 

K=int(eval(input()))



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

            s+=gcd(a,k)



print(s)

from math import gcd

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

from math import gcd

K = int(eval(input()))
s = 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):
            s += gcd(a, k)
print(s)

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