from math import gcd

n = int(eval(input()))

g = 0

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

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

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

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

print(g)



import functools

from math import gcd

n = int(eval(input()))

@functools.lru_cache(None)

def main(n):

  g=0

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

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

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

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

  return (g)

print((main(n)))
from math import gcd

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

import functools
from math import gcd

n = int(eval(input()))


@functools.lru_cache(None)
def main(n):
    g = 0
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            for k in range(1, n + 1):
                g += gcd(gcd(i, j), k)
    return g


print((main(n)))

+import functools -g = 0 -for i in range(1, n + 1): -    for j in range(1, n + 1): -        for k in range(1, n + 1): -            g += gcd(gcd(i, j), k) -print(g) + + +@functools.lru_cache(None) +def main(n): +    g = 0 +    for i in range(1, n + 1): +        for j in range(1, n + 1): +            for k in range(1, n + 1): +                g += gcd(gcd(i, j), k) +    return g + + +print((main(n)))