from math import gcd



k = int(eval(input()))

ans = 0

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

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

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

      ans += gcd(gcd(l,m),n)



print(ans)
import math

import itertools



k = int(eval(input()))

lst = [i for i in range(1,k+1)]

itr = itertools.combinations_with_replacement(lst, 3)

ans = 0



for i in itr:

  st = set(i)

  num = len(st)

  if num == 1:

    ans += i[0]

  elif num == 2:

    a,b = st

    ans += math.gcd(a,b) * 3

  else:

    ans += math.gcd(math.gcd(i[0],i[1]), i[2]) * 6



print(ans)
from math import gcd

k = int(eval(input()))
ans = 0
for l in range(1, k + 1):
    for m in range(1, k + 1):
        for n in range(1, k + 1):
            ans += gcd(gcd(l, m), n)
print(ans)

import math
import itertools

k = int(eval(input()))
lst = [i for i in range(1, k + 1)]
itr = itertools.combinations_with_replacement(lst, 3)
ans = 0
for i in itr:
    st = set(i)
    num = len(st)
    if num == 1:
        ans += i[0]
    elif num == 2:
        a, b = st
        ans += math.gcd(a, b) * 3
    else:
        ans += math.gcd(math.gcd(i[0], i[1]), i[2]) * 6
print(ans)

-from math import gcd +import math +import itertools +lst = [i for i in range(1, k + 1)] +itr = itertools.combinations_with_replacement(lst, 3) -for l in range(1, k + 1): -    for m in range(1, k + 1): -        for n in range(1, k + 1): -            ans += gcd(gcd(l, m), n) +for i in itr: +    st = set(i) +    num = len(st) +    if num == 1: +        ans += i[0] +    elif num == 2: +        a, b = st +        ans += math.gcd(a, b) * 3 +    else: +        ans += math.gcd(math.gcd(i[0], i[1]), i[2]) * 6