import math

from functools import reduce



def gcd(*numbers):

    return reduce(math.gcd, numbers)



k = int(eval(input()))



ans = 0

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

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

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

            ans += gcd(a, b, c)



print(ans)

            
import math



def gcd(*numbers):

    return math.gcd( math.gcd(a, b), c)



k = int(eval(input()))



ans = 0

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

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

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

            ans += gcd(a, b, c)



print(ans)

            
import math
from functools import reduce


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


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

import math


def gcd(*numbers):
    return math.gcd(math.gcd(a, b), c)


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

-from functools import reduce -    return reduce(math.gcd, numbers) +    return math.gcd(math.gcd(a, b), c)