import math

K = int(eval(input()))

total = 0

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

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

        a=math.gcd(x, y)

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

            total = total+math.gcd(z, a)

print(total)

import math

K = int(eval(input()))

total = 0

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

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

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

            total = total+math.gcd(x, math.gcd(y, z))

print(total)

import math

K = int(eval(input()))
total = 0
for x in range(1, K + 1):
    for y in range(1, K + 1):
        a = math.gcd(x, y)
        for z in range(1, K + 1):
            total = total + math.gcd(z, a)
print(total)

import math

K = int(eval(input()))
total = 0
for x in range(1, K + 1):
    for y in range(1, K + 1):
        for z in range(1, K + 1):
            total = total + math.gcd(x, math.gcd(y, z))
print(total)

-        a = math.gcd(x, y) -            total = total + math.gcd(z, a) +            total = total + math.gcd(x, math.gcd(y, z))