def euclid(a, b):

  division = a if (a > b) else b

  divider = b if (a > b) else a

  reminder = 0

  while divider != 0:

    reminder = division % divider

    division = divider

    divider = reminder

  

  return division



K = int(eval(input()))



result = 0

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

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

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

      gcd = euclid(euclid(k, l), m)

      result += gcd



print(result)

from math import gcd

K = int(eval(input()))



result = 0

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

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

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

      gcd_res = gcd(gcd(k, l), m)

      result += gcd_res



print(result)

def euclid(a, b):
    division = a if (a > b) else b
    divider = b if (a > b) else a
    reminder = 0
    while divider != 0:
        reminder = division % divider
        division = divider
        divider = reminder
    return division


K = int(eval(input()))
result = 0
for k in range(1, K + 1):
    for l in range(1, K + 1):
        for m in range(1, K + 1):
            gcd = euclid(euclid(k, l), m)
            result += gcd
print(result)

from math import gcd

K = int(eval(input()))
result = 0
for k in range(1, K + 1):
    for l in range(1, K + 1):
        for m in range(1, K + 1):
            gcd_res = gcd(gcd(k, l), m)
            result += gcd_res
print(result)

-def euclid(a, b): -    division = a if (a > b) else b -    divider = b if (a > b) else a -    reminder = 0 -    while divider != 0: -        reminder = division % divider -        division = divider -        divider = reminder -    return division - +from math import gcd -            gcd = euclid(euclid(k, l), m) -            result += gcd +            gcd_res = gcd(gcd(k, l), m) +            result += gcd_res