import math

from functools import reduce



def gcd(*numbers):

    return reduce(math.gcd, numbers)

k = int(eval(input()))

s = 0

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

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

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

      s += gcd(a,b,c)

print(s)

      
from math import gcd

k = int(eval(input()))

count = 0

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

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

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

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

print(count)
import math
from functools import reduce


def gcd(*numbers):
    return reduce(math.gcd, numbers)


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

from math import gcd

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

-import math -from functools import reduce - - -def gcd(*numbers): -    return reduce(math.gcd, numbers) - +from math import gcd -s = 0 +count = 0 -            s += gcd(a, b, c) -print(s) +            count += gcd(gcd(a, b), c) +print(count)