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(a,gcd(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):

    p = gcd(a,b)

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

      ans += gcd(p,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(a, gcd(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):
        p = gcd(a, b)
        for c in range(1, k + 1):
            ans += gcd(p, c)
print(ans)

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