import math

K = int(eval(input()))



ans = 0

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

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

        d = math.gcd(a, b)

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

            ans += math.gcd(d, c)



print(ans)
import math

K = int(eval(input()))



ans = 0



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

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

        ab = math.gcd(a, b)

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

            abc = math.gcd(ab, c)

            if a == b == c:

                m = 1

            elif a == b or b == c or c == a:

                m = 3

            else:

                m = 6

            ans += m * abc



print(ans)
import math

K = int(eval(input()))
ans = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        d = math.gcd(a, b)
        for c in range(1, K + 1):
            ans += math.gcd(d, c)
print(ans)

import math

K = int(eval(input()))
ans = 0
for a in range(1, K + 1):
    for b in range(a, K + 1):
        ab = math.gcd(a, b)
        for c in range(b, K + 1):
            abc = math.gcd(ab, c)
            if a == b == c:
                m = 1
            elif a == b or b == c or c == a:
                m = 3
            else:
                m = 6
            ans += m * abc
print(ans)

-    for b in range(1, K + 1): -        d = math.gcd(a, b) -        for c in range(1, K + 1): -            ans += math.gcd(d, c) +    for b in range(a, K + 1): +        ab = math.gcd(a, b) +        for c in range(b, K + 1): +            abc = math.gcd(ab, c) +            if a == b == c: +                m = 1 +            elif a == b or b == c or c == a: +                m = 3 +            else: +                m = 6 +            ans += m * abc