import math



K = int(eval(input()))



res = [math.gcd(a, math.gcd(b,c))  for a in range(1, K+1) for b in range(1, K+1) for c in range(1, K+1)]

 





print((sum(res)))



from math import gcd



K = int(eval(input()))



res = [gcd(a, gcd(b,c))  for a in range(1, K+1) for b in range(1, K+1) for c in range(1, K+1)]

 





print((sum(res)))



import math

K = int(eval(input()))
res = [
    math.gcd(a, math.gcd(b, c))
    for a in range(1, K + 1)
    for b in range(1, K + 1)
    for c in range(1, K + 1)
]
print((sum(res)))

from math import gcd

K = int(eval(input()))
res = [
    gcd(a, gcd(b, c))
    for a in range(1, K + 1)
    for b in range(1, K + 1)
    for c in range(1, K + 1)
]
print((sum(res)))

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