from math import gcd

# def gcd(a, b):

#     if a%b==0: return b

#     return gcd(b, a%b)

K=int(eval(input()))

sum=0

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

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

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

            sum+=gcd(gcd(a,b),c)

print(sum)
def gcd(a, b):

    if a%b==0: return b

    return gcd(b, a%b)

K=int(eval(input()))

sum=0

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

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

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

            if a==b==c: sum+=a

            elif a!=b!=c: sum+=gcd(gcd(a,b),c)*6 

            else: sum+=gcd(gcd(a,b),c)*3

print(sum)
from math import gcd

# def gcd(a, b):
#     if a%b==0: return b
#     return gcd(b, a%b)
K = int(eval(input()))
sum = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        for c in range(1, K + 1):
            sum += gcd(gcd(a, b), c)
print(sum)

def gcd(a, b):
    if a % b == 0:
        return b
    return gcd(b, a % b)


K = int(eval(input()))
sum = 0
for a in range(1, K + 1):
    for b in range(a, K + 1):
        for c in range(b, K + 1):
            if a == b == c:
                sum += a
            elif a != b != c:
                sum += gcd(gcd(a, b), c) * 6
            else:
                sum += gcd(gcd(a, b), c) * 3
print(sum)

-from math import gcd +def gcd(a, b): +    if a % b == 0: +        return b +    return gcd(b, a % b) -# def gcd(a, b): -#     if a%b==0: return b -#     return gcd(b, a%b) + -    for b in range(1, K + 1): -        for c in range(1, K + 1): -            sum += gcd(gcd(a, b), c) +    for b in range(a, K + 1): +        for c in range(b, K + 1): +            if a == b == c: +                sum += a +            elif a != b != c: +                sum += gcd(gcd(a, b), c) * 6 +            else: +                sum += gcd(gcd(a, b), c) * 3