from math import gcd #or import math

K = int(eval(input()))#or K = int(input())+1

plus = 0



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

    for a in range(1,K+1):# range(1,K)

        x = gcd(i,a) #or math.gcd(i,a)

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

            plus += gcd(x,j) #or math.gcd(x,j)

            

print(plus)
from math import gcd

K = int(eval(input()))

plus = 0

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

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

        temp = gcd(a, b)

        if temp != 1:

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

                plus += gcd(temp, c)

        else:

            plus += K

print(plus)
from math import gcd  # or import math

K = int(eval(input()))  # or K = int(input())+1
plus = 0
for i in range(1, K + 1):  # range(1,K)
    for a in range(1, K + 1):  # range(1,K)
        x = gcd(i, a)  # or math.gcd(i,a)
        for j in range(1, K + 1):  # range(1,K)
            plus += gcd(x, j)  # or math.gcd(x,j)
print(plus)

from math import gcd

K = int(eval(input()))
plus = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        temp = gcd(a, b)
        if temp != 1:
            for c in range(1, K + 1):
                plus += gcd(temp, c)
        else:
            plus += K
print(plus)

-from math import gcd  # or import math +from math import gcd -K = int(eval(input()))  # or K = int(input())+1 +K = int(eval(input())) -for i in range(1, K + 1):  # range(1,K) -    for a in range(1, K + 1):  # range(1,K) -        x = gcd(i, a)  # or math.gcd(i,a) -        for j in range(1, K + 1):  # range(1,K) -            plus += gcd(x, j)  # or math.gcd(x,j) +for a in range(1, K + 1): +    for b in range(1, K + 1): +        temp = gcd(a, b) +        if temp != 1: +            for c in range(1, K + 1): +                plus += gcd(temp, c) +        else: +            plus += K