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

print(ans)

from math import gcd

from numba import jit



@jit

def calc(): 

  k=int(eval(input()))

  ans=0

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

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

      d=gcd(a,b)

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

        ans+=gcd(d,c)

  print(ans)

calc()
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(gcd(a, b), c)
print(ans)

from math import gcd
from numba import jit


@jit
def calc():
    k = int(eval(input()))
    ans = 0
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            d = gcd(a, b)
            for c in range(1, k + 1):
                ans += gcd(d, c)
    print(ans)


calc()

+from numba import jit -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(gcd(a, b), c) -print(ans) + +@jit +def calc(): +    k = int(eval(input())) +    ans = 0 +    for a in range(1, k + 1): +        for b in range(1, k + 1): +            d = gcd(a, b) +            for c in range(1, k + 1): +                ans += gcd(d, c) +    print(ans) + + +calc()