import math

from functools import reduce

from itertools import product



def resolve():

  K = int(eval(input()))

  gcd = math.gcd

  step1 = [ gcd(x[0], x[1]) for x in product(list(range(1, K+1)), list(range(1, K+1))) ]

  result = sum([ gcd(x[0], x[1]) for x in product(step1, list(range(1, K+1))) ])

  print(result)



if __name__ == "__main__":

    resolve()

import math



k = int(eval(input()))



ans = 0

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

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

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

            ans += math.gcd(math.gcd(i,j), l)



print(ans)

import math
from functools import reduce
from itertools import product


def resolve():
    K = int(eval(input()))
    gcd = math.gcd
    step1 = [
        gcd(x[0], x[1]) for x in product(list(range(1, K + 1)), list(range(1, K + 1)))
    ]
    result = sum([gcd(x[0], x[1]) for x in product(step1, list(range(1, K + 1)))])
    print(result)


if __name__ == "__main__":
    resolve()

import math

k = int(eval(input()))
ans = 0
for i in range(1, k + 1):
    for j in range(1, k + 1):
        for l in range(1, k + 1):
            ans += math.gcd(math.gcd(i, j), l)
print(ans)

-from functools import reduce -from itertools import product - -def resolve(): -    K = int(eval(input())) -    gcd = math.gcd -    step1 = [ -        gcd(x[0], x[1]) for x in product(list(range(1, K + 1)), list(range(1, K + 1))) -    ] -    result = sum([gcd(x[0], x[1]) for x in product(step1, list(range(1, K + 1)))]) -    print(result) - - -if __name__ == "__main__": -    resolve() +k = int(eval(input())) +ans = 0 +for i in range(1, k + 1): +    for j in range(1, k + 1): +        for l in range(1, k + 1): +            ans += math.gcd(math.gcd(i, j), l) +print(ans)