from math import gcd

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)
import math

k = int(eval(input()))

sum = 0



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

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

        x = math.gcd(a, b)

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

            sum += math.gcd(x, c)



print(sum)
from math import gcd

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)

import math

k = int(eval(input()))
sum = 0
for a in range(1, k + 1):
    for b in range(1, k + 1):
        x = math.gcd(a, b)
        for c in range(1, k + 1):
            sum += math.gcd(x, c)
print(sum)

-from math import gcd +import math +        x = math.gcd(a, b) -            sum += gcd(gcd(a, b), c) +            sum += math.gcd(x, c)