K = int(eval(input()))



def gcd(x, y):

    while y:

        x, y = y, x % y

    return x



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)
K = int(eval(input()))



from math import gcd



ans = 0



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

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

		t = gcd(a, b)

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

			ans += gcd(t, c)

print(ans)

K = int(eval(input()))


def gcd(x, y):
    while y:
        x, y = y, x % y
    return x


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)

K = int(eval(input()))
from math import gcd

ans = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        t = gcd(a, b)
        for c in range(1, K + 1):
            ans += gcd(t, c)
print(ans)

- - -def gcd(x, y): -    while y: -        x, y = y, x % y -    return x - +from math import gcd +        t = gcd(a, b) -            ans += gcd(gcd(a, b), c) +            ans += gcd(t, c)