import math

K = int(eval(input()))

rst = 0

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

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

        tmp = math.gcd(i, j)

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

            rst += math.gcd(tmp, k)

print(rst)

import math



K = int(eval(input()))

sum = 0



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

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

    tmp = math.gcd(i, j)

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

      sum += math.gcd(tmp, k)

      

print(sum)

import math

K = int(eval(input()))
rst = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        tmp = math.gcd(i, j)
        for k in range(1, K + 1):
            rst += math.gcd(tmp, k)
print(rst)

import math

K = int(eval(input()))
sum = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        tmp = math.gcd(i, j)
        for k in range(1, K + 1):
            sum += math.gcd(tmp, k)
print(sum)

-rst = 0 +sum = 0 -            rst += math.gcd(tmp, k) -print(rst) +            sum += math.gcd(tmp, k) +print(sum)